// pages/main/addtionalreduce/home.js
var app = getApp();
var baseUrl = app.globalData.baseUrl;
var format = require('../../../utils/util.js');
Page({

  /**
   * Page initial data
   */
  data: {
    datas: ["", ""],
    is_declared: false,
    hascompleted:false,
    additionlist: [],
    complete_item:[],
    choosed_date: '',
    addition_data_last_year:false,//上一年是否有数据
    has_showModal:false,//当前页面是否展示过modal
    show_modal:false,
    tip_detail:"尊敬的用户您好!2020年度的专项附加扣除申报已经启动,请您及时确认填报信息,确保专项附加扣除能正常享受扣除。",
    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");
    var mydate = new Date();
    this.setData({
      choosed_date: mydate.getFullYear() ,
    })
  },

  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) + "&complete_data=" + JSON.stringify(this.data.complete_item) + "&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,data);

    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
    this.getAdditionList()
  },

  //获取申报列表
  getAdditionList: function () {
    wx.showLoading({
      title: '',
    })
    var that = this;
    var Authorization = getApp().globalData.Authorization;
    wx.request({
      url: baseUrl + 'persontax/v1/person-additions',
      data: {
        years:that.data.choosed_date
        // limit: 10,
        // offset: 0
      },
      header: {
        'content-type': 'application/json',
        "Authorization": Authorization
      },
      success: function (res) {
        if (res.statusCode==200 && res.data) {
          console.log("show_modal", !res.data.new_addition_status)
          that.setData({
            addition_data_last_year: res.data.last_addition_status,
            show_modal: !that.data.has_showModal&&!res.data.new_addition_status,
            complete_item: res.data.complete_item,
            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('items', items)
    return items
  },

  datePickerChange: function (e) {
    console.log("e", e)
    var timestamp = Date.parse(new Date(e.detail.value)) / 1000
    this.setData({
      choosed_date: format.yearFormString(timestamp) 
    })
    this.getAdditionList()
  }, 

// 暂不确认
  doNotConfirm:function(){
    this.setData({
      show_modal: false,
      has_showModal: true
    })
  },

// 立即确认
  doConfirm: function () {
    this.setData({
      show_modal: false,
      has_showModal:true
    })
    this.quickAdd()
   },

// 一键带入
  quickAdd:function(){
    var that = this;
    var Authorization = getApp().globalData.Authorization;
    wx.request({
      url: baseUrl + 'persontax/v1/person-additions-update',
      method:"POST",
      data: {
        user_id: app.globalData.userInfo?app.globalData.userInfo.id:""
      },
      header: {
        'content-type': 'application/json',
        "Authorization": Authorization
      },
      success: function (res) {
        if (res.statusCode==200 && res.data) {
          console.log("res", res)
          that.setData({
            choosed_date: "2020"
          })
          that.getAdditionList()
        }
        wx.hideLoading()
      },
      complete(res) {
        console.log(res.data)
      }
    })
  },

  /**
   * 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 () {

  }
})