var util = require('../../../utils/util.js')
const app = getApp();
Page({

  data: {
    handlerNameList: [
      // {id: 11, name: "海文牧场"}, {id: 22, name: "成宏牧场"}, {id: 32, name: "瑞福牧业"}
    ],
    handlerIndex:'',
    handlerId:'',

    id: '', 
   

    rules: {
      handlerName: [{ required: true, trigger: 'blur' }],
      hdate: [{ required: true, message: '日期必填', trigger: 'blur' }],
      content: [{ required: true, message: '方式必填', trigger: 'blur' }] 
    },
    // 新增/编辑
    create: {
        dialogStatus: '',
        temp: {
           handlerId: '', 
           handlerName: '', 
           hdate: util.getToday(), 
           content: '',
           remark: '', 
           created: util.getToday(), 
           receiverChoose: 'true' 
        },

        
    }
  },

  onLoad: function (options) {
    // 表单初始化
    wx.lin.initValidateForm(this)

    var id = options.id;
    this.setData({
      id: id
    })
    console.log(id)
    //处理人
    util.getDataByName({name: 'getEmployeesSelect',parammaps: {}}, this.getPickerList1)
  },
 
  getPickerList1:  function (res) {
    console.log('处理人下拉数据======>', res.data.list)
    this.setData({
      handlerNameList: res.data.list
    })
  },
  
  //下拉框：客户
  change_handler:function(e){
     var that = this
     console.log('change_handler的选项', e.detail.value)
     var newId = this.data.handlerNameList[e.detail.value]['id']
      this.setData({
        handlerIndex: e.detail.value,
        handlerId: newId,
        ['create.temp.handlerId']: newId,
      })
      

  },
  //日期选择触发
  on_hdate_tap:function(e){
    this.setData({
      ['create.temp.hdate']:e.detail.value
    })
  },
    
  
  //保存
  submit:function(e){
    
    var detail_value = e.detail.values
    var detail_errors = e.detail.errors
    var detail_errors = e.detail.errors
    console.log("detail_value",detail_value)
    console.log("create.temp",this.data.create.temp)


    const submit_data = Object.assign({}, this.data.create.temp, detail_value)
   
    console.log("保存发送：",submit_data)

    submit_data.cid = this.data.id
 
 

    var send_data = {
      "common": { "returnmap": "0" },
      "data": [
        {                
          "name": "insertHandle", "type": "e",
          "parammaps": submit_data
        },
        {                
          "name": "updateComplaintStatus", "type": "e",
          "parammaps": {
             "cid": this.data.id 
          }              
        }]          
      }

       //验证必填项是否为空
    if(submit_data.handlerId == ""){
      wx.showToast({title: '处理人必填',icon: 'none',duration: 2000})
    } else if(submit_data.hdate == ""){
      wx.showToast({title: '处理日期必填',icon: 'none',duration: 2000})
    } else if(submit_data.content == ""){
      wx.showToast({title: '处理方式必填',icon: 'none',duration: 2000})
 
    } else {
      if( wx.getStorageSync('isLoading')  == "true"){
        wx.showToast({
          title: '正在保存中，请稍等！',
          icon: 'none',
          duration: 2000
        })
      } else {
        util.execDataByConfig(send_data, this.getListSuccess)
      }
    }

 
  },

  getListSuccess:  function (res) {
    console.log('table数据======>', res)
    if (res.msg !== 'fail') {
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 2000,
        success: function(){
          wx.redirectTo({ url:"../complaintRecord/complaintRecord"})
        }
      })
    } else {
      wx.showToast({
        title: '保存失败'+res.data,
        icon: 'error',
        duration: 2000,
      })
    }
  },

 
  
  onReady: function () {

  },

  
  onShow: function () {

  },

 
  onHide: function () {

  },

   
  onUnload: function () {

  },

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

  },

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

  },

 
  onShareAppMessage: function () {

  }
})