<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        @confirm="handleSubmit"
        @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
        <el-form-item label="" prop="dataid">
          <el-input v-model="formData.dataid" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="单据编号" prop="num">
          <el-input v-model="formData.num" clearable placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="项目名称" prop="project">
          <el-input v-model="formData.project" clearable placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目编号" prop="project_num">
          <el-input v-model="formData.project_num" clearable placeholder="请输入项目编号" />
        </el-form-item>
        <el-form-item label="任务名称" prop="task">
          <el-input v-model="formData.task" clearable placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="工序" prop="processes">
          <el-input v-model="formData.processes" clearable placeholder="请输入工序" />
        </el-form-item>
        <el-form-item label="任务类型" prop="rtype">
          <el-input v-model="formData.rtype" clearable placeholder="请输入任务类型" />
        </el-form-item>
        <el-form-item label="专业类型" prop="zType">
          <el-input v-model="formData.zType" clearable placeholder="请输入专业类型" />
        </el-form-item>
        <el-form-item label="工程师" prop="engineer">
          <el-input v-model="formData.engineer" clearable placeholder="请输入工程师" />
        </el-form-item>
        <el-form-item label="项目负责人" prop="head">
          <el-input v-model="formData.head" clearable placeholder="请输入项目负责人" />
        </el-form-item>
        <el-form-item label="审核流程" prop="examine">
          <el-input v-model="formData.examine" clearable placeholder="请输入审核流程" />
        </el-form-item>
        <el-form-item label="一级审核人" prop="one">
          <el-input v-model="formData.one" clearable placeholder="请输入一级审核人" />
        </el-form-item>
        <el-form-item label="二级审核人" prop="two">
          <el-input v-model="formData.two" clearable placeholder="请输入二级审核人" />
        </el-form-item>
        <el-form-item label="三级审核人" prop="three">
          <el-input v-model="formData.three" clearable placeholder="请输入三级审核人" />
        </el-form-item>
        <el-form-item label="登记日期" prop="apptime">
          <el-input v-model="formData.apptime" clearable placeholder="请输入登记日期" />
        </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="area">
          <el-input v-model="formData.area" clearable placeholder="请输入建筑面积" />
        </el-form-item>
        <el-form-item label="建筑结构类型" prop="type">
          <el-input v-model="formData.type" clearable placeholder="请输入建筑结构类型" />
        </el-form-item>
        <el-form-item label="建筑用途" prop="use">
          <el-input v-model="formData.use" clearable placeholder="请输入建筑用途" />
        </el-form-item>
        <el-form-item label="施工单位" prop="construction">
          <el-input v-model="formData.construction" clearable placeholder="请输入施工单位" />
        </el-form-item>
        <el-form-item label="编制要求" prop="ask">
          <el-input v-model="formData.ask" clearable placeholder="请输入编制要求" />
        </el-form-item>
        <el-form-item label="造价依据" prop="according">
          <el-input v-model="formData.according" clearable placeholder="请输入造价依据" />
        </el-form-item>
        <el-form-item label="材料补差" prop="clbc">
          <el-input v-model="formData.clbc" clearable placeholder="请输入材料补差" />
        </el-form-item>
        <el-form-item label="送审金额" prop="ssje">
          <el-input v-model="formData.ssje" clearable placeholder="请输入送审金额" />
        </el-form-item>
        <el-form-item label="工程师核定价" prop="gcshd">
          <el-input v-model="formData.gcshd" clearable placeholder="请输入工程师核定价" />
        </el-form-item>
        <el-form-item label="核增/核减金额" prop="hzhj">
          <el-input v-model="formData.hzhj" clearable placeholder="请输入核增/核减金额" />
        </el-form-item>
        <el-form-item label="人工单价" prop="rg">
          <el-input v-model="formData.rg" clearable placeholder="请输入人工单价" />
        </el-form-item>
        <el-form-item label="项目负责人核定价" prop="xmhd">
          <el-input v-model="formData.xmhd" clearable placeholder="请输入项目负责人核定价" />
        </el-form-item>
        <el-form-item label="部门负责人核定价" prop="bmhd">
          <el-input v-model="formData.bmhd" clearable placeholder="请输入部门负责人核定价" />
        </el-form-item>
        <el-form-item label="审核部核定价" prop="shbhd">
          <el-input v-model="formData.shbhd" clearable placeholder="请输入审核部核定价" />
        </el-form-item>
        <el-form-item label="开工日期" prop="kaigong">
          <el-input v-model="formData.kaigong" clearable placeholder="请输入开工日期" />
        </el-form-item>
        <el-form-item label="竣工日期" prop="jungong">
          <el-input v-model="formData.jungong" clearable placeholder="请输入竣工日期" />
        </el-form-item>
        <el-form-item label="备注" prop="bz">
          <el-input v-model="formData.bz" clearable placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="结果文件" prop="result_file">
          <material-picker v-model="formData.result_file" />
        </el-form-item>
        <el-form-item label="项目负责人附件" prop="master_annex">
          <material-picker v-model="formData.master_annex" />
        </el-form-item>
        <el-form-item label="部门负责人附件" prop="bm_annex">
          <material-picker v-model="formData.bm_annex" />
        </el-form-item>
        <el-form-item label="审核部附件" prop="shb_annex">
          <material-picker v-model="formData.shb_annex" />
        </el-form-item>

      </el-form>
    </popup>
  </div>
</template>

<script lang="ts" setup name="taskHandlingThreeLevelReviewEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiTaskHandlingThreeLevelReviewAdd, apiTaskHandlingThreeLevelReviewEdit, apiTaskHandlingThreeLevelReviewDetail } from '@/api/task_handling_three_level_review'
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: '',
  dataid: '',
  num: '',
  project: '',
  project_num: '',
  task: '',
  processes: '',
  rtype: '',
  zType: '',
  engineer: '',
  head: '',
  examine: '',
  one: '',
  two: '',
  three: '',
  apptime: '',
  level: '',
  area: '',
  type: '',
  use: '',
  construction: '',
  ask: '',
  according: '',
  clbc: '',
  ssje: '',
  gcshd: '',
  hzhj: '',
  rg: '',
  xmhd: '',
  bmhd: '',
  shbhd: '',
  kaigong: '',
  jungong: '',
  bz: '',
  result_file: '',
  master_annex: '',
  bm_annex: '',
  shb_annex: '',

})


// 表单验证
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 apiTaskHandlingThreeLevelReviewDetail({
    id: row.id
  })
  setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate()
  const data = { ...formData,  }
  mode.value == 'edit'
      ? await apiTaskHandlingThreeLevelReviewEdit(data)
      : await apiTaskHandlingThreeLevelReviewAdd(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>
