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

const app = getApp();
var intervalBuild = 0;
var TestReportID = 0;

Page({
  data: {
    IsSelect: true,
    IsBuild: 0,
    IsIPhoneX: app.globalData.IsIPhoneX,
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      ReportType: options.type,
      Containnerheight: main.getWindowHeight(),
    });

    that.init();
  },
  init: function () {
    var that = this;
    TestReportID = 0;
    var TestTask = wx.getStorageSync("TestTask");
    var list = TestTask.List;
    var listRight = [];
    var listSkip = [];
    var listWrong = [];
    TestTask.TestRightStr = [];
    TestTask.TestWrongStr = [];
    TestTask.TestSkipStr = [];

    if (list) {
      for (var i = 0; i < list.length; i++) {
        // if (TestTask.BookID > 12) {
        //   list[i].Field1 = list[i].ContentNew.Field[1][0].Content;
        //   list[i].Word = list[i].ID;
        // }
        if (list[i].Result == 1) {
          listRight.push(list[i]);
          TestTask.TestRightStr.push(list[i].Word);
        } else if (list[i].Result == -1) {
          listWrong.push(list[i]);
          TestTask.TestWrongStr.push(list[i].Word);
        } else if (list[i].Result == 0) {
          listSkip.push(list[i]);
          TestTask.TestSkipStr.push(list[i].Word);
        }
      }

      var result = [];
      if (listWrong.length > 0) {
        result.push({
          Name: "答错的题目",
          Expand: 1,
          List: listWrong
        });
        if (listSkip.length > 0)
          result.push({
            Name: "跳过的题目",
            Expand: 1,
            List: listSkip
          });
        if (listRight.length > 0)
          result.push({
            Name: "答对的题目",
            Expand: 1,
            List: listRight
          });
      } else {
        if (listSkip.length > 0) {
          result.push({
            Name: "跳过的题目",
            Expand: 1,
            List: listSkip
          });
          if (listRight.length > 0)
            result.push({
              Name: "答对的题目",
              Expand: 1,
              List: listRight
            });
        } else {
          if (listRight.length > 0)
            result.push({
              Name: "答对的题目",
              Expand: 1,
              List: listRight
            });
        }
      }

      var name = "";
      if (TestTask.Name && TestTask.Name.indexOf("#") > 0) {
        name = TestTask.Name;
        name = name.substr(name.indexOf("#") + 1);
        if (TestTask.TestType == "read")
          name += " 会念";
        else
          name += " 会写";
      }

      that.setData({
        NumberWrong: listWrong.length,
        NumberSkip: listSkip.length,
        NumberRight: listRight.length,
        BookID: Number(TestTask.BookID),
        Name: name,
        List: result,
        NumberTotal: list.length,
      });

      if (that.data.ReportType == "new")
        that.saveInfo(TestTask);

      wx.setNavigationBarTitle({
        title: "报告"
      });
    }

  },
  expandPanel: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (that.data.List[index].Expand == "1")
      that.data.List[index].Expand = "0";
    else
      that.data.List[index].Expand = "1";
    that.setData({
      List: that.data.List,
    });
  },
  onSelectItem: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var word = e.currentTarget.dataset.word;
    for (var i = 0; i < that.data.List[index].List.length; i++) {
      if (that.data.List[index].List[i].Word == word) {
        if (that.data.List[index].List[i].Css == "Select") {
          that.data.List[index].List[i].Css = "";
          that.setData({
            List: that.data.List,
          });
        } else if (that.data.List[index].List[i].Css == "Select2") {
          wx.showToast({
            title: '已经生成过了',
            image: "../images/universalpic_ribbon_white_96x120.png",
          });
        } else {
          that.data.List[index].List[i].Css = "Select";
          that.setData({
            List: that.data.List,
          });
        }
        break;
      }
    }
  },
  buildCard: function () {
    var that = this;
    var arr = [],
      TestExistStr = [];
    for (var i = 0; i < that.data.List.length; i++) {
      for (var j = 0; j < that.data.List[i].List.length; j++) {
        if (that.data.List[i].List[j].Css === "Select") {
          var item = that.data.List[i].List[j];
          var obj = {};
          obj.Field1 = item.Content[1].Content;
          obj.Field2 = item.Content[2].Content;
          obj.Field3 = "";
          obj.Tags = item.Content[0].Content;
          if (!common.checkIsArray(obj.Tags))
            obj.Tags = obj.Tags.split(",");
          obj.ID = 0;
          obj.LimitTime = common.formatTime(new Date());
          obj.FontSize = item.FontSize;
          arr.push(obj);
          TestExistStr.push(item.Word);
          
        } else if (that.data.List[i].List[j].Css === "Select2") {
          var item = that.data.List[i].List[j];
          TestExistStr.push(item.Word);
        }
      }
    }
    //console.log(arr);

    if (arr.length > 0) {
      that.setData({
        IsBuild: 1,
      });
      var buildCardNumber = 0;
      intervalBuild = setInterval(function () {
        that.addCard(arr[buildCardNumber]);
        buildCardNumber++;
        that.setData({
          BuildCardNumber: Math.round(100 * buildCardNumber / arr.length) + "%",
        });
        if (buildCardNumber >= arr.length) {
          clearInterval(intervalBuild);
          var TestTask = {};
          TestTask.TestExistStr = TestExistStr;
          
          that.saveInfo(TestTask);
          that.setData({
            IsBuild: 2,
          });
        }
      }, 500);
    } else {
      wx.showToast({
        title: '先选题目',
        image: "../images/universalpic_wrong_white_120x120.png",
      });
    }

  },
  addCard: function (card) {
    var userid = app.globalData.userInfo.UserID;
    main.postData('AddMiaoguoCard?Type=add&UserID=' + userid, card, function (data) {

    });
  },
  close: function (e) {
    wx.removeStorageSync("TestTask");
    var delta = 2;
    if (this.data.ReportType == "edit")
      delta = 1;
    wx.navigateBack({
      delta: delta,
    });
  },
  saveInfo: function (TestTask) {
    var that = this;
    clearInterval(intervalBuild);
    var userid = app.globalData.userInfo.UserID;
    var param1 = wx.getStorageSync("TestTask");
    if (param1.List)
      param1.TestTotal = param1.List.length;

    var symbol="";
    if (that.data.BookID > 12) {
      symbol=","
    }
    if (TestTask.TestRightStr)
      param1.TestRightStr = TestTask.TestRightStr.join(symbol);
    if (TestTask.TestWrongStr)
      param1.TestWrongStr = TestTask.TestWrongStr.join(symbol);
    if (TestTask.TestSkipStr)
      param1.TestSkipStr = TestTask.TestSkipStr.join(symbol);
    if (TestTask.TestExistStr)
      param1.TestExistStr = TestTask.TestExistStr.join(symbol);

    if (TestReportID)
      param1.ID = TestReportID;

    param1.BookID=that.data.BookID;
    wx.setStorageSync('TestTask', param1);
    
    delete param1.List;
    delete param1.BookID;

    main.postData('InsertTestReport?UserID=' + userid, param1, function (data) {
      if (data){
        TestReportID = data;
      }
    });
  },
  gotoReport: function (e) {
    if ((this.data.NumberRight + this.data.NumberWrong + this.data.NumberSkip) >= this.data.NumberTotal) {
      wx.showToast({
        title: '已经完成！',
        image: "../images/universalpic_wrong_white_120x120.png",
      });
    } else {
      var testTask = wx.getStorageSync("TestTask");
      if (TestReportID)
        testTask.ID=TestReportID;
      wx.setStorageSync("TestTask2", testTask);
      var url = "./item?type=edit";
      wx.redirectTo({
        url: url,
      });
    }
  },
  onUnload: function () {
    wx.removeStorageSync("TestTask");
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})