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

const getList = () => {
  getApiLoading(Apis.WorkOrder.HouseWorkOrders.List, useModel?.formData?.value).then(res => {
    useModel.dataList.value = [...useModel?.dataList?.value, ...res?.data]
    useModel.meta.value = res?.meta
  })
}
const handleSearch = () => {
  useModel.formData.value.page = 1
  useModel.dataList.value = []
  getList()
}
export default {
  getList,
  handleToDetail(e: { id: string }) {
    uni.navigateTo({
      url: `/WORKORDER/show/index?id=${e?.id}`
    })
  },
  handleAdd() {
    uni.navigateTo({
      url: '/WORKORDER/add/index'
    })
  },
  handleChangeTabs(idx: number, e: any) {
    useModel.currentTabs.value = idx
    useModel.formData.value.type = e?.value
    handleSearch()
  },
  handlepopupClose(e: any) {
    getApiLoading(Apis.WorkOrder.HouseWorkOrders.Evaluation, {
      id: useModel?.evaluateParams?.value?.id,
      evaluation_score: e?.rating
    }).then(() => {
      useModel.showEvaluate.value = false
      uni.showToast({
        title: '评价已提交',
        icon: 'none'
      })
      handleSearch()
    })
    console.log(e, 'eee')
  },
  handleAssignEmployee(e: any) {
    if (e?.status === 'PendingAcceptance') {
      //去验收工单
      uni.navigateTo({
        url: `/WORKORDER/acceptance/index?id=${e?.id}`
      })
      return false
    }
    if (e?.status === 'PendingPayment') {
      //支付费用
      uni.navigateTo({
        url: `/WORKORDER/pay/index?id=${e?.id}`
      })
      return false
    }
    if (e?.status === 'Completed') {
      //评价
      useModel.evaluateParams.value = e
      useModel.showEvaluate.value = true
      return false
    }
    if (e?.status === 'Processing') {
      if (e?.assign_employee?.phone) {
        uni.makePhoneCall({
          phoneNumber: e?.assign_employee?.phone //仅为示例，并非真实的电话号码
        })
      } else {
        uni.showToast({
          title: '暂无电话号码',
          icon: 'none'
        })
      }
    }

    console.log(e, 'eee')
  },
  handleChangeStatus(e: { value: string }) {
    useModel.formData.value.status = e?.value
    handleSearch()
  },
  handleLoad(page: number) {
    useModel.formData.value.page = page
    getList()
  }
}
