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

  data: {
    allDisabled:true,//有客户下拉时临时用来控制其他下拉不显示
    c_txt_Show:false,
    customerShow:false,
    customerNameList: [
      // {id: 11, name: "海文牧场"}, {id: 22, name: "成宏牧场"}, {id: 32, name: "瑞福牧业"}
    ],
    customerIndex:'',
    customerId:'',
    receiverNameList: [
      // {id: 56, name: "苗舒怡"},{id: 57, name: "颛孙洋洋"},{id: 58, name: "李涛"}
    ],
    receiverIndex:'',
    receiverId:'',
    visitNameList: [
      // {id: 222, name: "拜访"}, {id: 223, name: "远程服务"}, {id: 224, name: "现场服务"}
    ],
    visitIndex:'',
    visitId:'',
    telvisitNameList: [
      // {id: 222, name: "拜访"}, {id: 223, name: "远程服务"}, {id: 224, name: "现场服务"}
    ],
    telvisitIndex:'',
    telvisitId:'',
    groupId: '', 
    groupName: '',

    receiverShow:false,
    r_txt_Show:false,
    receiverChoose:'false',
    telephone:"",
   
 
    rules: {
      customerName: [{ required: true, message: '客户必填', trigger: 'blur' }],
      rdate: [{ required: true, message: '拜访日期必填', trigger: 'blur' }],
      // visiterId: [{ required: true, message: '必填', trigger: 'blur' }],
      receiverName: [{ required: true, message: '客户接待人必填', trigger: 'blur' }],
      content: [{  required: true, message: '情况说明必填', trigger: 'blur' }],
    },
    // 新增/编辑
    create: {
        dialogStatus: '',
        temp: {
           customerId: '', 
           customerName: '', 
           groupId: '', 
           groupName: '', 
           rdate: util.getToday(), 
           employeeId: '', 
           employeeName: '', 
           receiverId: '', 
           receiverId2: '@insertContactsNew.LastInsertId', 
           receiverName: '', 
           remark: '', 
           content: '', 
           visitId: '', 
           visitName: '', 
           telvisitId: '', 
           telvisitName: '',
           created: util.getToday(), 
           receiverChoose: 'true' 
        },
        
    }
  },

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

    console.log(app.globalData.g_lgName)

    this.setData({
      ['create.temp.employeeId']: app.globalData.g_lgNameId,
      ['create.temp.employeeName']: app.globalData.g_lgName
    })

    //客户下拉
    //util.getDataByName({ name:'getCustomerSelect',parammaps: {} }, this.getPickerList1)
    
  },
  getPickerList1:  function (res) {
    console.log('客户下拉数据======>', res.data.list)
    var customerNameList = res.data.list
    this.setData({
      customerShow:true,
      allDisabled:false,
      customerNameList: customerNameList
    })
    if(customerNameList !== null){
      this.setData({c_txt_Show:false})
    } else {
      this.setData({c_txt_Show:true})
    }
  },
  // getPickerList2:  function (res) {
  //   console.log('接待人下拉数据======>', res.data.list)
  //   this.setData({
  //     receiverNameList: res.data.list
  //   })
  // },
  getPickerList3:  function (res) {
    console.log('拜访下拉数据======>', res.data.list)
    if(res.data.list !== null) {
      this.setData({
        visitNameList: res.data.list
      })
    } else {
      this.setData({
        visitNameList: [{id: "", name: "无数据"}]
      })
    }
    
  },
  getPickerList4:  function (res) {
    console.log('回访下拉数据======>', res.data.list)
    this.setData({
      telvisitNameList: res.data.list
    })
  },

  //客户文本框输入
  change_customer_input:function(e){
    var value =e.detail.value
    console.log(value)
    //客户列表框
    util.getDataByName({ name:'getCustomerNameFuzzy',parammaps: {"inputvalue":value} }, this.getPickerList1)
  },

  //客户列表点击
  on_cust_tap: function (e) {
    console.log("客户列表点击")
    var newId = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var that = this
 
    this.setData({
      customerShow:false,
      allDisabled:true,
      customerId: newId,
      ['create.temp.customerName']: name,
      ['create.temp.customerId']: newId,
    })
    //获取集团
    util.getDataByName({name: 'getgroupNameById',returntype: 'Map',parammaps: { id: newId }
    }, function(e){
      var groupId = e.data.list[0].groupId
      var groupName = e.data.list[0].groupName
      that.setData({
        ['create.temp.groupName']: groupName,
        ['create.temp.groupId']: groupId,
        groupName: groupName,
        groupId: groupId
      })
    })
    //获取接待人
    //util.getDataByName({name: 'getContacts',returntype: 'Map',parammaps: { customerId: newId }}, this.getPickerList2)
    //获取拜访记录
    util.getDataByName({name: 'getVisitByCustomerId',returntype: 'Map',parammaps: { customerId: newId }}, this.getPickerList3)
    //获取回访记录
    util.getDataByName({name: 'getTelvisitByCustomerId',returntype: 'Map',parammaps: { customerId: newId }}, this.getPickerList4)
  },
  //下拉框：客户
  // change_customer:function(e){
  //    var that = this
  //    console.log('customer的选项', e.detail.value)
  //    var newId = this.data.customerNameList[e.detail.value]['id']
  //     this.setData({
  //       customerIndex: e.detail.value,
  //       customerId: newId,
  //       ['create.temp.customerId']: newId,
  //     })
  //     //获取集团
  //     util.getDataByName({name: 'getgroupNameById',returntype: 'Map',parammaps: { id: newId }
  //     }, function(e){
  //       var groupId = e.data.list[0].groupId
  //       var groupName = e.data.list[0].groupName
  //       that.setData({
  //         ['create.temp.groupName']: groupName,
  //         ['create.temp.groupId']: groupId,
  //         groupName: groupName,
  //         groupId: groupId
  //       })
  //     })
  //     //获取接待人
  //     util.getDataByName({name: 'getContacts',returntype: 'Map',parammaps: { customerId: newId }}, this.getPickerList2)
  //     //获取拜访记录
  //     util.getDataByName({name: 'getVisitByCustomerId',returntype: 'Map',parammaps: { customerId: newId }}, this.getPickerList3)
  //     //获取回访记录
  //     util.getDataByName({name: 'getTelvisitByCustomerId',returntype: 'Map',parammaps: { customerId: newId }}, this.getPickerList4)

  // },
  //日期选择触发
  on_rdate_tap:function(e){
    this.setData({
      ['create.temp.rdate']:e.detail.value
    })
  },
   
  //下拉框：接待人
  change_receiver:function(e){
    console.log('receiver的选项', e.detail.value)
    var newId = this.data.receiverNameList[e.detail.value]['id']
     this.setData({
       receiverIndex: e.detail.value,
       receiverId: newId,
       ['create.temp.receiverId']: newId,
     })
  },
  //下拉框：拜访记录
  change_visit:function(e){
    console.log('change_visit的选项', e.detail.value)
    var newId = this.data.visitNameList[e.detail.value]['id']
     this.setData({
       visitIndex: e.detail.value,
       visitId: newId,
       ['create.temp.visitId']: newId,
     })
  },
  //下拉框：回访记录
  change_telvisit:function(e){
    console.log('cchange_telvisit的选项', e.detail.value)
    var newId = this.data.telvisitNameList[e.detail.value]['id']
     this.setData({
        telvisitIndex: e.detail.value,
        telvisitId: newId,
       ['create.temp.telvisitId']: newId,
     })
  },
  

  
  //接待人文本框输入
  change_receiver_input:function(e){
    var value =e.detail.value
    var customerId = this.data.customerId
    console.log(value)
    //接待人列表框
    util.getDataByName({ name:'getContactsByNameApp',parammaps: {"customerId":customerId,"contactName":value,} }, this.getPickerList2)
  },

  getPickerList2:  function (res) {
    console.log('接待人下拉数据======>', res.data.list)
    var receiverNameList = res.data.list
    this.setData({
      receiverShow:true,
      allDisabled:false,
      receiverNameList: receiverNameList
    })
    if(receiverNameList !== null){
      this.setData({r_txt_Show:false})
    } else {
      this.setData({r_txt_Show:true})
    }
  },

  //接待人列表点击
  on_rece_tap: function (e) {
    console.log("接待人列表点击")
    var newId = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var that = this

    this.setData({
      receiverShow:false,
      allDisabled:true,
      ['create.temp.receiverName']: name,
      ['create.temp.receiverId']: newId,
      receiverChoose: 'true' 
    })
    //获取电话
    util.getDataByName({name: 'getTelById',returntype: 'Map',parammaps: { id: newId }
    }, function(e){
      console.log(e)
  
      var telephone = e.data.list[0].telephone
      that.setData({
        ['create.temp.receiverTel']: telephone, 
        telephone: telephone 
      })
    })
  },

  //接待人完成
  confirm_receiver_input:function(e){
    console.log("接收人直接输入")
    this.setData({
      receiverShow:false,
      allDisabled:true,
      receiverChoose: 'false' 
    })
  },


  //保存
  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.createrId = app.globalData.g_createrId
    submit_data.receiverChoose = this.data.receiverChoose

    var send_data = {
      "common": { "returnmap": "0" },
      "data": [
        {
          "name": "insertContactsNewApp", "type": "e",
          "parammaps": {
            "customerId": submit_data.customerId,
            "contactName": submit_data.receiverName,
            "telephone": submit_data.receiverTel,
            "address": "",
            "remark": submit_data.remark,
            "receiverChoose": this.data.receiverChoose
          }
        },
        {
          "name": "insertRemoteApp", "type": "e",
          "parammaps": submit_data
        }
      ]
    }


    //验证必填项是否为空
    if(submit_data.customerId == ""){
      wx.showToast({title: '客户名称必填',icon: 'none',duration: 2000})
    } else if(submit_data.rdate == ""){
      wx.showToast({title: '服务日期必填',icon: 'none',duration: 2000})
    } else if(submit_data.receiverName == ""){
      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:"../remoteService/remoteService"})
        }
      })
    } else {
      wx.showToast({
        title: '保存失败'+res.data,
        icon: 'error',
        duration: 2000,
      })
    }
  },

 
  
  onReady: function () {

  },

  
  onShow: function () {

  },

 
  onHide: function () {

  },

   
  onUnload: function () {

  },

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

  },

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

  },

 
  onShareAppMessage: function () {

  }
})