var util = require('../../../utils/util.js')
Page({
  data: {
    orderId: null,
    noData: '../../../images/noData.png',
    dataListTips: '暂无数据',
    // 初始日期范围
    form: {
      beginDate: util.get7daysAgo(),
      endDate: util.getRealToday(),
      orderId: null,
      staffId: '',
    },
    serviceStaffname: '',
    records: [],
    installerList: [],
  },

  onLoad(options) {
    const id = options.id
    this.setData({
      orderId: id,
      ['form.orderId']: id,
    })
    this.getDetail()
    // this.getinstallerList()
  },

  getDetail() {
    var send_data = [
      { name: 'getInstallationDailyDetailByOrderId', returntype: 'Map', parammaps: this.data.form },
    ]
    util.getDataByNames(send_data, this.handleDetail)
  },

  handleDetail(res) {
    var data = res.data
    if (data.getInstallationDailyDetailByOrderId.list != null) {
      var list = data.getInstallationDailyDetailByOrderId.list
      var service = list.map((item, indexx) => {
        const obj = {
          name: item.installUserName,
          id: item.installUserId,
        }
        return obj
      })

      const uniqueData = service.filter((item, index, self) => {
        return index === self.findIndex((t) => t.id === item.id)
      })
      console.log(uniqueData, 'service')
      this.setData({
        records: data.getInstallationDailyDetailByOrderId.list,
        installerList: uniqueData,
      })
    } else {
      this.setData({
        records: [],
      })
    }
  },
  onInstallerChange: function (e) {
    console.log(e, 'eee')
    this.setData({
      serviceStaffname: this.data.installerList[e.detail.value].name,
      ['form.staffId']: this.data.installerList[e.detail.value].id,
    })
  },
  onInputChangeClear(e) {
    this.setData({
      serviceStaffname: '',
      ['form.staffId']: '',
    })
  },
  // 服务人员
  getinstallerList() {
    var send_data = [
      {
        name: 'getUsersSelect',
        offset: 0,
        pagecount: 0,
        parammaps: {
          enable: '1',
          orderId: this.data.orderId,
        },
      },
    ]

    util.getDataByNames(send_data, this.handleInstallerList)
  },
  handleInstallerList(res) {
    const obj = res.data
    if (obj.getUsersSelect) {
      this.setData({
        installerList: obj.getUsersSelect.list,
      })
    }
  },

  onStaffChange(e) {
    this.setData({
      serverPerson: this.data.serviceList[e.detail.value],
    })
  },

  onContactChange(e) {
    this.setData({
      serverPerson: e.detail.value,
    })
  },

  onDateChange(e) {
    this.setData({
      'formObj.acceptanceDate': e.detail.value,
    })
  },
  onSelectPerson(event) {
    this.setData({
      'formObj.serverPerson': this.data.serviceList[event.detail.value],
    })
  },
  onBack() {
    wx.navigateBack()
  },
  uploadReceipt() {
    // 上传验收单的逻辑
    const that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        that.setData({
          'formObj.acceptanceFile': res.tempFilePaths[0],
          'errors.acceptanceFile': '',
        })
      },
    })
  },

  validateForm() {
    const { serverPerson, acceptanceDate, acceptanceFile } = this.data.formObj
    if (!serverPerson) {
      wx.showToast({ title: '请填写服务人员', icon: 'none' })
      return false
    }
    if (!acceptanceDate) {
      wx.showToast({ title: '请选择验收日期', icon: 'none' })
      return false
    }
    if (!acceptanceFile) {
      wx.showToast({ title: '请上传验收单', icon: 'none' })
      return false
    }
    return true
  },

  formSubmit(e) {
    console.log('this.data.formObj：', this.data.formObj)
    wx.showToast({ title: '提交成功', icon: 'success' })
    if (!this.validateForm()) return
  },

  formReset(e) {
    console.log('form发生了reset事件，携带数据为：', e.detail.value)
    this.setData({
      chosen: '',
    })
  },

  goBack() {
    wx.navigateBack()
  },
  //日期1
  bindDateChange1: function (e) {
    this.setData({
      ['form.beginDate']: e.detail.value,
    })
  },
  //日期2
  bindDateChange2: function (e) {
    this.setData({
      ['form.endDate']: e.detail.value,
    })
  },

  viewHistory() {
    // 查看历史记录
    wx.navigateTo({
      url: '/pages/workbench/historyRecord/historyRecord',
    })
  },

  // 更新今日完成量
  updateTodayQuantity(e) {
    const value = parseInt(e.detail.value) || 0
    const remaining = this.data.totalQuantity - this.data.completedQuantity - value

    this.setData({
      todayQuantity: value,
      remainingQuantity: remaining >= 0 ? remaining : 0,
    })
  },

  // 保存记录
  saveRecord() {
    wx.showLoading({
      title: '保存中...',
    })

    // 这里添加保存逻辑

    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '保存成功',
        icon: 'success',
      })
    }, 1500)
  },
})
