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

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,
  getHouseWorkLogs,
  toPageAddLogs() {
    if (useModel?.dataShow?.value?.status === 'PendingAcceptance') {
      //去验收工单
      uni.navigateTo({
        url: `/WORKORDER/acceptance/index?id=${useModel?.dataShow?.value?.id}`
      })
    }
  },
  handleClosed() {
    if (
      useModel?.dataShow?.value?.expect_start_time &&
      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('用户点击取消')
        }
      }
    })
  },
  handlePendingPaymentQrCode(e: any) {},
  handleCopyPhone(phone: string) {
    if (phone) {
      uni.makePhoneCall({
        phoneNumber: phone //仅为示例，并非真实的电话号码
      })
    }
  }
}
