import { useWeAppAuthStore } from '@/common'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { showToast, showToastBack } from '@/common/libraries/naviHelper'
import { Apis } from '@/gen/Apis'
import useModel from './model'
const auth = useWeAppAuthStore()
export default {
  init(data: any) {
    useModel.HouseWorkOrdersStatusItemEnum.value = {
      Processing: { text: '仍在处理', value: 'Processing' },
      Completed: { text: '我已完工', value: 'Completed' }
    }
    useModel.HouseWorkOrdersCostEnum.value = {
      Processing2: { text: '是', value: '1' },
      Processing1: { text: '否', value: '0' }
    }
    useModel.formData.value = {
      ...data,
      house_work_orders_id: data?.id
    }
  },
  handleSubmit() {
    console.log('handleSubmit', useModel.formData.value)
   if (!useModel.formData.value.status) {
      return showToast('请先选择状态！')
    }
    if (!useModel.formData.value.description) {
      return showToast('请输入备注说明/进度说明！')
    }
    if (
      useModel.formData.value.status === 'Completed' &&
      !useModel.formData.value.attachments?.length
    ) {
      return showToast('请上传完工图片！')
    }
    if (
      useModel?.formData?.value?.status === 'Completed' &&
      useModel?.formData?.value?.location === 'MyHome' &&
      !useModel?.formData?.value?.is_fee
    ) {
      return showToast('请上选择是否产生费用！')
    }
    if (useModel.formData.value.is_fee === '1' && !useModel?.formData?.value?.amount) {
      return showToast('请输入费用金额！')
    }
    if (
      useModel.formData.value.is_fee === '1' &&
      !useModel?.formData?.value?.company_project_receipt_accounts_id
    ) {
      return showToast('请选择收款账号！')
    }
    getApiLoading(Apis.WorkOrder.HouseWorkLogs.Store, {
      ...useModel.formData.value,
      // is_fee: useModel.formData.value.is_fee === '1' ? true : false
    }).then(res => {
      showToastBack('提交成功！', 1, true)
      console.log(res)
    })
  }
}
