<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="124px"
      v-loading="formLoading"
    >
      <el-form-item label="上级ID" prop="parentId">
        <el-input v-model="formData.parentId" placeholder="请输入上级ID,若为顶级项目则为0" />
      </el-form-item>
      <el-form-item label="关联图层名" prop="associateLayerName">
        <el-input v-model="formData.associateLayerName" placeholder="请输入关联的矢量图层名" />
      </el-form-item>
      <el-form-item label="关联图层ID" prop="associateLayerId">
        <el-input v-model="formData.associateLayerId" placeholder="请输入关联的矢量图层ID" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="formData.sort" placeholder="请输入项目排序" />
      </el-form-item>
      <el-form-item label="项目名称" prop="itemName">
        <el-input v-model="formData.itemName" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="项目编号" prop="itemId">
        <el-input v-model="formData.itemId" placeholder="请输入项目编号" />
      </el-form-item>
      <el-form-item label="执行单位" prop="implementedBy">
        <el-input v-model="formData.implementedBy" placeholder="请输入执行单位" />
      </el-form-item>
      <el-form-item label="项目描述" prop="itemDescribe">
        <el-input
          v-model="formData.itemDescribe"
          :autosize="{ minRows: 2, maxRows: 4 }"
          type="textarea"
          placeholder="请输入项目描述"
        />
      </el-form-item>
      <el-form-item label="项目相关附件" prop="itemFile">
        <UploadFile v-model="formData.itemFile" />
      </el-form-item>
      <el-form-item label="项目负责人" prop="itemUserName">
        <el-input v-model="formData.itemUserName" placeholder="请输入项目负责人" />
      </el-form-item>
      <el-form-item label="项目总预算" prop="itemBudget">
        <el-input v-model="formData.itemBudget" placeholder="请输入项目总预算" />
      </el-form-item>
      <el-form-item label="已经分配资金" prop="allocatedFunds">
        <el-input v-model="formData.allocatedFunds" placeholder="请输入已经分配资金" />
      </el-form-item>
      <el-form-item label="项目获得资金" prop="itemGetFunds">
        <el-input v-model="formData.itemGetFunds" placeholder="请输入项目获得资金" />
      </el-form-item>
      <el-form-item label="项目开始时间" prop="itemStartTime">
        <el-date-picker
          v-model="formData.itemStartTime"
          type="date"
          value-format="x"
          placeholder="选择项目开始时间"
        />
      </el-form-item>
      <el-form-item label="项目预估结束时间" prop="itemEstimateEndTime">
        <el-date-picker
          v-model="formData.itemEstimateEndTime"
          type="date"
          value-format="x"
          placeholder="选择项目预估结束时间"
        />
      </el-form-item>
      <el-form-item label="项目实际结束时间" prop="itemActualEndTime">
        <el-date-picker
          v-model="formData.itemActualEndTime"
          type="date"
          value-format="x"
          placeholder="选择项目实际结束时间"
        />
      </el-form-item>
      <el-form-item label="项目状态" prop="status">
        <el-select
          v-model="formData.status"
          placeholder="请选择项目状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目影响范围" prop="itemInfluenceRange">
        <el-input v-model="formData.itemInfluenceRange" placeholder="请输入项目影响范围" />
      </el-form-item>
      <el-form-item label="项目风险描述" prop="itemRiskDescription">
        <Editor v-model="formData.itemRiskDescription" height="150px" />
      </el-form-item>
      <el-form-item label="地名" prop="placeName">
        <el-input v-model="formData.placeName" placeholder="请输入地名" />
      </el-form-item>
      <el-form-item label="规划用途" prop="planForUse">
        <el-input v-model="formData.planForUse" placeholder="请输入规划用途" />
      </el-form-item>
      <el-form-item label="地块编号" prop="placeId">
        <el-input v-model="formData.placeId" placeholder="请输入地块编号" />
      </el-form-item>
      <el-form-item label="地块类型" prop="placeType">
        <el-select v-model="formData.placeType" placeholder="请选择地块类型">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.LAYER_VISUALIZEMESSAGE_PLACE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产权人或单位名" prop="propertyOwnerNameOrUnitName">
        <el-input v-model="formData.propertyOwnerNameOrUnitName" placeholder="请输入产权人或单位名" />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入联系方式" />
      </el-form-item>
      <el-form-item label="产权证编号" prop="titleDeedNumber">
        <el-input v-model="formData.titleDeedNumber" placeholder="请输入产权证编号" />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入详细地址" />
      </el-form-item>
      <el-form-item label="图层颜色" prop="layerColor">
        <el-input v-model="formData.layerColor" placeholder="请输入矢量图层颜色" />
      </el-form-item>
      <el-form-item label="产权估值" prop="propertyValuation">
        <el-input v-model="formData.propertyValuation" placeholder="请输入产权估值" />
      </el-form-item>
      <el-form-item label="产权简介" prop="propertyRightsIntroduction">
        <el-input v-model="formData.propertyRightsIntroduction" placeholder="请输入产权简介" />
      </el-form-item>
      <el-form-item label="使用权类型" prop="useType">
        <el-select v-model="formData.useType" placeholder="请选择使用权类型">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.LAYER_VISUALIZEMESSAGE_USE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用权开始时间" prop="useStartTime">
        <el-date-picker
          v-model="formData.useStartTime"
          type="date"
          value-format="x"
          placeholder="选择使用权开始时间"
        />
      </el-form-item>
      <el-form-item label="使用权结束时间" prop="useEndTime">
        <el-date-picker
          v-model="formData.useEndTime"
          type="date"
          value-format="x"
          placeholder="选择使用权结束时间"
        />
      </el-form-item>
      <el-form-item label="产权法律诉讼记录" prop="titleProceedingsRecords">
        <el-input v-model="formData.titleProceedingsRecords" placeholder="请输入产权法律诉讼记录" />
      </el-form-item>
      <el-form-item label="产权交易信息" prop="propertyTransactionsInfo">
        <el-input v-model="formData.propertyTransactionsInfo" placeholder="请输入产权交易信息" />
      </el-form-item>
      <el-form-item label="抵押情况" prop="mortgageSituation">
        <el-input v-model="formData.mortgageSituation" placeholder="请输入抵押情况" />
      </el-form-item>
      <el-form-item label="产权人照片上传" prop="propertyOwnerImage">
        <UploadImg v-model="formData.propertyOwnerImage" />
      </el-form-item>
      <el-form-item label="实景照片上传" prop="realImage">
        <UploadImg v-model="formData.realImage" />
      </el-form-item>
      <el-form-item label="图片信息描述" prop="imageDescription">
        <el-input v-model="formData.imageDescription" placeholder="请输入图片信息描述" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { VisualizeMessageApi, VisualizeMessageVO } from '@/api/layer/visualizemessage'
import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";

/** 项目信息表 表单 */
defineOptions({ name: 'VisualizeMessageForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  parentId: undefined,
  associateLayerName: undefined,
  associateLayerId: undefined,
  placeName: undefined,
  sort: undefined,
  itemName: undefined,
  itemId: undefined,
  implementedBy: undefined,
  itemDescribe: undefined,
  itemFile: undefined,
  itemUserName: undefined,
  itemBudget: undefined,
  allocatedFunds: undefined,
  itemGetFunds: undefined,
  itemStartTime: undefined,
  itemEstimateEndTime: undefined,
  itemActualEndTime: undefined,
  status: undefined,
  itemInfluenceRange: undefined,
  itemRiskDescription: "",
  planForUse: undefined,
  placeId: undefined,
  placeType: undefined,
  propertyOwnerNameOrUnitName: undefined,
  phone: undefined,
  titleDeedNumber: undefined,
  address: undefined,
  layerColor: undefined,
  propertyValuation: undefined,
  propertyRightsIntroduction: undefined,
  useType: undefined,
  useStartTime: undefined,
  useEndTime: undefined,
  titleProceedingsRecords: undefined,
  propertyTransactionsInfo: undefined,
  mortgageSituation: undefined,
  propertyOwnerImage: undefined,
  realImage: undefined,
  imageDescription: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await VisualizeMessageApi.getVisualizeMessage(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as VisualizeMessageVO
    if (formType.value === 'create') {
      await VisualizeMessageApi.createVisualizeMessage(data)
      message.success(t('common.createSuccess'))
    } else {
      await VisualizeMessageApi.updateVisualizeMessage(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    parentId: undefined,
    associateLayerName: undefined,
    associateLayerId: undefined,
    placeName: undefined,
    sort: undefined,
    itemName: undefined,
    itemId: undefined,
    implementedBy: undefined,
    itemDescribe: undefined,
    itemFile: undefined,
    itemUserName: undefined,
    itemBudget: undefined,
    allocatedFunds: undefined,
    itemGetFunds: undefined,
    itemStartTime: undefined,
    itemEstimateEndTime: undefined,
    itemActualEndTime: undefined,
    status: undefined,
    itemInfluenceRange: undefined,
    itemRiskDescription: "",
    planForUse: undefined,
    placeId: undefined,
    placeType: undefined,
    propertyOwnerNameOrUnitName: undefined,
    phone: undefined,
    titleDeedNumber: undefined,
    address: undefined,
    layerColor: undefined,
    propertyValuation: undefined,
    propertyRightsIntroduction: undefined,
    useType: undefined,
    useStartTime: undefined,
    useEndTime: undefined,
    titleProceedingsRecords: undefined,
    propertyTransactionsInfo: undefined,
    mortgageSituation: undefined,
    propertyOwnerImage: undefined,
    realImage: undefined,
    imageDescription: undefined,
  }
  formRef.value?.resetFields()
}
</script>
