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

const app = getApp();
var testGradeArr=constant.testGradeArr;

Page({
  data: {
    List: [],
    TestSelectGrade: 0,
    IsIPhoneX: app.globalData.IsIPhoneX,
    IsIPad:app.globalData.IsIPad,
    ScrollLeft:0,
  },
  onLoad: function (options) {
    if (options.UserID){
      app.globalData.userInfo={};
      app.globalData.userInfo.UserID=options.UserID;
    }

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

    main.getHanzi(function (arr) {
      if (!arr) {
        wx.removeStorageSync("HanziUpdateTime");
        wx.removeStorageSync("HanziAll");
        main.getHanzi(function (arr) {});
      }
    });
  },
  onShow: function () {
    this.getTestReportList();
  },
  selectSubject:function(e){
    var subject=e.currentTarget.dataset.type;
    var that = this;
    that.setData({
      Subject: subject,
    });
  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    if (url=="../other/userbook"){

    }
    else if (e.currentTarget.dataset.type=="Shici"){
      var name1=e.currentTarget.dataset.name1;
      name1=name1.replace("语文古诗文","语文");
      url="./item?testtype=recite&name1="+name1+"&bookid="+e.currentTarget.dataset.bookid;
    }
    else if (e.currentTarget.dataset.type=="Word" && e.currentTarget.dataset.bookid==100){
      var name1=e.currentTarget.dataset.name1;
      url="./item?testtype=read&name1="+name1+"&name2=英文字母&bookid=100";
    }
    else if (e.currentTarget.dataset.name1 && e.currentTarget.dataset.bookid){
      url="./list?name1="+e.currentTarget.dataset.name1+"&id="+e.currentTarget.dataset.bookid;
    }
    wx.navigateTo({
      url: url,
    });
  },
  getTestReportList: function () {
    var that = this;
    for(var i=0;i<testGradeArr.length;i++){
      for(var j=0;j<testGradeArr[i].List.length;j++){
        testGradeArr[i].List[j].Finish=0;
      }
    }
    that.setData({
      GradeArr: testGradeArr,
    });
    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 || data[i].Name[0].indexOf("语文词语")>=0 || data[i].Name[0].indexOf("识字 上学期")>=0 || data[i].Name[0].indexOf("拼音")>=0 || data[i].Name[1].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 (item.List[k].ID==101 && dataTemp[i].BookID==101){
                  for(var n=1;n<=4;n++){
                    if (Number(dataTemp[i].Name[2])==n && dataTemp[i].IsFinished==1)
                      item.List[k].Finish++;
                  }
                }
                //古诗词计算进度
                else if (item.List[k].ID==dataTemp[i].BookID && dataTemp[i].TestType=="recite" && dataTemp[i].IsFinished == 1)
                  item.List[k].Finish=1;
                else{
                  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);
            item.List[k].NameStr=item.List[k].Name.replace("语文","");
          }
        }

        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
              }
            }
          });

          if (that.data.TestSelectGrade<9){
            that.setData({
              Subject:"CHN",
            });
            if (that.data.TestSelectGrade>4){
              that.setData({
                ScrollLeft:500,
              });
            }
          }
          else{
            that.setData({
              Subject:"ENG",
            });
            if (that.data.TestSelectGrade>11){
              that.setData({
                ScrollLeft:500,
              });
            }
          }
        });
      }
    });
  },
  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,
    });
    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");
  },
  gotoReLaunch: function (e) {
    var url=e.currentTarget.dataset.url;
    wx.reLaunch({
      url: url,
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})