import useModel from './model'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { Apis } from '@/gen/Apis'
import { specificTime } from '@/common/libraries/day'

const getShow = (id: string) => {
  getApiLoading(Apis.WorkOrder.HouseWorkOrders.Show, { id }).then(res => {
    useModel.dataShow.value = res?.data
  })
}

const getHouseWorkLogs = (id: number) => {
  getApiLoading(Apis.WorkOrder.HouseWorkLogs.List, { house_work_orders_id: id }).then(res => {
    useModel.logsData.value = res?.data
  })
}

export default {
  getShow,
  handleEvaluate() {
    // uni.navigateTo({
    //   url: '/ME/user_evaluate/index'
    // })
    useModel.showEvaluate.value = true
  },
  handleClosed() {
    if (specificTime(useModel?.dataShow?.value?.expect_start_time)) {
      uni.showModal({
        title: '提示',
        content:
          '当前时间距离您选择的“期望处理时间”已经不足2小时，暂不支持关闭工单。若需取消，请联系处理人员操作。',
        cancelText: '点错了',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return false
    }
    uni.showModal({
      title: '提示',
      content: '关闭工单后无法继续操作，如需继续需要重新提交，您是否确认关闭',
      cancelText: '点错了',
      success: function (res) {
        if (res.confirm) {
          getApiLoading(Apis.WorkOrder.HouseWorkOrders.Close, {
            id: useModel?.dataShow?.value?.id
          }).then(res => {
            getShow(useModel?.dataShow?.value?.id)
            getHouseWorkLogs(useModel?.dataShow?.value?.id)
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  handlepopupClose() {
    useModel.showEvaluate.value = false
    uni.showToast({
      title: '评价已提交',
      icon: 'none'
    })
  },
  getHouseWorkLogs,

  handleAcceptance() {
    if (useModel?.dataShow?.value?.status === 'PendingAcceptance') {
      //去验收工单
      uni.navigateTo({
        url: `/WORKORDER/acceptance/index?id=${useModel?.dataShow?.value?.id}`
      })
      return false
    }
    if (useModel?.dataShow?.value?.status === 'PendingPayment') {
      //支付费用
      uni.navigateTo({
        url: `/WORKORDER/pay/index?id=${useModel?.dataShow?.value?.id}`
      })
      return false
    }
    if (useModel?.dataShow?.value?.status === 'Completed') {
      //评价
      useModel.showEvaluate.value = true
      return false
    }
    
  },
  handlePhoneCall(){
    //联系处理人
      if (useModel?.dataShow?.value?.assign_employee?.phone) {
        uni.makePhoneCall({
          phoneNumber: useModel?.dataShow?.value?.assign_employee?.phone //仅为示例，并非真实的电话号码
        })
      } else {
        uni.showToast({
          title: '暂无电话号码',
          icon: 'none'
        })
      }
  },
  hadnleUpdateTime() {
    if (specificTime(useModel?.dataShow?.value?.expect_start_time)) {
      uni.showModal({
        title: '提示',
        content: '距离工单开始时间小于2小时，不能修改处理时间！若需要，请联系处理人员操作。',
        cancelText: '点错了',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return false
    }
    uni.navigateTo({
      url: `/WORKORDER/update_work_time/index?id=${useModel?.dataShow?.value?.id}&day=${useModel?.dataShow?.value?.expect_start_time}`
    })
  },
  handleRework() {
    uni.navigateTo({
      url: `/WORKORDER/update_work_rework/index?id=${useModel?.dataShow?.value?.id}`
    })
  }
}
