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


  data: {
    year: "2019",
    declare_status: "", //申报状态"0" //待申报  "1" //申报成功  “2” 申报中
    refund_status: "", //退税状态  "0" 无需退税; "1" 待退税;"2" 退税中"3" 退税成功;"4"退税失败;"5"放弃退税;
    pay_status: "", //补税状态    "0" 豁免;  "1" 待补税;"2" 补税成功
    declaring_unit_status: "", //1:本单位申报,2:本单位其他申报,3:其他单位申报,4:待确认申报//5用户自行申报
    payinfo: {},
    unitInfo: [],
    showUnit: false,
    declaring_unit_id: "",
    declaring_unit:"",
    showModal_selfDeclare: false,
    uniqual_title01:"",
    uniqual_content01: "",
    declare_text_extra: "",//请等候申报结果
    new_refund_tax:0,
    declare_end_date:"",
    declare_end_days: 0,
    other_end_date: "",
    other_end_days: 0,
    refund_end_date: "",
    refund_end_days: 0,
    show_other_unit_view:false,
    exempt_status:""
  },


  onLoad: function(options) {
   
  },


  onReady: function() {

  },


  onShow: function() {
    this.getPayInfo();
  },

  yearPick: function() {

  },

  getPayInfo: function() {
    var that = this
    this.Authorization = getApp().globalData.Authorization;
    wx.request({
      url: baseUrl + "payroll/v1/settlement-tax/declare-detail",
      header: {
        'Authorization': this.Authorization
      },
      data: {
        'years': that.data.year
      },
      success: function(result) {
        console.log("payinfo", result)
        if (result.statusCode == 200) {
          that.updateText(result.data)
          that.setData({
            declare_status: result.data.declare_status,
            refund_status: result.data.refund_status,
            pay_status: result.data.pay_status,
            declaring_unit: result.data.declaring_unit,
            declaring_unit_status: result.data.declaring_unit_status,
            new_refund_tax: Math.abs(result.data.refund_tax),
            declare_end_date: format.getDateString(result.data.declare_end_time) ,
            declare_end_days: format.daysTillNow(Date.parse(new Date())/1000,result.data.declare_end_time),
            other_end_date: format.getDateString(result.data.other_end_time),
            other_end_days: format.daysTillNow(Date.parse(new Date()) / 1000, result.data.other_end_time),
            refund_end_date: format.getDateString(result.data.refund_end_time),
            refund_end_days: format.daysTillNow(Date.parse(new Date()) / 1000, result.data.refund_end_time),
            exempt_status: result.data.exempt,
            payinfo: result.data
          })
        }
      },
    })
  },

  getUnitInfo: function() { //获取申报单位
    var that = this
    this.Authorization = getApp().globalData.Authorization;
    that.setData({
      showUnit: true
    })
    wx.request({
      url: baseUrl + "payroll/v1/settlement-tax/declare-unit",
      header: {
        'Authorization': this.Authorization
      },
      data: {},
      success: function(result) {
        console.log("unitInfo", result.data)
        if (result.statusCode == 200) {
          that.setData({
            unitInfo: result.data.items
          })
        }
      },
      complete: function(e) {
        that.setData({
          showUnit: true
        })
      }
    })
  },

  updateDeclareState: function(id) { //是否在本单位申报
    var that = this
    this.Authorization = getApp().globalData.Authorization;
    wx.request({
      url: baseUrl + "payroll/v1/settlement-tax/record",
      header: {
        'Authorization': this.Authorization
      },
      method: "PUT",
      data: {
        "years":that.data.year,
        'declaring_unit_id': id,
        "declaring_unit_status": id?'':'5',
      },
      success: function(result) {
        console.log("declareinfo", result)
        if (result.statusCode == 200) {
          that.setData({

          })
          that.getPayInfo();
        }
      },
    })
  },

  // 是否放弃退税,(是:"5",否:"")
  forgiveRefund: function(s) {
    var that = this
    this.Authorization = getApp().globalData.Authorization;
    wx.request({
      url: baseUrl + "payroll/v1/settlement-tax/record",
      header: {
        'Authorization': this.Authorization
      },
      method: "PUT",
      data: {
        "years": that.data.year,
        'refund_status': s
      },
      success: function(result) {
        console.log("refund", result)
        if (result.statusCode == 200) {
          that.getPayInfo();
          that.setData({

          })
        }
      },
    })
  },

  // 确认提交申请退税
  commitRefund: function() {
    var that = this
    this.Authorization = getApp().globalData.Authorization;
    wx.request({
      url: baseUrl + "payroll/v1/settlement-tax/record",
      header: {
        'Authorization': this.Authorization
      },
      method: "PUT",
      data: {
        "years": that.data.year,
        "name": "xxxx", //姓名,
        "bank": "xxxx", //开户行,
        "bank_card_no": "xxxx", //银行卡号,
        "bank_province": "xxxx", //银行省份,
      },
      success: function(result) {
        console.log("refund", result)
        if (result.statusCode == 200) {
          that.setData({

          })
        }
      },
    })
  },


  // 跳转到H5页面测试
  skipH5_01: function() {
    wx.navigateTo({
      url: '../../common/webview?type=1',
    })
  },

  skipH5_02: function() {
    wx.navigateTo({
      url: '../../common/webview?type=2',
    })
  },

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

  declareHere: function() {
    if(this.data.declaring_unit_status==''){
      wx.showToast({
        icon:"none",
        title: '单位还没有启用汇算清缴申报,请等候单位通知',
      })
      return
    }
    this.getUnitInfo()
  },

  declareNotHere: function() {
    if (this.data.declaring_unit_status == '') {
      wx.showToast({
        icon: "none",
        title: '单位还没有启用汇算清缴申报,请等候单位通知',
      })
      return
    }
    var that = this
    wx.showModal({
      title: '',
      content: '确认不在本单位进行汇算清缴申报吗?',
      confirmColor: '#357aeb',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.updateDeclareState("")
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  unitPick: function(e) { //选择申报单位
    console.log("unitPick", e)
    var info_arr = e.currentTarget.id.split("~")
    this.setData({
      declaring_unit_id: info_arr[1] ,
      declaring_unit: info_arr[0],
      showUnit: false,
      show_other_unit_view:true,
    })
  },

  selfdeclare_close() { //关闭自行申报弹窗
    this.setData({
      showModal_selfDeclare: false
    })
  },

  declareBySelf: function() { //查看自行申报方式
    this.setData({
      showModal_selfDeclare: true
    })
  },

  godeclareHistory: function() {
    wx.redirectTo({
      url: '../advancepayment/adPayHome',
    })
  },

  noOtherUnitInfo: function() { //没有其他单位个税申报
    this.updateDeclareState(this.data.declaring_unit_id)
    this.setData({
      show_other_unit_view:false
    })
  },

  otherUnitInfo: function() { //有其他单位个税申报
    wx.navigateTo({
      url: 'addUnitInfo/addUnitInfo?unit_id='+this.data.declaring_unit_id,
    })
    this.setData({
      show_other_unit_view: false
    })
  },

  // 申请退税
  doRefund: function() {
    wx.navigateTo({
      url: 'refundInfo/refundInfo',
    })
  },

  // 放弃退税
  doNotRefund: function() {
    var that = this
    wx.showModal({
      title: '确认放弃退税吗?',
      content: '',
      confirmColor: '#357aeb',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.forgiveRefund("5")
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  refundDetail:function(){//退税详情
    wx.navigateTo({
      url: 'refundDetail/refundDetail?status=' + this.data.refund_status,
    })
  },

  updateText:function(data){
    var text = ""
    var text_status = ""
    var text_title = "应补退税额"
    if (data.declare_status=='2'){//申报中
      text = "请等候申报结果"
    } else {//申报成功
      if (data.declare_result=='0') {//申报结果0:退税,1:补税,2:无需补退税
        text_title = "应退税额"
        if (data.refund_status == "1") {
          text = "请确认是否申请退款?"
        } else if (data.refund_status == '2') {
          text = "申请退税中"
          text_status = "退税中"
        } else if (data.refund_status == '4') {
          text = "退税失败,请确认您的银行卡信息是否正确?"
          text_status = "退税失败"
        } else if (data.refund_status == '5') {
          text = "您已放弃退税"
          text_status = "已放弃"
        }
      } else if (data.declare_result == '1'){//补税
        text_title = "应补税额"
        if (data.exempt == 'yes') {
          text = "您已享受豁免"
          text_status = "豁免"
        } else if (data.pay_status == '1') {
          text = "请等待企业为您代缴税款"
          text_status = "待缴款"
        } else if (data.pay_status == '2') {
          text = "单位已为您代缴税款"
          text_status = "已缴款"
        }
      }

      // if (data.refund_tax > 0 || data.refund_tax == 0) {
      //   text_title = "应补税额"
      // } else {
      //   text_title = "应退税额"
      // }
    } 
    this.setData({
      declare_text_extra:text,
      uniqual_content01:text_status,
      uniqual_title01: text_title,
    })
  },

  onHide: function() {

  },


  onUnload: function() {

  },


  onPullDownRefresh: function() {

  },


  onReachBottom: function() {

  },


  onShareAppMessage: function() {

  }
})