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

  data: {
    customerNameList: [
      // {id: 11, name: "海文牧场"}, {id: 22, name: "成宏牧场"}, {id: 32, name: "瑞福牧业"}
    ],
    customerIndex:'',
    customerId:'',
    //投诉接收人
    receiverNameList: [
      // {id: 56, name: "苗舒怡"},{id: 57, name: "颛孙洋洋"},{id: 58, name: "李涛"}
    ],
    receiverIndex:'',
    receiverId:'',
    //被投诉人
    respondentNameList: [
      // {id: 222, name: "拜访"}, {id: 223, name: "远程服务"}, {id: 224, name: "现场服务"}
    ],
    respondentIndex:'',
    respondentId:'',
   

    allDisabled:true,
    receiverShow:false,
    r_txt_Show:false,
    receiverChoose:'false',
    telephone:"",

    //投诉人
    proposerNameList: [
      // {id: 222, name: "拜访"}, {id: 223, name: "远程服务"}, {id: 224, name: "现场服务"}
    ],
    proposerIndex:'',
    proposerId:'',

    groupId: '', 
    groupName: '',


    rules: {
      customerName: [{ required: true, message: '客户必填', trigger: 'blur' }],
      cdate: [{ required: true, message: '日期必填', trigger: 'blur' }],
      proposerName: [{ required: true, message: '投诉人必填', trigger: 'blur' }],
      telephone: [{ required: true, message: '投诉人电话必填', trigger: 'blur' }],
      receiverName: [{ required: true, message: '投诉接收人必填', trigger: 'blur' }],
      respondentName: [{ required: true, message: '被投诉人必填', trigger: 'blur' }],
      content: [{  required: true, message: '情况说明必填', trigger: 'blur' }],
    },
    // 新增/编辑
    create: {
        dialogStatus: '',
        temp: {
           customerId: '', 
           customerName: '', 
           groupId: '', 
           groupName: '', 
           cdate: util.getToday(), 
           proposerId: '', 
           proposerName: '', 
           receiverId: '', 
           receiverId2: '@insertContactsNew.LastInsertId', 
           receiverName: '', 
           telephone: '',
           respondentId: '',
           respondentName: '',
           content: '',
           remark: '', 
           created: util.getToday(), 
           receiverChoose: 'true' 
        },

        
    }
  },

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

    console.log(app.globalData.g_lgName)


    var id = options.id;
    //获取编辑内容
    util.getDataByName({ name:'getComplaintById', parammaps: {"id":id} }, this.getShowList)

    
  },
  getShowList:function(res){
    console.log('展示数据======>', res.data.list)
    var customerId = res.data.list[0].customerId
    this.setData({
      ['create.temp']:  res.data.list[0],
   
      customerId: res.data.list[0].customerId, 
      receiverId: res.data.list[0].receiverId, 
      respondentId: res.data.list[0].respondentId,
      proposerId: res.data.list[0].proposerId
    })
    //客户下拉
    util.getDataByName({ name:'getCustomerSelect',parammaps: {} }, this.getPickerList1)
    //获取投诉接受人
    //util.getDataByName({name: 'getEmployeesSelect2',parammaps: {}}, this.getPickerList2)
    //被投诉人
    util.getDataByName({name: 'getEmployeesSelect',parammaps: {}}, this.getPickerList3)
    //获取投诉人
    //util.getDataByName({name: 'getContacts',returntype: 'Map',parammaps: { customerId: customerId }}, this.getPickerList4)

    
  },
  getPickerList1:  function (res) {
    console.log('客户下拉数据======>', res.data.list)
    const id = this.data.customerId
    const customerIndex = res.data.list.findIndex(obj => obj.id == id) 
    this.setData({
      customerNameList: res.data.list,
      customerIndex: customerIndex
    })
  },
  getPickerList2:  function (res) {
    console.log('投诉接收人下拉数据======>', res.data.list)
    const id = this.data.receiverId
    const receiverIndex = res.data.list.findIndex(obj => obj.id == id) 
    this.setData({
      receiverNameList: res.data.list,
      receiverIndex: receiverIndex
    })
  },
  getPickerList3:  function (res) {
    console.log('被投诉人下拉数据======>', res.data.list)
    const id = this.data.respondentId
    const respondentIndex = res.data.list.findIndex(obj => obj.id == id) 
    this.setData({
      respondentNameList: res.data.list,
      respondentIndex: respondentIndex
    })
  },
  // getPickerList4:  function (res) {
  //   console.log('投诉人数据======>', res.data.list)
  //   const id = this.data.proposerId
  //   const proposerIndex = res.data.list.findIndex(obj => obj.id == id) 
  //   this.setData({
  //     proposerNameList: res.data.list,
  //     proposerIndex: proposerIndex
  //   })
  // },
 
  //日期选择触发
  on_cdate_tap:function(e){
    this.setData({
      ['create.temp.cdate']: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_proposer:function(e){
    console.log('change_proposer的选项', e.detail.value)
    var newId = this.data.proposerNameList[e.detail.value]['id']
     this.setData({
       proposerIndex: e.detail.value,
       proposerId: newId,
       ['create.temp.proposerId']: newId,
     })
  },
  //下拉框：被投诉人
  change_respondent:function(e){
    console.log('change_respondent的选项', e.detail.value)
    var newId = this.data.respondentNameList[e.detail.value]['id']
     this.setData({
        respondentIndex: e.detail.value,
        respondentId: newId,
       ['create.temp.respondentId']: 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.getPickerList4)
  },

  getPickerList4:  function (res) {
    console.log('接待人下拉数据======>', res.data.list)
    var proposerNameList = res.data.list
    this.setData({
      receiverShow:true,
      allDisabled:false,
      proposerNameList: proposerNameList
    })
    if(proposerNameList !== 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.proposerName']: name,
      ['create.temp.proposerNameId']: 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.telephone']: 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)
    submit_data.receiverChoose = this.data.receiverChoose
   
    console.log("保存发送：",submit_data)
 
 
    var send_data = {
      "common": { "returnmap": "0" },
      "data": [
        {
          "name": "insertContactsNewApp", "type": "e",
          "parammaps": {
            "customerId": submit_data.customerId,
            "contactName": submit_data.proposerName,
            "telephone": submit_data.telephone,
            "address": "",
            "remark": submit_data.remark,
            "receiverChoose": this.data.receiverChoose
          }
        },
        {
          "name": "updateComplaintApp",
          "resultmaps": {
            "list": [submit_data]
          },
          "children": [{
            "name": "updateComplaintApp",
            "type": "e",
            "parammaps": {
              "groupId": "@updateComplaintApp.groupId",
                "cdate": "@updateComplaintApp.cdate",
          "proposerName": "@updateComplaintApp.proposerName",
                "telephone": "@updateComplaintApp.telephone",
                "receiverId": "@updateComplaintApp.receiverId",
            "respondentId": "@updateComplaintApp.respondentId",
                "remark": "@updateComplaintApp.remark",
                "content": "@updateComplaintApp.content",
                "id": "@updateComplaintApp.id"

            }
          }]
        }
      ]
    }

    //验证必填项是否为空
    if(submit_data.customerId == ""){
      wx.showToast({title: '客户名称必填',icon: 'none',duration: 2000})
    } else if(submit_data.cdate == ""){
      wx.showToast({title: '投诉日期必填',icon: 'none',duration: 2000})
    } else if(submit_data.proposerName == ""){
      wx.showToast({title: '投诉人必填',icon: 'none',duration: 2000})
    } else if(submit_data.telephone == ""){
      wx.showToast({title: '投诉人电话必填',icon: 'none',duration: 2000})
    } else if(submit_data.respondentId == ""){
      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 () {

  }
})