// pages/main/addtionalreduce/home.js
var app = getApp();
var baseUrl = app.globalData.baseUrl;
Page({

  /**
   * Page initial data
   */
  data: {
    datas: ["", ""],
    is_declared: false,
    hascompleted:false,
    additionlist: [],
    declareStatus: {
      "to_declare ": "待申报",
      "declaring": "申报中",
      "success": "审核成功",
      "failed": "审核失败"
    },
    additiontitle: {
      "children_education": "子女教育",
      "continuing_education": "继续教育",
      "support_duty": "赡养老人",
      "medical_fund": "大病医疗",
      "house_fund_rent": "住房",
      "house_fund_loan": "住房",
      "house_fund": "住房"
    },
    reducetype:{
      "":"月",
      "month":"月",
      "year":"年"
    },
    datas02: ["子女教育", "继续教育", "赡养老人", "大病医疗", "住房贷款利息"]
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function (options) {
    console.log("onLoad");
  },

  godeclare: function (e) {
    console.log("godeclare", e);
    var newdata = this.data.additionlist[e.currentTarget.id]
    wx.navigateTo({
      url: 'godeclare/godeclare?legal_entity_id=' + newdata.legal_entity_id + "&legal_entity=" + newdata.legal_entity + "&type=0",
    })
  },

  goupdate: function (e) {
    console.log("goupdate", e);
    var newdata = this.data.additionlist[e.currentTarget.id]
    wx.navigateTo({
      url: 'godeclare/godeclare?legal_entity_id=' + newdata.legal_entity_id + "&legal_entity=" + newdata.legal_entity + "&select_data=" + JSON.stringify(newdata) + "&type=1",
    })
  },

  godetail: function () {
    wx.navigateTo({
      url: 'godetail/godetail',
    })
  },

  lookandadd: function (e) {
    var str = e.currentTarget.id.split('-')
    var data = this.data.additionlist;
    console.log("lookandadd", str);

    wx.navigateTo({
      url: 'lookandupdate/lookandupdate?status=' + data[str[0]].addition_items[str[1]].addition_category + "&employee_status=" + data[str[0]].employee_status + "&legal_entity_id=" + data[str[0]].legal_entity_id + "&legal_entity=" + data[str[0]].legal_entity,
    })
  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function () {
    console.log("onReady");
  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function () {
    console.log("onShow");
    var that = this
    wx.showLoading({
      title: '',
    })
    this.getAdditionList()
  },

  getAdditionList: function () {
    var that = this;
    var Authorization = getApp().globalData.Authorization;
    wx.request({
      url: baseUrl + 'persontax/v1/person-additions',
      // data: {
      //   limit: 10,
      //   offset: 0
      // },
      header: {
        'content-type': 'application/json',
        "Authorization": Authorization
      },
      success: function (res) {
        if (res && res.data) {
          console.log("res", res)
          that.setData({
            additionlist: that.handleData(res.data.items) 
          })
        }
        wx.hideLoading()
      },
      complete(res) {
        that.setData({
          hascompleted: true
        })
        console.log(res.data)
      }
    })
  },
  handleData: function (items) {
    for (var i = 0; i < items.length ; i++){
      var bigItem = items[i]
      for (var j = 0; j < bigItem.addition_items.length; j++) {
        var midItem = bigItem.addition_items[j]
        if (midItem.addition_category == 'continuing_education'){
          var majors = [], professions = []
          for (var k = 0; k < midItem.detail_items.length; k++) {
            var smallItem = midItem.detail_items[k]
            if (smallItem.continue_education_type == 'major'){
              majors.push(smallItem)
            } else if (smallItem.continue_education_type == 'nomajor'){
              professions.push(smallItem)
            }
          }
          items[i].addition_items[j].majors = majors
          items[i].addition_items[j].professions = professions
          break;
        }
      } 
    }
    console.log('ddddd', items)
    return items
  },
  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function () {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function () {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function () {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function () {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function () {

  }
})