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

  data: {
    customerId:"",
 
    rules: {
      contactName: [{ required: true, message: '联系人必填', trigger: 'blur' }],
      telephone: [{ required: true, message: '联系人电话必填', trigger: 'blur' }],
 
    },
    // 新增/编辑
    create: {
        dialogStatus: '',
        temp: {
           contactName: '', 
           telephone: '', 
           address: '', 
           remark: '', 
           customerId:"",
           created: util.getToday(), 
   
        },
        
    }
  },

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

    this.setData({
      "customerId":id,
      ['create.temp.customerId']: id
 
    })
 
  
  },
  
   
  //保存
  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)


    var send_data = {
      "name":   "insertContacts" ,
      "parammaps":  submit_data
    }


     //验证必填项是否为空
     if(submit_data.contactName == ""){
      wx.showToast({title: '联系人必填',icon: 'none',duration: 2000})
    } else if(submit_data.telephone == ""){
      wx.showToast({title: '联系人电话必填',icon: 'none',duration: 2000})
    } else {
      if( wx.getStorageSync('isLoading')  == "true"){
        wx.showToast({
          title: '正在保存中，请稍等！',
          icon: 'none',
          duration: 2000
        })
      } else {
        util.postDataByName(send_data, this.getListSuccess)
      }
    }


   
  },

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

 
  
  onReady: function () {

  },

  
  onShow: function () {

  },

 
  onHide: function () {

  },

   
  onUnload: function () {

  },

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

  },

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

  },

 
  onShareAppMessage: function () {

  }
})