// pages/project/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,
    showStartTime:false,
    showEndTime:false,
    ynProjcetStatusOptions:[],
    form:{
      
    },
    id:"",
    ynProjcetStatusindex:0,
    status:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id!=null&&options.id!=""){
      let that = this
      this.setData({id:options.id})
      UTIL.httpRequest(API.URL_GET_PROJECTGET + this.data.id, {method:'GET'}, {
        success: (res) => {
          this.setData({'form':res.data})

          UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'yn_projcet_status', {method:'GET'}, {
            success: (r) => {
              if(r.data.length>0){
                that.setData({
                  ynProjcetStatusOptions:r.data,
                })
                r.data.map((rr,ind) => {
                     if(rr.dictValue == res.data.ynProjcetStatus){
                       that.setData({'ynProjcetStatusindex':ind})
                     }
                 })
              }
            }
          })
        }
      })
    }else{
      let that = this
      this.setData({
        'form.startTime':this.getNewDate(new Date()),
        'form.endTime':this.getNewDate(new Date())
      });
      UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'yn_projcet_status', {method:'GET'}, {
        success: (r) => {
          if(r.data.length>0){
            that.setData({
              ynProjcetStatusOptions:r.data,
              'form.ynProjcetStatus':r.data[0].dictValue
            })
          }
        }
      })
    }
    
  },
  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});
  },
  onConfirmynProjcetStatus (e) {
    let obj =  e.detail.value;
    this.setData({
      'form.ynProjcetStatus':this.data.ynProjcetStatusOptions[obj].dictValue,
      'ynProjcetStatusindex':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
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  closeBox(even){
    this.setData({
      [even.currentTarget.dataset.name]:false
    })
  },
  openBox(even){
    this.setData({
      [even.currentTarget.dataset.name]:true
    })
  },
  onConfirmTime(event){
    this.setData({
      [event.currentTarget.dataset.name]: false,
      [event.currentTarget.dataset.value]: UTIL.formatDate(event.detail),
    });
  },
  onConfirmYn(event) {
    this.setData({
      [event.currentTarget.dataset.name]: false,
      [event.currentTarget.dataset.value]: event.detail.value.dictValue,
      [event.currentTarget.dataset.value+'Text']: event.detail.value.dictLabel,
    });
  },
  goSubmit(){
    if(this.data.form.projectName===''||this.data.form.projectName==null){ //项目名称
      UTIL.showToastNoneIcon('请填写项目名称！');
      return false;
    }else if(this.data.form.projectContractor===''||this.data.form.projectContractor==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.projectAmount === ''||this.data.form.projectAmount==null){//合共价款
      UTIL.showToastNoneIcon('请输入合共价款！');
      return false;
    }else if(this.data.form.ynProjcetStatus === ''||this.data.form.ynProjcetStatus==null){//工程状态
      UTIL.showToastNoneIcon('工程状态不能为空！');
      return false;
    }else if(this.data.status=='0'){
      this.setData({'status':1})
      var that = this ;
    let data = that.data.form;
    data.method = 'POST';
    if(that.data.form.id==""||that.data.form.id==null){
      UTIL.httpRequest(API.URL_GET_PROJECTADD, 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);
          }
          that.setData({'status':0})
        },
        complete: function (response) {
          wx.hideNavigationBarLoading();
        }
      })
    }else{
      UTIL.httpRequest(API.URL_POST_PROJECTUPDATE, 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();
        }
      })
    }
    
    }
    
  },
  onChange(event){
    this.setData({
      [event.currentTarget.dataset.value]: event.detail,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})