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

  data: {
    photoShow1:false,
    photoUrl1:"",
    photoShow2:false,
    photoUrl2:"",
    id:"",
    customerNameList: [
      // {id: 11, name: "海文牧场"}, {id: 22, name: "成宏牧场"}, {id: 32, name: "瑞福牧业"}
    ],
    customerIndex:'',
    customerId:'',
    receiverNameList: [
      // {id: 56, name: "苗舒怡"},{id: 57, name: "颛孙洋洋"},{id: 58, name: "李涛"}
    ],
    receiverIndex:'',
    receiverId:'',
    serviceTypeNameList: [
      // {id: 222, name: "拜访"}, {id: 223, name: "远程服务"}, {id: 224, name: "现场服务"}
    ],
    servicetypeIndex:'',
    servicetypeId:'',

    visitNameList: [
      //  {id: 1215, name: "拜访单号：BF210220001 拜访人：管理员 拜访日期：2021-03-20"}
    ],
    visitIndex:'',
    visitId:'',
    telvisitNameList: [
      // {id: 456, name: "回访单号： 回访人：多秋阳 回访日期：2019-11-18"}
    ],
    telvisitIndex:'',
    telvisitId:'',

    


    groupId: '', 
    groupName: '',

   
   
 
    rules: {
      customerName: [{ required: true, message: '客户必填', trigger: 'blur' }],
      serviceCode: [{ required: true, message: '服务单号必填', trigger: 'blur' }],
      serviceBeginDate: [{ required: true, message: '服务日期必填', trigger: 'blur' }],
      employeeName: [{ required: true, message: '服务人员必填', trigger: 'blur' }],
      serviceTypeName: [{ required: true, message: '服务类型必填', trigger: 'blur' }],
      receiverName: [{ required: true, message: '接待人必填', trigger: 'blur' }],
      receiverTel: [{ required: true, message: '接待人电话必填', trigger: 'blur' }],
      describe: [{ required: true, message: '服务描述必填', trigger: 'blur' }],
      serviceResult: [{ required: true, message: '服务结果必填', trigger: 'blur' }],
      scenePhoto: [{ required: true, message: '服务单照片必填', trigger: 'blur' }],
      serviceCodePhoto0: [{ required: true, message: '现场照片必填', trigger: 'blur' }],
      location: [{ required: true, message: '必填', trigger: 'blur' }]
    
    },
    // 新增/编辑
    create: {
        dialogStatus: '',
  
        temp: {
           customerId: '', customerName: '', groupId: '',groupName: '', serviceCode: '',  serviceBeginDate: util.getToday(), employeeId: '', servicetypeId: '', employeeName: '', serviceTypeName: '',visiterId: '', visiterName: '', receiverId: '', receiverName: '',receiverTel: '', remark: '', content: '', nextId: '',  nextName: '', describe: '', serviceResult: '',telvisitId: '', stockupIds: '',telvisitName: '', stockupName: '',
           created: util.getToday(), 
           receiverChoose: 'true' , location: '',
           scenePhoto1: -1, scenePhoto2: -1, scenePhoto3: -1, scenePhoto4: -1,scenePhoto5: -1,scenePhoto6: -1,scenePhoto7: -1,scenePhoto8: -1,scenePhoto9: -1,serviceCodePhoto: -1,
           picname1: '', picname2: '', picname3: '', picname4: '', picname5: '', picname6: '', picname7: '', picname8: '', picname9: '',codePicname: ''
        },
    },
    scenePhotoUrlList:[],
    serviceCodePhotoUrlList:[]
    
  },

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

 

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

   
    
  },
  getShowList:function(res){
    console.log('展示数据======>', res.data.list)
    var customerId = res.data.list[0].customerId
    

    var picname1 = res.data.list[0].picname1
    var picname2 = res.data.list[0].picname2
    var picname3 = res.data.list[0].picname3
    var picname4 = res.data.list[0].picname4
    var picname5 = res.data.list[0].picname5
    var picname6 = res.data.list[0].picname6
    var picname7 = res.data.list[0].picname7
    var picname8 = res.data.list[0].picname8
    var picname9 = res.data.list[0].picname9
    var scenePhotoUrlList = []
    var codePicname = res.data.list[0].codePicname
    var serviceCodePhotoUrlList = []
    if(picname1 != undefined || picname1 != undefined){scenePhotoUrlList.push(app.globalData.g_url + picname1)}
    if(picname2 != undefined || picname2 != undefined){scenePhotoUrlList.push(app.globalData.g_url + picname2)}
    if(picname3 != undefined || picname3 != undefined){scenePhotoUrlList.push(app.globalData.g_url + picname3)}
    if(picname4 != undefined || picname4 != undefined){scenePhotoUrlList.push(app.globalData.g_url + picname4)}
    if(picname5 != undefined || picname5 != undefined){scenePhotoUrlList.push(app.globalData.g_url + picname5)}
    if(picname6 != undefined || picname6 != undefined){scenePhotoUrlList.push(app.globalData.g_url + picname6)}
    if(picname7 != undefined || picname7 != undefined){scenePhotoUrlList.push(app.globalData.g_url + picname7)}
    if(picname8 != undefined || picname8 != undefined){scenePhotoUrlList.push(app.globalData.g_url + picname8)}
    if(picname9 != undefined || picname9 != undefined){scenePhotoUrlList.push(app.globalData.g_url + picname9)}

    if(codePicname != undefined ){serviceCodePhotoUrlList.push(app.globalData.g_url + codePicname)}


  

    this.setData({
      ['create.temp']:  res.data.list[0],
 
      customerId: res.data.list[0].customerId, 
      receiverId: res.data.list[0].receiverId, 
      servicetypeId: res.data.list[0].servicetypeId,
      visitId: res.data.list[0].visitId,
      telvisitId: res.data.list[0].telvisitId,
      postalId: res.data.list[0].postalId,
      scenePhotoUrlList:scenePhotoUrlList,
      serviceCodePhotoUrlList:serviceCodePhotoUrlList,
 
    })


    
     
    //获取拜访
    util.getDataByName({name: 'getVisitByCustomerId',returntype: 'Map',parammaps: { customerId: customerId }}, this.getPickerList4)
    //获取回访
    util.getDataByName({name: 'getTelvisitByCustomerId',returntype: 'Map',parammaps: { customerId: customerId }}, this.getPickerList5)
 
   
  },
  getPickerList1:  function (res) {
    console.log('客户下拉数据======>', res.data.list)
    this.setData({
      customerNameList: res.data.list
    })
  },
   
  getPickerList4:  function (res) {
    console.log('拜访记录======>', res.data.list)
 
    const id = this.data.visitId
   
    if(id){
      const visitIndex = res.data.list.findIndex(obj => obj.id == id) 
      this.setData({
        visitNameList: res.data.list,
        visitIndex: visitIndex
      })
    } else {

    }
  },
  getPickerList5:  function (res) {
    console.log('回访记录 =====>', res.data.list)
 
    const id = this.data.telvisitId
  
    if(id){
      const telvisitIndex = res.data.list.findIndex(obj => obj.id == id) 
      this.setData({
        telvisitNameList: res.data.list,
        telvisitIndex: telvisitIndex
      })
    } else {

    }
  },
 
   
  //日期选择触发
  on_serviceDate_tap:function(e){
    this.setData({
      ['create.temp.serviceBeginDate']: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_servicetype:function(e){
    console.log('change_servicetype的选项', e.detail.value)
    var newId = this.data.serviceTypeNameList[e.detail.value]['id']
     this.setData({
       servicetypeIndex: e.detail.value,
       servicetypeId: newId,
       ['create.temp.servicetypeId']: 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('change_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,
     })
  },
   

 
  //查看
  on_see_tap1: function (e) {
    var urlmsg = e.currentTarget.dataset.urlmsg;
    console.log(urlmsg)
 
    this.setData({
      photoShow1:true,
      photoUrl1:urlmsg
    })
  },

  on_close_tap1: function (e) {
    console.log(222)
    this.setData({
      photoShow1:false
    })
  },
   //查看
   on_see_tap2: function (e) {
    var urlmsg = e.currentTarget.dataset.urlmsg;
    console.log(urlmsg)
 
    this.setData({
      photoShow2:true,
      photoUrl2:urlmsg
    })
  },

  on_close_tap2: function (e) {
    console.log(222)
    this.setData({
      photoShow2:false
    })
  },

   //完成服务
   on_service_tap: function (e) {
    var id = this.data.id;
    wx.redirectTo({ url:"../siteServiceCompleteService/siteServiceCompleteService?id=" + id})
  },
 

  //服务反馈
  on_feedback_tap: function (e) {
    var id = this.data.id;
    wx.redirectTo({ url:"../siteServiceFeedback/siteServiceFeedback?id=" + id})
  },
 
  //撤销
  on_revoke_tap: function (e) {
    var id = this.data.id;
    wx.redirectTo({ url:"../siteServiceRevoke/siteServiceRevoke?id=" + id})
  },
 
   
  
 

 
  
  onReady: function () {

  },

  
  onShow: function () {

  },

 
  onHide: function () {

  },

   
  onUnload: function () {

  },

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

  },

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

  },

 
  onShareAppMessage: function () {

  }
})