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

  data: {
 
    groupNameList: [
      // {id: 11, name: "海文牧场"}, {id: 22, name: "成宏牧场"}, {id: 32, name: "瑞福牧业"}
    ],
    groupIndex:'',
    grouprId:'',
    customeStatusList: [
      // {id: 11, name: "海文牧场"}, {id: 22, name: "成宏牧场"}, {id: 32, name: "瑞福牧业"}
    ],
    customeStatusIndex:'',
    customeStatusId:'',
    rangeNameList: [
      // {id: 11, name: "海文牧场"}, {id: 22, name: "成宏牧场"}, {id: 32, name: "瑞福牧业"}
    ],
    rangeIndex:'',
    rangeId:'',
    provinceList: [
       {id: "北京市", name: "北京市"}, {id: "天津市", name: "天津市"}, {id: "河北省", name: "河北省"}, {id: "山西省", name: "山西省"}, {id: "内蒙古自治区", name: "内蒙古自治区"}, {id: "辽宁省", name: "辽宁省"}, {id: "吉林省", name: "吉林省"}, {id: "黑龙江省", name: "黑龙江省"}, {id: "上海市", name: "上海市"}, {id: "江苏省", name: "江苏省"}, {id: "浙江省", name: "浙江省"}, {id: "安徽省", name: "安徽省"}, {id: "福建省", name: "福建省"}, {id: "江西省", name: "江西省"}, {id: "山东省", name: "山东省"}, {id: "河南省", name: "河南省"}, {id: "湖北省", name: "湖北省"}, {id: "湖南省", name: "湖南省"}, {id: "广东省", name: "广东省"}, {id: "广西壮族自治区", name: "广西壮族自治区"}, {id: "海南省", name: "海南省"}, {id: "四川省", name: "四川省"}, {id: "贵州省", name: "贵州省"}, {id: "云南省", name: "云南省"}, {id: "重庆市", name: "重庆市"}, {id: "西藏自治区", name: "西藏自治区"}, {id: "陕西省", name: "陕西省"}, {id: "甘肃省", name: "甘肃省"}, {id: "青海省", name: "青海省"}, {id: "宁夏回族自治区", name: "宁夏回族自治区"}, {id: "新疆维吾尔自治区", name: "新疆维吾尔自治区"}, {id: "香港特别行政区", name: "香港特别行政区"}, {id: "澳门特别行政区", name: "澳门特别行政区"}, {id: "台湾省", name: "台湾省"} 
    ],
    provinceIndex:'',
    province:'',

    directorNameList: [
      // {id: 56, name: "苗舒怡"},{id: 57, name: "颛孙洋洋"},{id: 58, name: "李涛"}
    ],
    directorIndex:'',
    directorId:'',
 
   

    
 
    rules: {
      customerName: [{ required: true, message: '客户必填', trigger: 'blur' }],
      groupName: [{ required: true, message: '客户集团必填', trigger: 'blur' }],
      customeStatusName: [{ required: true, message: '客户状态必填', trigger: 'blur' }],
      directorName: [{ required: true, message: '负责人必填', trigger: 'blur' }] 
    },
    // 新增/编辑
    create: {
        dialogStatus: '',
        temp: {
           customerId: '',  customerName: '',  groupId: '', groupName: '',  customeStatusId: '', customeStatusName: '',  rangeId: '', rangeName: '', province: '', address: '',scode: '', directorId: '',directorName: '',createrName: '', createrId: '',  createtime: util.getToday(),   remark: '', content: '',  
           receiverChoose: 'true' 
        },
        
    }
  },

  onLoad: function (options) {
    // 表单初始化
    wx.lin.initValidateForm(this)
 
    var id = options.id;
    //获取编辑内容
    util.getDataByName({ name:'getCustomerByIdApp', 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],
      ['create.temp.createrId']: app.globalData.g_lgNameId,
      ['create.temp.createrName']: app.globalData.g_lgName,
      customeStatusId: res.data.list[0].customeStatusId, 
      groupId: res.data.list[0].groupId, 
      rangeId: res.data.list[0].rangeId,
      province: res.data.list[0].province,
      directorId: res.data.list[0].directorId
    })

   

    const provinceid = res.data.list[0].province
    var provinceList = this.data.provinceList
    const provinceIndex = provinceList.findIndex(obj => obj.id == provinceid) 
    this.setData({
 
      provinceIndex: provinceIndex
    })

    // 客户集团下拉
    util.getDataByName({ name: 'getDictListSelect1', parammaps: { "pid": "54" } }, this.getPickerList2)
    // 客户区域下拉
    util.getDataByName({ name: 'getDictListSelect2', parammaps: { "pid": "53" } }, this.getPickerList3)
    // 客户状态下拉
    util.getDataByName({ name: 'getDictListSelect3', parammaps: { "pid": "52" } }, this.getPickerList4)
    // 负责人下拉
    util.getDataByName({ name: 'getEmployeesSelect', parammaps: {} }, this.getPickerList5)
  },
  
  getPickerList2:  function (res) {
    console.log('客户集团下拉数据======>', res.data.list)
 
    const id = this.data.groupId
    const groupIndex = res.data.list.findIndex(obj => obj.id == id) 
    this.setData({
      groupNameList: res.data.list,
      groupIndex: groupIndex
    })
  },
  getPickerList3:  function (res) {
    console.log('客户区域下拉数据======>', res.data.list)
    
    const id = this.data.rangeId
    const rangeIndex = res.data.list.findIndex(obj => obj.id == id) 
    this.setData({
      rangeNameList: res.data.list,
      rangeIndex: rangeIndex
    })
  },
  getPickerList4:  function (res) {
    console.log('客户状态下拉数据======>', res.data.list)
 
    const id = this.data.customeStatusId
    const customeStatusIndex = res.data.list.findIndex(obj => obj.id == id) 
    this.setData({
      customeStatusList: res.data.list,
      customeStatusIndex: customeStatusIndex
    })
    
  },
  getPickerList5:  function (res) {
    console.log('负责人下拉数据======>', res.data.list)
 
    const id = this.data.directorId
    const directorIndex = res.data.list.findIndex(obj => obj.id == id) 
    this.setData({
      directorNameList: res.data.list,
      directorIndex: directorIndex
    })
  },

 
 
  //下拉框：客户集团
  change_group:function(e){
    var that = this
    console.log('change_group的选项', e.detail.value)
    var newId = this.data.groupNameList[e.detail.value]['id']
     this.setData({
       groupIndex: e.detail.value,
       groupId: newId,
       ['create.temp.groupId']: newId,
     })
  },

  //下拉框：客户状态
  change_customeStatus:function(e){
    var that = this
    console.log('change_customeStatus的选项', e.detail.value)
    var newId = this.data.customeStatusList[e.detail.value]['id']
     this.setData({
       customeStatusIndex: e.detail.value,
       customeStatusId: newId,
       ['create.temp.customeStatusId']: newId,
     })
  },

  //下拉框：客户区域
  change_range:function(e){
    var that = this
    console.log('change_range的选项', e.detail.value)
    var newId = this.data.rangeNameList[e.detail.value]['id']
     this.setData({
       rangeIndex: e.detail.value,
       rangeId: newId,
       ['create.temp.rangeId']: newId,
     })
  },

  //下拉框：客户省份
  change_province:function(e){
    var that = this
    console.log('change_province的选项', e.detail.value)
    var newId = this.data.provinceList[e.detail.value]['id']
     this.setData({
       provinceIndex: e.detail.value,
       province: newId,
       ['create.temp.province']: newId,
     })
  },
   //下拉框：负责人
   change_director:function(e){
    var that = this
    console.log('change_director的选项', e.detail.value)
    var newId = this.data.directorNameList[e.detail.value]['id']
     this.setData({
       directorIndex: e.detail.value,
       directorId: newId,
       ['create.temp.directorId']: newId,
     })
  },

  //保存
  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": 'updateCustomer',
      "parammaps":submit_data
 
    }


    //验证必填项是否为空
    if(submit_data.customerId == ""){
      wx.showToast({title: '客户名称必填',icon: 'none',duration: 2000})
    } else if(submit_data.groupId == ""){
      wx.showToast({title: '客户集团必填',icon: 'none',duration: 2000})
    } else if(submit_data.customeStatusId == ""){
      wx.showToast({title: '客户状态必填',icon: 'none',duration: 2000})
    } else if(submit_data.directorId == ""){
      wx.showToast({title: '负责人必填',icon: 'none',duration: 2000})
    }  else if(submit_data.scode.length != 18){
      wx.showToast({title: '社会统一代码必须18位！',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)
    if (res.msg !== 'fail') {
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 2000,
        success: function(){
          wx.switchTab({ url:"../customerInformation/customerInformation"})
        }
      })
    } else {
      wx.showToast({
        title: '保存失败'+res.data,
        icon: 'error',
        duration: 2000,
      })
    }
  },

 
  
  onReady: function () {

  },

  
  onShow: function () {

  },

 
  onHide: function () {

  },

   
  onUnload: function () {

  },

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

  },

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

  },

 
  onShareAppMessage: function () {

  }
})