// 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:{
        reviewTime:'2022-01-01',
        buildingTime:'2022-01-01',
        startTime:'2022-01-01',
        endTime:'2022-01-01',
        biddingDate:'2022-01-01',
    },
    assetTypeindex:'0',
    settleTypeindex:'0',
    collectionPayindex:'0',
    contractionSourceindex:'0',
    contractionStatusindex:'0',
    biddingWayindex:'0',
    biddingTypeindex:'0',
    showBiddingDate:false,
    minDate: new Date(2008, 5, 1).getTime(),
    maxDate: new Date(2040, 0, 31).getTime(),
    // 合同类型字典
    assetTypeOptions: [],
    // 结款方式字典
    settleTypeOptions: [],
    // 收付款类型字典
    collectionPayOptions: [],
    // 合同来源字典
    contractionSourceOptions: [],
    // 合同状态字典
    contractionStatusOptions: [],
    // 招标方式字典
    biddingWayOptions: [],
    // 招标类型字典
    biddingTypeOptions: [],

    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
    })
  },
  onConfirmBiddingDate(e){
    let data = this.getNewDate(new Date(e.detail.value));
    this.setData({'form.biddingDate':data});
  },
  onConfirmReviewTime(e){
    let data = this.getNewDate(new Date(e.detail.value));
    this.setData({'form.reviewTime':data});
  },
  onConfirmBuildingTime(e){
    let data = this.getNewDate(new Date(e.detail.value));
    this.setData({'form.buildingTime':data});
  },
  onConfirmStartTime(e){
    let data = this.getNewDate(new Date(e.detail.value));
    this.setData({'form.startTime':data});
  },
  onConfirmEndTime(e){
    let data = this.getNewDate(new Date(e.detail.value));
    this.setData({'form.endTime':data});
  },
  onConfirmbiddingType (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.biddingType':this.data.biddingTypeOptions[obj].dictValue,
      'biddingTypeindex':obj
    })
  },
  onConfirmShowBiddingWay (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.biddingWay':this.data.biddingWayOptions[obj].dictValue,
      'biddingWayindex':obj
    })
  },
  onConfirmSettleType (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.settleType':this.data.settleTypeOptions[obj].dictValue,
      'settleTypeindex':obj
    })
  },
  onConfirmCollectionPay (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.collectionPay':this.data.collectionPayOptions[obj].dictValue,
      'collectionPayindex':obj
    })
  },
  onConfirmContractionSource (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.contractionSource':this.data.contractionSourceOptions[obj].dictValue,
      'contractionSourceindex':obj
    })
  },
  onConfirmAssetType (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.assetType':this.data.assetTypeOptions[obj].dictValue,
      'assetTypeindex':obj
    })
  },
  onConfirmContractionStatus (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.contractionStatus':this.data.contractionStatusOptions[obj].dictValue,
      'contractionStatusindex':obj
    })
  },
  onConfirmBiddingWay (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.biddingWay':this.data.biddingWayOptions[obj].dictValue,
      'biddingWayindex':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.form.code===''||this.data.form.code==null){ //合同编码
      UTIL.showToastNoneIcon('请填写合同编码！');
      return false;
    }else if(this.data.form.name===''||this.data.form.name==null){  //合同名称
      UTIL.showToastNoneIcon('请输入合同名称！');
      return false;
    }else if(this.data.form.firstParty===''||this.data.form.firstParty==null){  //甲方
      UTIL.showToastNoneIcon('请输入甲方名称！');
      return false;
    }else if(this.data.form.secondParty===''||this.data.form.secondParty==null){  //乙方
      UTIL.showToastNoneIcon('请输入乙方名称！');
      return false;
    }else if(this.data.form.collectionPay === ''||this.data.form.collectionPay==null){//收付款类型
      UTIL.showToastNoneIcon('收付款类型不能为空！');
      return false;
    }else if(this.data.form.contractionSource === ''||this.data.form.contractionSource==null){//合同来源
      UTIL.showToastNoneIcon('合同来源不能为空！');
      return false;
    }else if(this.data.form.totalAmount === ''||this.data.form.totalAmount==null){//合同金额
      UTIL.showToastNoneIcon('合同金额不能为空！');
      return false;
    }else if(this.data.form.buildingTime === ''||this.data.form.buildingTime==null){//签订日期
      UTIL.showToastNoneIcon('签订日期不能为空！');
      return false;
    }else if(this.data.form.startTime === ''||this.data.form.startTime==null){//开始日期
      UTIL.showToastNoneIcon('开始日期不能为空！');
      return false;
    }else if(this.data.form.endTime === ''||this.data.form.endTime==null){//结束日期
      UTIL.showToastNoneIcon('结束日期不能为空！');
      return false;
    }else if(this.data.form.price === ''||this.data.form.price==null){//单价
      UTIL.showToastNoneIcon('单价不能为空！');
      return false;
    }else if(this.data.form.receivedAmount === ''||this.data.form.receivedAmount==null){//已结款
      UTIL.showToastNoneIcon('已结款不能为空！');
      return false;
    }else if(this.data.form.contractionStatus === ''||this.data.form.contractionStatus==null){//合同状态
      UTIL.showToastNoneIcon('合同状态不能为空！');
      return false;
    }else if(this.data.status=='0'){
      this.setData({'status':1})
      if(this.data.form.id!=null&&this.data.form.id!=""){
        let data = this.data.form;
        data.method = "POST";
        UTIL.httpRequest(API.URL_POST_CONTRACTUPDATE,data,{
          success: (res) => {
            this.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);
            }
            this.setData({'status':0})
          },
          complete: function (response) {
            wx.hideNavigationBarLoading();
          }
        })
      }else{
        let data = this.data.form;
        data.method = "POST";
        UTIL.httpRequest(API.URL_POST_CONTRACTADD,data,{
          success: (res) => {
            this.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);
            }
          },
          complete: function (response) {
            wx.hideNavigationBarLoading();
          }
        })
      }
    }
  },
  bindNewInput: function (e) {
    this.setData({
      [e.currentTarget.dataset.name]: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id!=null&&options.id!=""){
      this.setData({id:options.id})
      //获取收入合同状态
      UTIL.httpRequest(API.URL_GET_CONTRACTDETAIL + this.data.id, {method:'GET'}, {
        success: (res) => {
          this.setData({'form':res.data});
          let that = this;
          //获取收入合同状态
           UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'type_of_contract', {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 + 'contraction_source', {method:'GET'}, {
             success: (r) => {
               if(r.data.length>0){
                 that.setData({
                   contractionSourceOptions:r.data,
                 })
                 r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.contractionSource){
                       that.setData({'contractionSourceindex':ind})
                    }
                })
               }
             }
           })
            //获取收入收付款类型
            UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'collection_pay', {method:'GET'}, {
             success: (r) => {
               if(r.data.length>0){
                 that.setData({
                   collectionPayOptions:r.data,
                 })
                 r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.collectionPay){
                       that.setData({'collectionPayindex':ind})
                    }
                })
               }
             }
           })
           //获取收入结款方式
           UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'settle_type', {method:'GET'}, {
             success: (r) => {
               if(r.data.length>0){
                 that.setData({
                   settleTypeOptions:r.data,
                 })
                 r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.settleType){
                       that.setData({'settleTypeindex':ind})
                    }
                })
               }
             }
           })
             //获取收入合同状态
             UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'contraction_status', {method:'GET'}, {
               success: (r) => {
                 if(r.data.length>0){
                   that.setData({
                    contractionStatusOptions:r.data,
                   })
                   r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.contractionStatus){
                       that.setData({'contractionStatusindex':ind})
                    }
                })
                 }
               }
             })
              //获取收入招标方式
              UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bidding_way', {method:'GET'}, {
               success: (r) => {
                 if(r.data.length>0){
                   that.setData({
                     biddingWayOptions:r.data,
                   })
                   r.data.map((rr,ind) => {
                    if(rr.dictValue == res.data.biddingWay){
                       that.setData({'biddingWayindex':ind})
                    }
                })
                 }
               }
             })
              //获取收入招标类型
              UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bidding_types', {method:'GET'}, {
               success: (r) => {
                 if(r.data.length>0){
                   that.setData({
                     biddingTypeOptions:r.data,
                   })
                   r.data.map((rr,ind) => {
                        if(rr.dictValue == res.data.biddingType){
                          that.setData({'biddingTypeindex':ind})
                        }
                    })
                 }
               }
             })
        }
      })
    }else{
      let that = this;
      this.setData({
        'form.reviewTime':this.getNewDate(),
        'form.buildingTime':this.getNewDate(),
        'form.startTime':this.getNewDate(),
        'form.endTime':this.getNewDate(),
        'form.biddingDate':this.getNewDate(),
      })
      //获取收入合同状态
       UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'type_of_contract', {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 + 'contraction_source', {method:'GET'}, {
         success: (res) => {
           if(res.data.length>0){
             that.setData({
               contractionSourceOptions:res.data,
               'form.contractionSource':res.data[0].dictValue
             })
           }
         }
       })
        //获取收入收付款类型
        UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'collection_pay', {method:'GET'}, {
         success: (res) => {
           if(res.data.length>0){
             that.setData({
               collectionPayOptions:res.data,
               'form.collectionPay':res.data[0].dictValue
             })
           }
         }
       })
       //获取收入结款方式
       UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'settle_type', {method:'GET'}, {
         success: (res) => {
           if(res.data.length>0){
             that.setData({
               settleTypeOptions:res.data,
               'form.settleType':res.data[0].dictValue
             })
           }
         }
       })
         //获取收入合同状态
         UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'contraction_status', {method:'GET'}, {
           success: (res) => {
             if(res.data.length>0){
               that.setData({
                contractionStatusOptions:res.data,
                 'form.contractionStatus':res.data[0].dictValue
               })
             }
           }
         })
          //获取收入招标方式
          UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bidding_way', {method:'GET'}, {
           success: (res) => {
             if(res.data.length>0){
               that.setData({
                 biddingWayOptions:res.data,
                 'form.biddingWay':res.data[0].dictValue
               })
             }
           }
         })
          //获取收入招标类型
          UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bidding_types', {method:'GET'}, {
           success: (res) => {
             if(res.data.length>0){
               that.setData({
                 biddingTypeOptions:res.data,
                 'form.biddingType':res.data[0].dictValue
               })
             }
           }
         })
    }
   
  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})