// pages/contract/add/add.js
import * as UTIL from '../../../utils/util.js';
import * as API from '../../../utils/API.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    id:null,
    form:{},
    assetTypeindex:'0',
    changeTypeindex:'0',
    depreciationTypeindex:'0',
    changeValueindex:'0',
   // 折旧方式字典
    depreciationTypeOptions:[],
   // 资产类别字典
   assetTypeOptions: [],
   // 变更类型字典
   changeTypeOptions: [
     { 'dictLabel':'数量/面积变更','dictValue':'1'},
     { 'dictLabel':'原值变更','dictValue':'2'},
     { 'dictLabel':'预计使用年数变更','dictValue':'3'},
     { 'dictLabel':'累计折旧变更','dictValue':'4'},
     { 'dictLabel':'已折旧年数变更','dictValue':'5'},
     { 'dictLabel':'残值率变更','dictValue':'6'},
     { 'dictLabel':'折旧方式变更','dictValue':'7'},
   ],
   status:0,
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  openBox(even){
    this.setData({
      [even.currentTarget.dataset.name]:true
    })
  },
  onChange(event){
    this.setData({
      [event.currentTarget.dataset.formname]: event.detail,
    })
  },
  closeBox(even){
    this.setData({
      [even.currentTarget.dataset.name]:false
    })
  },
  onConfirmChangeType (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.changeType':this.data.changeTypeOptions[obj].dictValue,
      'changeTypeindex':obj
    })
  },
  onConfirmChangeValue(e){
    let obj =  e.detail.value;
    this.setData({
      'form.changeValue':this.data.depreciationTypeOptions[obj].dictValue,
      'changeValueindex':obj
    })
  },
  onConfirmDepreciationType(e){
    let obj =  e.detail.value;
    this.setData({
      'form.depreciationType':this.data.depreciationTypeOptions[obj].dictValue,
      'depreciationTypeindex':obj
    })
  },
  onConfirmAssetType (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.assetType':this.data.assetTypeOptions[obj].dictValue,
      'assetTypeindex':obj
    })
  },
  getNewDate(date){
    //date是传过来的时间戳，注意需为13位，10位需*1000
    //也可以不传,获取的就是当前时间
    var time
    if(date){
       time = new Date(date);
    }else{
       time = new Date();
    }
    var year= time.getFullYear()  //年
    var month = ("0" + (time.getMonth() + 1)).slice(-2); //月
    var day = ("0" + time.getDate()).slice(-2); //日
    var mydate = year + "-" + month + "-" + day;
    return mydate
  },
  goSubmit:function(){
    if(this.data.changeTypeindex != 6&&(this.data.form.changeValue===''||this.data.form.changeValue==null)){ //变更后值
      UTIL.showToastNoneIcon('变更后值不能给你为空！');
      return false;
    }else if(this.data.status == '0'){
      let that = this
      this.setData({'status':'1'})
        let data = this.data.form;
        data.method = "POST";
        if(this.data.changeTypeindex == 0){
          data.quantity = data.changeValue
          UTIL.httpRequest(API.URL_POST_UPDATEQUANTITY,data,{
            success: (res) => {
              that.setData({'status':'0'})
             if(res.code == 200){
              UTIL.showToastNoneIcon("变更成功");
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon("变更失败："+res.msg);
            }
            },
            fail: function (response) {
              if (typeof fail === FUNCTION_TEXT) {
                fail(handleFail(response));
              } else {
                showToastNoneIcon(API.MSG_FAIL_HTTP);
              }
              that.setData({'status':0})
            },
            complete: function (response) {
              wx.hideNavigationBarLoading();
            }
          })
        }else if(this.data.changeTypeindex == 1){
          data.originalValue = data.changeValue
          UTIL.httpRequest(API.URL_POST_UPDATEORIGINALVALUE,data,{
            success: (res) => {
              that.setData({'status':'0'})
             if(res.code == 200){
              UTIL.showToastNoneIcon("变更成功");
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon("变更失败："+res.msg);
            }
            },
            fail: function (response) {
              if (typeof fail === FUNCTION_TEXT) {
                fail(handleFail(response));
              } else {
                showToastNoneIcon(API.MSG_FAIL_HTTP);
              }
              that.setData({'status':0})
            },
            complete: function (response) {
              wx.hideNavigationBarLoading();
            }
          })
        }else if(this.data.changeTypeindex == 2){
          data.expectedYears = data.changeValue
          UTIL.httpRequest(API.URL_POST_UPDATEEXPECTEDYEARS,data,{
            success: (res) => {
              that.setData({'status':'0'})
             if(res.code == 200){
              UTIL.showToastNoneIcon("变更成功");
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon("变更失败："+res.msg);
            }
            },
            fail: function (response) {
              if (typeof fail === FUNCTION_TEXT) {
                fail(handleFail(response));
              } else {
                showToastNoneIcon(API.MSG_FAIL_HTTP);
              }
              that.setData({'status':0})
            },
            complete: function (response) {
              wx.hideNavigationBarLoading();
            }
          })
        }else if(this.data.changeTypeindex == 3){
          data.depreciationValue = data.changeValue
          UTIL.httpRequest(API.URL_POST_UPDATEDEPRECIATIONVALUE,data,{
            success: (res) => {
              that.setData({'status':'0'})
             if(res.code == 200){
              UTIL.showToastNoneIcon("变更成功");
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon("变更失败："+res.msg);
            }
            },
            fail: function (response) {
              if (typeof fail === FUNCTION_TEXT) {
                fail(handleFail(response));
              } else {
                showToastNoneIcon(API.MSG_FAIL_HTTP);
              }
              that.setData({'status':0})
            },
            complete: function (response) {
              wx.hideNavigationBarLoading();
            }
          })
        }else if(this.data.changeTypeindex == 4){
          data.depreciationYears = data.changeValue
          UTIL.httpRequest(API.URL_POST_UPDATEDEPRECIATIONYEARS,data,{
            success: (res) => {
              that.setData({'status':'0'})
             if(res.code == 200){
              UTIL.showToastNoneIcon("变更成功");
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon("变更失败："+res.msg);
            }
            },
            fail: function (response) {
              if (typeof fail === FUNCTION_TEXT) {
                fail(handleFail(response));
              } else {
                showToastNoneIcon(API.MSG_FAIL_HTTP);
              }
              that.setData({'status':0})
            },
            complete: function (response) {
              wx.hideNavigationBarLoading();
            }
          })
        }else if(this.data.changeTypeindex == 5){
          data.residualsRate = data.changeValue
          UTIL.httpRequest(API.URL_POST_UPDATERESIDUALSRATE,data,{
            success: (res) => {
              that.setData({'status':'0'})
             if(res.code == 200){
              UTIL.showToastNoneIcon("变更成功");
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon("变更失败："+res.msg);
            }
            },
            fail: function (response) {
              if (typeof fail === FUNCTION_TEXT) {
                fail(handleFail(response));
              } else {
                showToastNoneIcon(API.MSG_FAIL_HTTP);
              }
              that.setData({'status':0})
            },
            complete: function (response) {
              wx.hideNavigationBarLoading();
            }
          })
        }else if(this.data.changeTypeindex == 6){
          data.depreciationType = this.data.depreciationTypeOptions[this.data.changeValueindex].dictValue
          UTIL.httpRequest(API.URL_POST_UPDATEDEPRECIATIONMETHOD,data,{
            success: (res) => {
              that.setData({'status':'0'})
             if(res.code == 200){
              UTIL.showToastNoneIcon("变更成功");
              setTimeout(function(){
                wx.navigateBack({
                  delta:1
                })
              },2000)
            }else{
              UTIL.showToastNoneIcon("变更失败："+res.msg);
            }
            },
            fail: function (response) {
              if (typeof fail === FUNCTION_TEXT) {
                fail(handleFail(response));
              } else {
                showToastNoneIcon(API.MSG_FAIL_HTTP);
              }
              that.setData({'status':0})
            },
            complete: function (response) {
              wx.hideNavigationBarLoading();
            }
          })
        }
        
      
    }
  },
  bindNewInput: function (e) {
    this.setData({
      [e.currentTarget.dataset.name]: e.detail.value
    })
    if(e.currentTarget.dataset.name == 'form.originalValue' ){
      this.setData({
        'form.netSalvage' : (this.data.form.residualsRate*e.detail.value/100).toFixed(2),
        'form.perYearDepreciationValue' : ((e.detail.value-(this.data.form.residualsRate*e.detail.value/100))/this.data.form.expectedYears).toFixed(2),
        'form.netValue':this.data.form.originalValue-this.data.form.depreciationValue
      })
    }
      if(e.currentTarget.dataset.name == 'form.residualsRate' &&e.detail.value<=100){
        this.setData({
          'form.netSalvage' : (this.data.form.originalValue*e.detail.value/100)<this.data.form.originalValue?(this.data.form.originalValue*e.detail.value/100).toFixed(2):this.data.form.originalValue,
        })
        this.setData({
          'form.perYearDepreciationValue' : ((this.data.form.originalValue-this.data.form.netSalvage)/this.data.form.expectedYears).toFixed(2),
        })
      }
      if(e.currentTarget.dataset.name == 'form.residualsRate' &&e.detail.value>100){
        this.setData({
          'form.residualsRate' : 5,
        })
      }
      if(e.currentTarget.dataset.name == 'form.netSalvage' ){
        this.setData({
          'form.perYearDepreciationValue' : ((this.data.form.originalValue-e.detail.value)/this.data.form.expectedYears).toFixed(2)
        })
      }
      if(e.currentTarget.dataset.name == 'form.expectedYears' ){
        this.setData({
          'form.perYearDepreciationValue' : ((this.data.form.originalValue-this.data.form.netSalvage)/e.detail.value).toFixed(2)
        })
      }
      if(e.currentTarget.dataset.name == 'form.depreciationYears' ){
        this.setData({
          'form.depreciationValue' : (this.data.form.perYearDepreciationValue*e.detail.value)>this.data.form.originalValue?this.data.form.originalValue:(this.data.form.perYearDepreciationValue*e.detail.value).toFixed(2),
          'form.netValue' : (this.data.form.originalValue-(this.data.form.perYearDepreciationValue*e.detail.value))>=0?(this.data.form.originalValue-(this.data.form.perYearDepreciationValue*e.detail.value).toFixed(2)):0
        })
      }
    
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id!=null&&options.id!=""){
      this.setData({id:options.id})
      //获取收入合同状态
      UTIL.httpRequest(API.URL_GET_PERMANENTDETAIL + this.data.id, {method:'GET'}, {
        success: (res) => {
          this.setData({'form':res.data});
          let that = this;
          //获取资产类别
           UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'asset_type', {method:'GET'}, {
             success: (r) => {
               if(r.data.length>0){
                 that.setData({
                    assetTypeOptions:r.data,
                 })
                 r.data.map((rr,ind) => {
                     if(rr.dictValue == res.data.assetType){
                        that.setData({'assetTypeindex':ind})
                     }
                 })
               }
             }
           })
           //获取经营属性
           UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'operation_type', {method:'GET'}, {
             success: (r) => {
               if(r.data.length>0){
                 that.setData({
                    operationTypeOptions:r.data,
                 })
                 r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.operationType){
                       that.setData({'operationTypeindex':ind})
                    }
                })
               }
             }
           })
            //获取增加方式
            UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'add_type', {method:'GET'}, {
             success: (r) => {
               if(r.data.length>0){
                 that.setData({
                    addTypeOptions:r.data,
                 })
                 r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.addType){
                       that.setData({'addTypeindex':ind})
                    }
                })
               }
             }
           })
           //获取使用情况
           UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'use_type', {method:'GET'}, {
             success: (r) => {
               if(r.data.length>0){
                 that.setData({
                    useTypeOptions:r.data,
                 })
                 r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.useType){
                       that.setData({'useTypeindex':ind})
                    }
                })
               }
             }
           })
             //获取折旧方式
             UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'depreciation_type', {method:'GET'}, {
               success: (r) => {
                 if(r.data.length>0){
                   that.setData({
                    depreciationTypeOptions:r.data,
                   })
                   r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.depreciationType){
                       that.setData({'depreciationTypeindex':ind})
                    }
                })
                 }
               }
             })
              //获取资产状态
              UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'asset_status', {method:'GET'}, {
               success: (r) => {
                 if(r.data.length>0){
                   that.setData({
                    assetStatusOptions:r.data,
                   })
                   r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.assetStatus){
                       that.setData({'assetStatusindex':ind})
                    }
                })
                 }
               }
             })
              //获取单位
              UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'fixed_assets_unit', {method:'GET'}, {
               success: (r) => {
                 if(r.data.length>0){
                   that.setData({
                    unitOptions:r.data,
                   })
                   r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.unit){
                       that.setData({'unitindex':ind})
                    }
                })
                 }
               }
             })
             //获取系统是否
             UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'sys_yes_no', {method:'GET'}, {
                success: (r) => {
                  if(r.data.length>0){
                    that.setData({
                        sysYesNoOptions:r.data,
                    })
                    r.data.map((rr,ind) => {
                     if(rr.dictValue == res.data.isMin){
                        that.setData({'isMinindex':ind})
                     }
                     if(rr.dictValue == res.data.isFormAsset){
                        that.setData({'isFormAssetindex':ind})
                     }
                 })
                  }
                }
              })
        }
      })
    }else{
      let that = this;
      //获取资产类别
       UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'asset_type', {method:'GET'}, {
         success: (res) => {
           if(res.data.length>0){
             that.setData({
               assetTypeOptions:res.data,
               'form.assetType':res.data[0].dictValue
             })
           }
         }
       })
       //获取经营属性
       UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'operation_type', {method:'GET'}, {
         success: (res) => {
           if(res.data.length>0){
             that.setData({
                operationTypeOptions:res.data,
               'form.operationType':res.data[0].dictValue
             })
           }
         }
       })
        //获取增加方式
        UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'add_type', {method:'GET'}, {
         success: (res) => {
           if(res.data.length>0){
             that.setData({
                addTypeOptions:res.data,
               'form.addType':res.data[0].dictValue
             })
           }
         }
       })
       //获取使用情况
       UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'use_type', {method:'GET'}, {
         success: (res) => {
           if(res.data.length>0){
             that.setData({
                useTypeOptions:res.data,
               'form.useType':res.data[0].dictValue
             })
           }
         }
       })
         //获取折旧方式
         UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'depreciation_type', {method:'GET'}, {
           success: (res) => {
             if(res.data.length>0){
               that.setData({
                depreciationTypeOptions:res.data,
                 'form.depreciationType':res.data[1].dictValue
               })
             }
           }
         })
          //获取资产状态
          UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'asset_status', {method:'GET'}, {
           success: (res) => {
             if(res.data.length>0){
               that.setData({
                assetStatusOptions:res.data,
                 'form.assetStatus':res.data[0].dictValue
               })
             }
           }
         })
          //获取单位
          UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'fixed_assets_unit', {method:'GET'}, {
           success: (res) => {
             if(res.data.length>0){
               that.setData({
                unitOptions:res.data,
                 'form.unit':res.data[0].dictValue
               })
             }
           }
         })
          //获取是否
          UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'sys_yes_no', {method:'GET'}, {
            success: (res) => {
              if(res.data.length>0){
                that.setData({
                    sysYesNoOptions:res.data,
                  'form.isMin':res.data[0].dictValue,
                  'form.isFormAsset':res.data[0].dictValue
                })
              }
            }
          })
    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})