<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="types">
          <el-select class="flex-1" v-model="formData.types" clearable placeholder="请选择盖章申请类型">
            <el-option
                v-for="(item, index) in dictData.apply_with_seal_type"
                :key="index"
                :label="item.name"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="文件类型" prop="filetype">
          <el-select class="flex-1" v-model="formData.filetype" clearable placeholder="请选择文件类型">
            <el-option
                v-for="(item, index) in dictData.apply_with_seal_file_type"
                :key="index"
                :label="item.name"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="骑缝盖章" prop="ridingseam">
          <el-radio-group v-model="formData.ridingseam" placeholder="请选择骑缝盖章">
            <el-radio
                v-for="(item, index) in dictData.isqfgz"
                :key="index"
                :label="item.value"
            >
              {{ item.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="印章名称" prop="name">
          <el-input v-model="formData.name" clearable placeholder="请输入印章名称" />
        </el-form-item>
        <el-form-item label="盖章次数" prop="frequency">
          <el-input v-model="formData.frequency" clearable placeholder="请输入盖章次数" />
        </el-form-item>
        <el-form-item label="限制次数" prop="limit">
          <el-input v-model="formData.limit" clearable placeholder="请输入限制次数" />
        </el-form-item>
        <el-form-item label="用印部门" prop="depar">
          <el-input v-model="formData.depar" clearable placeholder="请输入用印部门" />
        </el-form-item>
        <el-form-item label="盖章人" prop="stampman">
          <el-input v-model="formData.stampman" 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="principal">
          <el-input v-model="formData.principal" clearable placeholder="请输入项目负责人" />
        </el-form-item>
        <el-form-item label="盖章内容" prop="content">
          <el-input v-model="formData.content" clearable placeholder="请输入盖章内容" />
        </el-form-item>
        <el-form-item label="印章是否外借" prop="loan">
          <el-input v-model="formData.loan" clearable placeholder="请输入印章是否外借" />
        </el-form-item>
        <el-form-item label="印章借用日期" prop="borrow">
          <el-date-picker
              class="flex-1 !flex"
              v-model="formData.borrow"
              clearable
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="选择印章借用日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="结束借用日期" prop="returndate">
          <el-date-picker
              class="flex-1 !flex"
              v-model="formData.returndate"
              clearable
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="选择结束借用日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="申请人" prop="applicant">
          <el-input v-model="formData.applicant" clearable placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="申请日期" prop="date">
          <el-date-picker
              class="flex-1 !flex"
              v-model="formData.date"
              clearable
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="选择申请日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="实际借章日期" prop="sjborrow">
          <el-date-picker
              class="flex-1 !flex"
              v-model="formData.sjborrow"
              clearable
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="选择实际借章日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="实际借章人" prop="sjborrower">
          <el-input v-model="formData.sjborrower" clearable placeholder="请输入实际借章人" />
        </el-form-item>
        <el-form-item label="实际归还日期" prop="sjreturn">
          <el-date-picker
              class="flex-1 !flex"
              v-model="formData.sjreturn"
              clearable
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="选择实际归还日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="实际归还人" prop="returnee">
          <el-input v-model="formData.returnee" clearable placeholder="请输入实际归还人" />
        </el-form-item>
        <el-form-item label="附件" prop="annex">
          <material-picker v-model="formData.annex" />
        </el-form-item>

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

<script lang="ts" setup name="applyWithSealEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiApplyWithSealAdd, apiApplyWithSealEdit, apiApplyWithSealDetail } from '@/api/apply_with_seal'
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: '',
  types: '',
  filetype: '',
  ridingseam: '',
  name: '',
  frequency: '',
  limit: '',
  depar: '',
  stampman: '',
  project: '',
  principal: '',
  content: '',
  loan: '',
  borrow: '',
  returndate: '',
  applicant: '',
  date: '',
  sjborrow: '',
  sjborrower: '',
  sjreturn: '',
  returnee: '',
  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 apiApplyWithSealDetail({
    id: row.id
  })
  setFormData(data)
}


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