<template>
  <div class="edit-popup">
    <popup ref="popupRef" :title="popupTitle" :async="true" width="60%" @confirm="handleSubmit" @close="handleClose">
      <el-form ref="formRef" :inline="true" :model="formData" label-width="120px" :rules="formRules">
        <el-form-item label="主表标识" prop="task_allocation_dataid">
          <el-input v-model="formData.task_allocation_dataid" clearable placeholder="请输入主表标识" />
        </el-form-item>
        <el-form-item label="任务名称" prop="task_name">
          <el-input v-model="formData.task_name" clearable placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务名称id" prop="task_name_id">
          <el-input v-model="formData.task_name_id" clearable placeholder="请输入任务名称id" />
        </el-form-item>
        <el-form-item label="工序" prop="process">
          <el-input v-model="formData.process" clearable placeholder="请输入工序" />
        </el-form-item>
        <el-form-item label="任务类型" prop="task_type_id">
          <el-select class="flex-1" v-model="formData.task_type_id" clearable placeholder="请选择任务类型">
            <el-option v-for="(item, index) in dictData.task_type" :key="index" :label="item.name"
              :value="parseInt(item.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="专业类型" prop="professional_type">
          <el-select class="flex-1" v-model="formData.professional_type" clearable placeholder="请选择专业类型">
            <el-option v-for="(item, index) in dictData.major_type" :key="index" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="工程师" prop="engineer">
          <el-input v-model="formData.engineer" clearable placeholder="请输入工程师" />
        </el-form-item>
        <el-form-item label="工程师id" prop="engineer_id">
          <el-input v-model="formData.engineer_id" clearable placeholder="请输入工程师id" />
        </el-form-item>
        <el-form-item label="任务级次" prop="level">
          <el-input v-model="formData.level" clearable placeholder="请输入任务级次" />
        </el-form-item>
        <el-form-item label="一级审核人" prop="first_reviewer">
          <el-input v-model="formData.first_reviewer" clearable placeholder="请输入一级审核人" />
        </el-form-item>
        <el-form-item label="一级审核人id" prop="first_reviewer_id">
          <el-input v-model="formData.first_reviewer_id" clearable placeholder="请输入一级审核人id" />
        </el-form-item>
        <el-form-item label="二级审核人" prop="secondary_reviewer">
          <el-input v-model="formData.secondary_reviewer" clearable placeholder="请输入二级审核人" />
        </el-form-item>
        <el-form-item label="二级审核人id" prop="secondary_reviewer_id">
          <el-input v-model="formData.secondary_reviewer_id" clearable placeholder="请输入二级审核人id" />
        </el-form-item>
        <el-form-item label="三级审核人" prop="third_level_reviewer">
          <el-input v-model="formData.third_level_reviewer" clearable placeholder="请输入三级审核人" />
        </el-form-item>
        <el-form-item label="三级审核人id" prop="third_level_reviewer_id">
          <el-input v-model="formData.third_level_reviewer_id" clearable placeholder="请输入三级审核人id" />
        </el-form-item>
        <el-form-item label="中标金额" prop="bid_winning_amount">
          <el-input v-model="formData.bid_winning_amount" clearable placeholder="请输入中标金额（元）" />
        </el-form-item>
        <el-form-item label="送审造价" prop="cost">
          <el-input v-model="formData.cost" clearable placeholder="请输入送审造价（元）" />
        </el-form-item>
        <el-form-item label="第三方核定金额" prop="approved_amount">
          <el-input v-model="formData.approved_amount" clearable placeholder="请输入第三方核定金额（元）" />
        </el-form-item>
        <el-form-item label="施工单位" prop="construction_unit">
          <el-input v-model="formData.construction_unit" clearable placeholder="请输入施工单位" />
        </el-form-item>
        <el-form-item label="施工单位id" prop="construction_unit_id">
          <el-input v-model="formData.construction_unit_id" clearable placeholder="请输入施工单位id" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="formData.contact" clearable placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contact_phone">
          <el-input v-model="formData.contact_phone" clearable placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="天数" prop="days">
          <el-input v-model="formData.days" clearable placeholder="请输入天数" />
        </el-form-item>
        <el-form-item label="开始日期" prop="start_date">
          <el-input v-model="formData.start_date" clearable placeholder="请输入开始日期" />
        </el-form-item>
        <el-form-item label="结束日期" prop="end_date">
          <el-input v-model="formData.end_date" clearable placeholder="请输入结束日期" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>

<script lang="ts" setup name="taskDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiTaskDetailAdd, apiTaskDetailEdit, apiTaskDetailDetail } from '@/api/task_detail'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑任务明细' : '新增任务明细'
})

// 表单数据
const formData = reactive({
  id: '',
  task_allocation_dataid: '',
  task_name: '',
  task_name_id: '',
  process: '',
  task_type_id: '',
  professional_type: '',
  engineer: '',
  engineer_id: '',
  level: '',
  first_reviewer: '',
  first_reviewer_id: '',
  secondary_reviewer: '',
  secondary_reviewer_id: '',
  third_level_reviewer: '',
  third_level_reviewer_id: '',
  bid_winning_amount: '',
  cost: '',
  approved_amount: '',
  construction_unit: '',
  construction_unit_id: '',
  contact: '',
  contact_phone: '',
  days: '',
  start_date: '',
  end_date: '',
  remark: '',

})


// 表单验证
const formRules = reactive<any>({

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }


}

const getDetail = async (row: Record<string, any>) => {
  const data = await apiTaskDetailDetail({
    id: row.id
  })
  setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate()
  const data = { ...formData, }
  mode.value == 'edit'
    ? await apiTaskDetailEdit(data)
    : await apiTaskDetailAdd(data)
  popupRef.value?.close()
  emit('success')
}

//打开弹窗
const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
  emit('close')
}



defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
