// 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" //申报成功
    refund_status: "", //退税状态  "0" 无需退税; "1" 待退税;"2" 退税中;"3" 退税成功;"4"退税失败;"5"放弃退税
    pay_status: "", //补税状态    "0" 无需补税;  "1" 待补税;"2" 补税成功
    declaring_unit_status: "", //1:本单位申报,2:本单位其他申报,3:其他单位申报,4:待确认申报//用户自行申报
    payinfo: {},
    unitInfo: [],
    showUnit: false,
    declaring_unit_id: "",
    showModal_selfDeclare: false,
    uniqual_title01:"",
    new_refund_tax:0,
    declare_end_date:"",
    declare_end_days: 0,
    show_other_unit_view:false,
  },


  onLoad: function(options) {
    this.getPayInfo();
  },


  onReady: function() {

  },


  onShow: function() {

  },

  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)
        var text01="应补退税额"
        if (result.statusCode == 200) {
          if (that.data.declare_status=='1'){//申报成功
            if (result.data.refund_tax > 0 || result.data.refund_tax == 0){
              text01="应补税额"
            } else {
              text01 = "应退税额"
            }  
          }
          that.setData({
            declare_status: result.data.declare_status,
            refund_status: result.data.refund_status,
            pay_status: result.data.pay_status,
            declaring_unit_status: result.data.declaring_unit_status,
            new_refund_tax: Math.abs(result.data.refund_tax),
            uniqual_title01:text01,
            declare_end_date: format.yearFormString(result.data.declare_end_time) + "年" + format.monthFormString(result.data.declare_end_time) + "月" + format.dayFormString(result.data.declare_end_time) + "日",
            declare_end_days: format.daysTillNow(Date.parse(new Date())/1000,result.data.declare_end_time),
            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': ''
      },
      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() {
    this.getUnitInfo()
  },

  declareNotHere: function() {
    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)
    this.setData({
      declaring_unit_id: e.currentTarget.id,
      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(e.currentTarget.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() {
    wx.showModal({
      title: '确认放弃退税吗?',
      content: '',
      confirmColor: '#357aeb',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          this.forgiveRefund("5")
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  onHide: function() {

  },


  onUnload: function() {

  },


  onPullDownRefresh: function() {

  },


  onReachBottom: function() {

  },


  onShareAppMessage: function() {

  }
})