import common from '../../utils/util';
import main from '../../utils/main';

const app = getApp();

Page({
  data: {
    List: [],
    IsSelectGrade: false,
    TestSelectGrade: 0,

  },
  onLoad: function (options) {
    if (options.UserID){
      app.globalData.userInfo={};
      app.globalData.userInfo.UserID=options.UserID;
    }

    var that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
    });

    main.getHanzi(function (arr) {
      if (!arr) {
        wx.removeStorageSync("HanziUpdateTime");
        wx.removeStorageSync("HanziAll");
        main.getHanzi(function (arr) {});
      }
    });
  },
  onShow: function () {
    this.getTestReportList();
  },
  showSelectGrade: function () {
    var that = this;
    that.setData({
      IsSelectGrade: true,
    });
  },
  closeSelectGrade: function () {
    var that = this;
    that.setData({
      IsSelectGrade: false,
    });
  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });
  },
  getTestReportList: function () {
    var that = this;
    that.setData({
      GradeArr: [{
          Name: "一年级",
          Name2: "语文一年级",
          Select: "Select",
          List: [{
            ID: 1,
            Type: "Shizi",
            Name: "语文识字",
            Name2: "上学期",
            Finish: 0,
            Total: 12,
            Image: "../images/examine_subject_a001.png"
          }, {
            ID: 13,
            Type: "Shizi",
            Name: "语文写字",
            Name2: "上学期",
            Finish: 0,
            Total: 26,
            Image: "../images/examine_subject_a001.png"
          }, {
            ID: 2,
            Type: "Shizi",
            Name: "语文识字",
            Name2: "下学期",
            Finish: 0,
            Total: 14,
            Image: "../images/examine_subject_a002.png"
          }, {
            ID: 14,
            Type: "Shizi",
            Name: "语文写字",
            Name2: "下学期",
            Finish: 0,
            Total: 28,
            Image: "../images/examine_subject_a002.png"
          }]
        },
        {
          Name: "二年级",
          Name2: "语文二年级",
          Select: "",
          List: [{
            ID: 3,
            Type: "Shizi",
            Name: "语文识字",
            Name2: "上学期",
            Finish: 0,
            Total: 16,
            Image: "../images/examine_subject_a003.png"
          }, {
            ID: 15,
            Type: "Shizi",
            Name: "语文写字",
            Name2: "上学期",
            Finish: 0,
            Total: 28,
            Image: "../images/examine_subject_a003.png"
          }, {
            ID: 4,
            Type: "Shizi",
            Name: "语文识字",
            Name2: "下学期",
            Finish: 0,
            Total: 16,
            Image: "../images/examine_subject_a004.png"
          }, {
            ID: 16,
            Type: "Shizi",
            Name: "语文写字",
            Name2: "下学期",
            Finish: 0,
            Total: 29,
            Image: "../images/examine_subject_a004.png"
          }]
        },
        {
          Name: "三年级",
          Name2: "语文三年级",
          Select: "",
          List: [{
            ID: 5,
            Type: "Shizi",
            Name: "语文识字",
            Name2: "上学期",
            Finish: 0,
            Total: 16,
            Image: "../images/examine_subject_a005.png"
          }, {
            ID: 17,
            Type: "Shizi",
            Name: "语文写字",
            Name2: "上学期",
            Finish: 0,
            Total: 28,
            Image: "../images/examine_subject_a005.png"
          }, {
            ID: 6,
            Type: "Shizi",
            Name: "语文识字",
            Name2: "下学期",
            Finish: 0,
            Total: 16,
            Image: "../images/examine_subject_a006.png"
          }, {
            ID: 18,
            Type: "Shizi",
            Name: "语文写字",
            Name2: "下学期",
            Finish: 0,
            Total: 21,
            Image: "../images/examine_subject_a006.png"
          }]
        },
        {
          Name: "四年级",
          Name2: "语文四年级",
          Select: "",
          List: [{
            ID: 7,
            Type: "Shizi",
            Name: "语文识字",
            Name2: "上学期",
            Finish: 0,
            Total: 16,
            Image: "../images/examine_subject_a007.png"
          },  {
            ID: 19,
            Type: "Shizi",
            Name: "语文写字",
            Name2: "上学期",
            Finish: 0,
            Total: 20,
            Image: "../images/examine_subject_a007.png"
          }, {
            ID: 8,
            Type: "Shizi",
            Name: "语文识字",
            Name2: "下学期",
            Finish: 0,
            Total: 16,
            Image: "../images/examine_subject_a008.png"
          },  {
            ID: 20,
            Type: "Shizi",
            Name: "语文写字",
            Name2: "下学期",
            Finish: 0,
            Total: 20,
            Image: "../images/examine_subject_a008.png"
          }]
        },
        {
          Name: "五年级",
          Name2: "语文五年级",
          Select: "",
          List: [{
            ID: 9,
            Type: "Shizi",
            Name: "语文识字",
            Name2: "上学期",
            Finish: 0,
            Total: 16,
            Image: "../images/examine_subject_a009.png"
          }, {
            ID: 21,
            Type: "Shizi",
            Name: "语文写字",
            Name2: "上学期",
            Finish: 0,
            Total: 21,
            Image: "../images/examine_subject_a009.png"
          }, {
            ID: 10,
            Type: "Shizi",
            Name: "语文识字",
            Name2: "下学期",
            Finish: 0,
            Total: 16,
            Image: "../images/examine_subject_a010.png"
          }, {
            ID: 22,
            Type: "Shizi",
            Name: "语文写字",
            Name2: "下学期",
            Finish: 0,
            Total: 16,
            Image: "../images/examine_subject_a010.png"
          }
        ]
        },
        {
          Name: "六年级",
          Name2: "语文六年级",
          Select: "",
          List: [
            {
              ID: 23,
              Type: "Shizi",
              Name: "语文写字",
              Name2: "上学期",
              Finish: 0,
              Total: 18,
              Image: "../images/examine_subject_a011.png"
            }, {
              ID: 24,
              Type: "Shizi",
              Name: "语文写字",
              Name2: "下学期",
              Finish: 0,
              Total: 12,
              Image: "../images/examine_subject_a012.png"
            }, 
          ]
        },
        {
          Name: "英语常用单词",
          Name2: "英语常用单词",
          Select: "",
          List: [
            {
              ID: 101,
              Type: "Word",
              Name: "常用单词1",
              Name2: "词汇1~220",
              Finish: 0,
              Total: 22,
              Image: "../images/examine_subject_b001.png"
            },
            {
              ID: 102,
              Type: "Word",
              Name: "常用单词2",
              Name2: "词汇221~500",
              Finish: 0,
              Total: 28,
              Image: "../images/examine_subject_b002.png"
            },
            {
              ID: 103,
              Type: "Word",
              Name: "常用单词3",
              Name2: "词汇500~1000",
              Finish: 0,
              Total: 50,
              Image: "../images/examine_subject_b003.png"
            },
            {
              ID: 104,
              Type: "Word",
              Name: "常用单词4",
              Name2: "词汇1001~1500",
              Finish: 0,
              Total: 50,
              Image: "../images/examine_subject_b004.png"
            },
            {
              ID: 105,
              Type: "Word",
              Name: "常用单词5",
              Name2: "词汇1501~2000",
              Finish: 0,
              Total: 50,
              Image: "../images/examine_subject_b005.png"
            },
          ]
        },
      ],
    })
    var userid = app.globalData.userInfo.UserID;
    main.getData('GetTestReportList?UserID=' + userid, function (data) {
      if (data) {
        var dataTemp = [];
        for (var i = 0; i < data.length; i++) {
          for (var j = 0; j < dataTemp.length; j++) {
            if (dataTemp[j].Name[0] == data[i].Name[0] &&
              dataTemp[j].Name[1] == data[i].Name[1] &&
              dataTemp[j].TestType == data[i].TestType &&
              data[i].IsFinished == 1
            ) {
              dataTemp.splice(j, 1);
              break;
            }
          }
          dataTemp.push(data[i]);

          if (data[i].Name[0].indexOf("英语")>=0){
            if (data[i].TestRightStr){
              data[i].TestRightStr=data[i].TestRightStr.split(",");
            }
            if (data[i].TestWrongStr){
              data[i].TestWrongStr=data[i].TestWrongStr.split(",");
            }          
            if (data[i].TestSkipStr){
              data[i].TestSkipStr=data[i].TestSkipStr.split(",");
            }
            if (data[i].TestExistStr){
              data[i].TestExistStr=data[i].TestExistStr.split(",");
            }
          }
          
          
          if (!data[i].TestRightStr)
            data[i].TestRightStr="";
          if (!data[i].TestWrongStr)
            data[i].TestWrongStr="";
          if (!data[i].TestSkipStr)
            data[i].TestSkipStr="";
          if (!data[i].TestExistStr)
            data[i].TestExistStr="";
                
        }

        for (var i = 0; i < dataTemp.length; i++) {
          var name = dataTemp[i].Name[0].split(" ");
          for (var j = 0; j < that.data.GradeArr.length; j++) {
            var item = that.data.GradeArr[j];
            if (name[0] == item.Name) {
              for (var k = 0; k < item.List.length; k++) {
                if (name[1] == item.List[k].Name && name[2] == item.List[k].Name2) {
                  if (dataTemp[i].IsFinished == 1)
                    item.List[k].Finish++;
                }
              }
            }
          }
        }

        for (var j = 0; j < that.data.GradeArr.length; j++) {
          var item = that.data.GradeArr[j];
          for (var k = 0; k < item.List.length; k++) {
            item.List[k].Finished = Math.round(100 * item.List[k].Finish / item.List[k].Total);
          }
        }

        that.setData({
          TestReport: data,
          GradeArr: that.data.GradeArr,
        });
        wx.setStorageSync("UserTestReport", data);

        common.getStorageValue(that, "TestSelectGrade", 0, function () {
          that.onSelectGrade({
            currentTarget: {
              dataset: {
                value: that.data.TestSelectGrade
              }
            }
          });
        });
      }
    });
  },
  onSelectGrade: function (e) {
    var index = e.currentTarget.dataset.value;
    for (var i = 0; i < this.data.GradeArr.length; i++)
      this.data.GradeArr[i].Select = "";
    this.data.GradeArr[index].Select = "Select";
    this.setData({
      TestSelectGrade: index,
      GradeArr: this.data.GradeArr,
    });
    this.closeSelectGrade();
    wx.setStorageSync("TestSelectGrade", index);
  },
  gotoTestReportInfo: function (e) {
    var id = e.currentTarget.dataset.id;
    var userid = app.globalData.userInfo.UserID;
    main.GetTestReportInfo(id,function(data){
        wx.setStorageSync("TestTask", data);
        wx.navigateTo({
          url: "./report?type=edit",
        });
    });
  },
  onUnload: function () {
    wx.removeStorageSync("UserTestReport");
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})