<template>
  <div class="dialog-box" style="position: relative">
    <el-scrollbar class="custom-scroll content-box">
      <el-form
        ref="formEditJobProjectRef"
        :model="formData"
        :size="layoutStore.defaultFormItemSize"
        :rules="rules"
        label-width="120px"
        label-position="right"
        @submit.prevent
      >
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="出国劳务标题" prop="JobProject.jobTitle">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobTitle"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="100"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属分类" prop="JobProject.belongTab">
              <el-select
                class="input-item"
                v-model="formData.JobProject.belongTab"
                placeholder=""
                :clearable="true"
                :filterable="true"
                @visible-change="belongTabWidget.onVisibleChange"
              >
                <el-option
                  v-for="item in belongTabWidgetDropdownList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作国家或地区" prop="JobProject.jobArea">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobArea"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="300"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="城市(二级)" prop="JobProject.city">
              <el-input
                class="input-item"
                v-model="formData.JobProject.city"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="255"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="详细地址" prop="JobProject.address">
              <el-input
                class="input-item"
                v-model="formData.JobProject.address"
                type="textarea"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="255"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="出国劳务简述" prop="JobProject.remarks">
              <el-input
                class="input-item"
                v-model="formData.JobProject.remarks"
                type="textarea"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="300"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="标签" prop="JobProject.jobLabel">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobLabel"
                type="textarea"
                placeholder="多个标签请以&分割"
                :clearable="true"
                :show-word-limit="false"
                maxlength="40"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="招聘人数" prop="JobProject.jobPeople">
              <el-input-number
                class="input-item"
                v-model="formData.JobProject.jobPeople"
                placeholder=""
                :clearable="true"
                :step="1"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="招聘部门" prop="JobProject.dept">
              <el-input
                class="input-item"
                v-model="formData.JobProject.dept"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="255"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工资上限" prop="JobProject.salaryCap">
              <el-input-number
                class="input-item"
                v-model="formData.JobProject.salaryCap"
                placeholder=""
                :clearable="true"
                :step="1"
                :precision="2"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工资下限" prop="JobProject.salaryFloor">
              <el-input-number
                class="input-item"
                v-model="formData.JobProject.salaryFloor"
                placeholder=""
                :clearable="true"
                :step="1"
                :precision="2"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄上限" prop="JobProject.ageCap">
              <el-input-number
                class="input-item"
                v-model="formData.JobProject.ageCap"
                placeholder=""
                :clearable="true"
                :step="1"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄下限" prop="JobProject.ageFloor">
              <el-input-number
                class="input-item"
                v-model="formData.JobProject.ageFloor"
                placeholder=""
                :clearable="true"
                :step="1"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工资单位" prop="JobProject.salaryUnit">
              <el-input
                class="input-item"
                v-model="formData.JobProject.salaryUnit"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别要求" prop="JobProject.sex">
              <el-radio-group
                class="input-item"
                v-model="formData.JobProject.sex"
              >
                <el-radio
                  v-for="item in sexWidgetDropdownList"
                  :key="item.id"
                  :label="item.id"
                >
                  {{ item.name }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学历要求" prop="JobProject.jobEducation">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobEducation"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="100"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="语言要求" prop="JobProject.jobLanguage">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobLanguage"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="100"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同期限" prop="JobProject.jobContract">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobContract"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="100"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="岗位要求" prop="JobProject.jobRequire">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobRequire"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="300"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="福利待遇" prop="JobProject.jobWelfare">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobWelfare"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="300"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="截至日期" prop="JobProject.jobDeadline">
              <el-date-picker
                class="input-item"
                v-model="formData.JobProject.jobDeadline"
                type="datetime"
                placeholder=""
                :clearable="true"
                format="YYYY-MM-DD HH:mm:ss"
                value-format="YYYY-MM-DD HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作时间" prop="JobProject.jobTime">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobTime"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="300"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="招聘岗位" prop="JobProject.jobPost">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobPost"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="200"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目优势" prop="JobProject.jobAdvantage">
              <el-input
                class="input-item"
                v-model="formData.JobProject.jobAdvantage"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="255"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="劳务项目图" prop="JobProject.jobUrl">
              <el-upload
                class="upload-image-item"
                name="uploadFile"
                :headers="getUploadHeaders"
                :action="getUploadActionUrl('/admin/app/jobProject/upload')"
                :data="{fieldName: 'jobUrl', asImage: true}"
                :on-success="onJobUrlUploadSuccess"
                :on-remove="onJobUrlRemoveFile"
                :on-error="onUploadError"
                :show-file-list="false"
              >
              <div v-if="jobUrlWidgetFileList && jobUrlWidgetFileList[0] != null" style="position: relative">
                <img class="upload-image-show"
                  :src="jobUrlWidgetFileList[0].url" />
                <div class="upload-img-del el-icon-close"
                  @click.stop="onJobUrlRemoveFile(null, null)" />
              </div>
              <i v-else class="el-icon-plus upload-image-item"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人二维码" prop="JobProject.contactCode">
              <el-upload
                class="upload-image-item"
                name="uploadFile"
                :headers="getUploadHeaders"
                :action="getUploadActionUrl('/admin/app/jobProject/upload')"
                :data="{fieldName: 'contactCode', asImage: true}"
                :on-success="onContactCodeUploadSuccess"
                :on-remove="onContactCodeRemoveFile"
                :on-error="onUploadError"
                :show-file-list="false"
              >
              <div v-if="contactCodeWidgetFileList && contactCodeWidgetFileList[0] != null" style="position: relative">
                <img class="upload-image-show"
                  :src="contactCodeWidgetFileList[0].url" />
                <div class="upload-img-del el-icon-close"
                  @click.stop="onContactCodeRemoveFile(null, null)" />
              </div>
              <i v-else class="el-icon-plus upload-image-item"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="劳务图集" prop="JobProject.jobFile.fileUrl">
              <el-upload
                class="upload-image-item upload-image-multi"
                name="uploadFile"
                :headers="getUploadHeaders"
                :action="getUploadActionUrl('/admin/app/jobFile/upload')"
                :data="{fieldName: 'fileUrl', asImage: true}"
                :on-success="onFileUrlUploadSuccess"
                :on-remove="onFileUrlRemoveFile"
                :on-error="onUploadError"
                :on-exceed="onUploadLimit"
                list-type="picture-card"
                :file-list="fileUrlWidgetFileList"
                :limit="fileUrlWidgetMaxCount"
                :show-file-list="true"
              >
              <i class="el-icon-plus upload-image-item"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否热点" prop="JobProject.isHotspot">
              <el-select
                class="input-item"
                v-model="formData.JobProject.isHotspot"
                placeholder=""
                :clearable="true"
                :filterable="true"
                @visible-change="isHotspotWidget.onVisibleChange"
              >
                <el-option
                  v-for="item in isHotspotWidgetDropdownList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示顺序" prop="JobProject.showOrder">
              <el-input-number
                class="input-item"
                v-model="formData.JobProject.showOrder"
                placeholder=""
                :clearable="true"
                :step="1"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="JobProject.enable">
              <el-select
                class="input-item"
                v-model="formData.JobProject.enable"
                placeholder=""
                :clearable="true"
                :filterable="true"
                @visible-change="enableWidget.onVisibleChange"
              >
                <el-option
                  v-for="item in enableWidgetDropdownList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="浏览量" prop="JobProject.pageViewCount">
              <el-input-number
                class="input-item"
                v-model="formData.JobProject.pageViewCount"
                placeholder=""
                :clearable="true"
                :step="1"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收藏量" prop="JobProject.favouriteCount">
              <el-input-number
                class="input-item"
                v-model="formData.JobProject.favouriteCount"
                placeholder=""
                :clearable="true"
                :step="1"
                :controls="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否急聘" prop="JobProject.isUrgent">
              <el-radio-group
                class="input-item"
                v-model="formData.JobProject.isUrgent"
              >
                <el-radio
                  v-for="item in isUrgentWidgetDropdownList"
                  :key="item.id"
                  :label="item.id"
                >
                  {{ item.name }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="富文本内容" class="rich-input" prop="JobProject.jobContent.content">
              <rich-editor
                ref="content"
                class="input-item"
                v-model="formData.JobProject.jobContent.content"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-scrollbar>
    <el-row class="footer-box" type="flex" justify="end" align="middle">
      <el-button :size="layoutStore.defaultFormItemSize" @click="onCancel()">取消</el-button>
      <el-button :size="layoutStore.defaultFormItemSize" type="primary" @click="onSubmitJobProjectClick()">保存</el-button>
    </el-row>
  </div>
</template>

<script lang="ts">
export default {
  name: 'formEditJobProject',
};
</script>

<script setup lang="ts">
import { DialogProp } from '@/components/Dialog/types';
import { VxeColumn, VxeTable } from 'vxe-table';
import { ANY_OBJECT } from '@/types/generic';
import { DictData, DictionaryBase } from '@/common/staticDict/types';
import { ElMessage, ElMessageBox, UploadFile } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import { useCommon } from '@/common/hooks/useCommon';
import { useLayoutStore, useStaticDictStore } from '@/store';
import { useDownload } from '@/common/hooks/useDownload';
import { useDropdown } from '@/common/hooks/useDropdown';
import { DropdownOptions, ListData } from '@/common/types/list';
import { useTable } from '@/common/hooks/useTable';
import { TableOptions } from '@/common/types/pagination';
import { useUpload } from '@/common/hooks/useUpload';
import { useUploadWidget } from '@/common/hooks/useUploadWidget';
import { DictionaryController } from '@/api/system';
import { treeDataTranslate, findItemFromList, findTreeNodePath, findTreeNode } from '@/common/utils';
import { JobFileData } from '@/api/generated/jobFileController';
import { JobContentData } from '@/api/generated/jobContentController';
import { JobProjectData } from '@/api/generated/jobProjectController';
import { JobFileController, JobContentController, JobProjectController } from '@/api/generated';

const router = useRouter();
const route = useRoute();
const layoutStore = useLayoutStore();
const { downloadFile } = useDownload();
const { getUploadHeaders, getUploadActionUrl, fileListToJson, parseUploadData, getPictureList } = useUpload();
const { 
  Delete,
  Search,
  Edit,
  Plus,
  Refresh,
  Picture,
  Dialog,
  mainContextHeight,
  clientHeight,
  checkPermCodeExist,
  parseParams,
  parseArrayParams,
  formatDateByStatsType,
  getDateRangeFilter,
} = useCommon();
// 静态字典
const { staticDict: StaticDict } = useStaticDictStore();

const props = withDefaults(
  defineProps<{
    subPage?: number | string | boolean;
    id?: ANY_OBJECT;
    saveOnSubmit?: boolean;
    rowData?: ANY_OBJECT;
    // 当使用Dialog.show弹出组件时，须定义该prop属性，以便对dialog进行回调
    dialog?: DialogProp<ANY_OBJECT[]>;
  }>(),
  {
    subPage: 0,
    id: undefined,
    saveOnSubmit: true,
    rowData: undefined,
    dialog: undefined,
  },
);

const formEditJobProjectRef = ref();
// 表单数据定义
type FormEditJobProjectData = {
  JobProject: JobProjectData;
};
// 表单数据
const formData = reactive<FormEditJobProjectData>({
  JobProject: {
    // 主键id
    id: undefined,
    // 出国劳务标题
    jobTitle: undefined,
    // 标签
    jobLabel: undefined,
    // 所属分类
    belongTab: undefined,
    // 工作国家或地区
    jobArea: undefined,
    // 招聘人数
    jobPeople: undefined,
    // 学历要求
    jobEducation: undefined,
    // 语言要求
    jobLanguage: undefined,
    // 合同期限
    jobContract: undefined,
    // 截至日期
    jobDeadline: undefined,
    // 岗位要求
    jobRequire: undefined,
    // 福利待遇
    jobWelfare: undefined,
    // 工作时间
    jobTime: undefined,
    // 招聘岗位
    jobPost: undefined,
    // 项目优势
    jobAdvantage: undefined,
    // 劳务图片
    jobUrl: undefined,
    // 是否热点
    isHotspot: undefined,
    // 显示顺序
    showOrder: undefined,
    // 是否启用
    enable: undefined,
    // 出国劳务简述
    remarks: undefined,
    // 创建用户
    createUserId: undefined,
    // 创建时间
    createTime: undefined,
    // 更新用户
    updateUserId: undefined,
    // 更新时间
    updateTime: undefined,
    // 删除标记(1: 正常 -1: 已删除)
    dataState: undefined,
    // 工资上限
    salaryCap: undefined,
    // 工资下限
    salaryFloor: undefined,
    // 工资单位
    salaryUnit: undefined,
    // 城市(二级)
    city: undefined,
    // 详细地址
    address: undefined,
    // 浏览量
    pageViewCount: undefined,
    // 招聘部门
    dept: undefined,
    // 性别要求(1:男 2:女 3:不限)
    sex: undefined,
    // 年龄上限
    ageCap: undefined,
    // 年龄下限
    ageFloor: undefined,
    // 收藏量
    favouriteCount: undefined,
    // 是否急聘(0:否 1:是)
    isUrgent: undefined,
    // 上传的联系二维码
    contactCode: undefined,
    jobFile: {
      // 主键id
      id: undefined,
      // 文件名称
      fileName: undefined,
      // 文件大小
      fileSize: undefined,
      // 文件类型
      fileType: undefined,
      // 文件存储地址
      fileUrl: undefined,
      // 上传时间
      uploadTime: undefined,
      // 创建用户
      createUserId: undefined,
      // 创建时间
      createTime: undefined,
      // 更新用户
      updateUserId: undefined,
      // 更新时间
      updateTime: undefined,
      // 关联表id
      associationTableId: undefined,
      // 删除标记(1: 正常 -1: 已删除)
      dataState: undefined,
    },
    jobContent: {
      // 主键id
      id: undefined,
      // 富文本内容
      content: undefined,
      // 关联表id
      associationId: undefined,
      // 创建用户
      createUserId: undefined,
      // 创建时间
      createTime: undefined,
      // 更新用户
      updateUserId: undefined,
      // 更新时间
      updateTime: undefined,
      // 删除标记(1: 正常 -1: 已删除)
      dataState: undefined,
    },
  },
},
);
// 表单验证规则
const rules = reactive({
  'JobProject.dept': [
    {required: true, message: '请输入招聘部门', trigger: 'blur'},
    {type: 'string', max: 255, message: '招聘部门长度必须小于255位', trigger: 'blur'}
  ],
  'JobProject.city': [
    {type: 'string', max: 255, message: '城市(二级)长度必须小于255位', trigger: 'blur'}
  ],
  'JobProject.ageCap': [
    {required: true, message: '请输入年龄上限', trigger: 'blur'}
  ],
  'JobProject.jobWelfare': [
    {required: true, message: '请输入福利待遇', trigger: 'blur'},
    {type: 'string', max: 300, message: '福利待遇长度必须小于300位', trigger: 'blur'}
  ],
  'JobProject.jobRequire': [
    {required: true, message: '请输入岗位要求', trigger: 'blur'},
    {type: 'string', max: 300, message: '岗位要求长度必须小于300位', trigger: 'blur'}
  ],
  'JobProject.jobPost': [
    {required: true, message: '请输入招聘岗位', trigger: 'blur'},
    {type: 'string', max: 200, message: '招聘岗位长度必须小于200位', trigger: 'blur'}
  ],
  'JobProject.salaryUnit': [
    {type: 'string', max: 255, message: '工资单位长度必须小于255位', trigger: 'blur'}
  ],
  'JobProject.jobTime': [
    {required: true, message: '请输入工作时间', trigger: 'blur'},
    {type: 'string', max: 300, message: '工作时间长度必须小于300位', trigger: 'blur'}
  ],
  'JobProject.jobLanguage': [
    {required: true, message: '请输入语言要求', trigger: 'blur'},
    {type: 'string', max: 100, message: '语言要求长度必须小于100位', trigger: 'blur'}
  ],
  'JobProject.jobEducation': [
    {required: true, message: '请输入学历要求', trigger: 'blur'},
    {type: 'string', max: 100, message: '学历要求长度必须小于100位', trigger: 'blur'}
  ],
  'JobProject.jobDeadline': [
    {required: true, message: '请输入截至日期', trigger: 'blur'},
    {type: 'string', max: 100, message: '截至日期长度必须小于100位', trigger: 'blur'}
  ],
  'JobProject.jobContract': [
    {required: true, message: '请输入合同期限', trigger: 'blur'},
    {type: 'string', max: 100, message: '合同期限长度必须小于100位', trigger: 'blur'}
  ],
  'JobProject.pageViewCount': [
  ],
  'JobProject.jobContent.content': [
    {required: true, message: '请输入富文本内容', trigger: 'blur'}
  ],
  'JobProject.salaryFloor': [
    {required: true, message: '请输入工资下限', trigger: 'blur'}
  ],
  'JobProject.jobAdvantage': [
    {type: 'string', max: 255, message: '项目优势长度必须小于255位', trigger: 'blur'}
  ],
  'JobProject.ageFloor': [
    {required: true, message: '请输入年龄下限', trigger: 'blur'}
  ],
  'JobProject.address': [
    {type: 'string', max: 255, message: '详细地址长度必须小于255位', trigger: 'blur'}
  ],
  'JobProject.salaryCap': [
    {required: true, message: '请输入工资上限', trigger: 'blur'}
  ],
  'JobProject.jobFile.fileUrl': [
  ],
  'JobProject.sex': [
    {required: true, message: '请输入性别要求', trigger: 'blur'}
  ],
  'JobProject.isUrgent': [
  ],
  'JobProject.jobTitle': [
    {required: true, message: '请输入出国劳务标题', trigger: 'blur'},
    {type: 'string', max: 100, message: '出国劳务标题长度必须小于100位', trigger: 'blur'}
  ],
  'JobProject.belongTab': [
    {required: true, message: '请输入所属分类', trigger: 'blur'}
  ],
  'JobProject.jobLabel': [
    {required: true, message: '请输入标签', trigger: 'blur'},
    {type: 'string', max: 255, message: '标签长度必须小于255位', trigger: 'blur'}
  ],
  'JobProject.remarks': [
    {type: 'string', max: 300, message: '出国劳务简述长度必须小于300位', trigger: 'blur'}
  ],
  'JobProject.enable': [
    {required: true, message: '请输入是否启用', trigger: 'blur'}
  ],
  'JobProject.jobPeople': [
    {required: true, message: '请输入招聘人数', trigger: 'blur'}
  ],
  'JobProject.jobUrl': [
    {required: true, message: '请输入劳务项目图', trigger: 'blur'}
  ],
  'JobProject.jobArea': [
    {type: 'string', max: 300, message: '工作国家或地区长度必须小于300位', trigger: 'blur'}
  ],
  'JobProject.showOrder': [
    {required: true, message: '请输入显示顺序', trigger: 'blur'}
  ],
  'JobProject.favouriteCount': [
  ],
  'JobProject.isHotspot': [
    {required: true, message: '请输入是否热点', trigger: 'blur'}
  ],
  // 'JobProject.contactCode': [
  //   {required: true, message: '请输入联系人二维码', trigger: 'blur'}
  // ],
});

const onCancel = () => {
  if (props.dialog) {
    props.dialog.cancel();
  }
};

const isEdit = computed(() => {
  return props.saveOnSubmit ? props.id != null : props.rowData != null;
});

// 初始化页面数据
const loadJobProjectData = () => {
  return new Promise<void>((resolve, reject) => {
    if (!isEdit.value) {
      resolve();
      return;
    }
    if (!props.saveOnSubmit && props.rowData != null) {
      formData.JobProject = JSON.parse(JSON.stringify(props.rowData));
      resolve();
      return;
    }
    let params: ANY_OBJECT = {
      id: props.id
    };
    JobProjectController.view(params).then(res => {
      formData.JobProject = { ...res.data };
      if (formData.JobProject.jobFile == null) {
        formData.JobProject.jobFile = {
          id: undefined,
          fileName: undefined,
          fileSize: undefined,
          fileType: undefined,
          fileUrl: undefined,
          uploadTime: undefined,
          createUserId: undefined,
          createTime: undefined,
          updateUserId: undefined,
          updateTime: undefined,
          associationTableId: undefined,
          dataState: undefined
        };
      }
      if (formData.JobProject.jobContent == null) {
        formData.JobProject.jobContent = {
          id: undefined,
          content: undefined,
          associationId: undefined,
          createUserId: undefined,
          createTime: undefined,
          updateUserId: undefined,
          updateTime: undefined,
          dataState: undefined
        };
      }
      resolve();
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 所属分类下拉数据获取函数
 */
const loadBelongTabDropdownList = (): Promise<ListData<DictData>> => {
  return new Promise((resolve, reject) => {
    const params = {
          parentId:16
    };
    DictionaryController.dictDirectoryInfoByParentId(params).then(res => {
      resolve({
        dataList: res.getList(),
      });
    }).catch(e => {
      reject(e);
    });
  });
};
// 所属分类配置参数
const belongTabOptions: DropdownOptions<DictData> = {
  loadData: loadBelongTabDropdownList,
  isTree: false,
};
// 所属分类下拉组件
const belongTabWidget = useDropdown(belongTabOptions);
const { dropdownList: belongTabWidgetDropdownList } = belongTabWidget
/**
 * 性别要求下拉数据获取函数
 */
const loadSexDropdownList = (): Promise<ListData<DictData>> => {
  return new Promise((resolve, reject) => {
    DictionaryController.dictGlobalDict({ dictCode: 'JobSex', itemIdType: 'String' }).then(res => {
      resolve({
        dataList: res.getList(),
      });
    }).catch(e => {
      reject(e);
    });
  });
};
// 性别要求配置参数
const sexOptions: DropdownOptions<DictData> = {
  loadData: loadSexDropdownList,
  isTree: false,
};
// 性别要求下拉组件
const sexWidget = useDropdown(sexOptions);
const { dropdownList: sexWidgetDropdownList } = sexWidget
/**
 * 联系人二维码上传成功
 */
const onContactCodeUploadSuccess = (
  response: ANY_OBJECT,
  file: UploadFile,
  fileList: UploadFile[],
) => {
  if (response.success) {
    file.downloadUri = response.data.downloadUri;
    file.filename = response.data.filename;
    file.uploadPath = response.data.uploadPath;
    file.url = URL.createObjectURL(file.raw);
    contactCodeWidget.onFileChange(file, fileList);
    formData.JobProject.contactCode = fileListToJson(contactCodeWidgetFileList.value);
  } else {
    ElMessage.error(response.message);
  }
};
/**
 * 移除联系人二维码
 */
const onContactCodeRemoveFile = (file: UploadFile | null, fileList: UploadFile[] | null) => {
  contactCodeWidget.onFileChange(file, fileList);
  formData.JobProject.contactCode = fileListToJson(contactCodeWidgetFileList.value);
};
// 联系人二维码上传文件组件
const contactCodeWidget = useUploadWidget(1);
const { fileList: contactCodeWidgetFileList, maxCount: contactCodeWidgetMaxCount } = contactCodeWidget;
/**
 * 劳务项目图上传成功
 */
const onJobUrlUploadSuccess = (
  response: ANY_OBJECT,
  file: UploadFile,
  fileList: UploadFile[],
) => {
  if (response.success) {
    file.downloadUri = response.data.downloadUri;
    file.filename = response.data.filename;
    file.uploadPath = response.data.uploadPath;
    file.url = URL.createObjectURL(file.raw);
    jobUrlWidget.onFileChange(file, fileList);
    formData.JobProject.jobUrl = fileListToJson(jobUrlWidgetFileList.value);
  } else {
    ElMessage.error(response.message);
  }
};
/**
 * 移除劳务项目图
 */
const onJobUrlRemoveFile = (file: UploadFile | null, fileList: UploadFile[] | null) => {
  jobUrlWidget.onFileChange(file, fileList);
  formData.JobProject.jobUrl = fileListToJson(jobUrlWidgetFileList.value);
};
// 劳务项目图上传文件组件
const jobUrlWidget = useUploadWidget(1);
const { fileList: jobUrlWidgetFileList, maxCount: jobUrlWidgetMaxCount } = jobUrlWidget;
/**
 * 劳务图集上传成功
 */
const onFileUrlUploadSuccess = (
  response: ANY_OBJECT,
  file: UploadFile,
  fileList: UploadFile[],
) => {
  if (response.success) {
    file.downloadUri = response.data.downloadUri;
    file.filename = response.data.filename;
    file.uploadPath = response.data.uploadPath;
    file.url = URL.createObjectURL(file.raw);
    fileUrlWidget.onFileChange(file, fileList);
    formData.JobProject.jobFile.fileUrl = fileListToJson(fileUrlWidgetFileList.value);
  } else {
    ElMessage.error(response.message);
  }
};
/**
 * 移除劳务图集
 */
const onFileUrlRemoveFile = (file: UploadFile | null, fileList: UploadFile[] | null) => {
  fileUrlWidget.onFileChange(file, fileList);
  formData.JobProject.jobFile.fileUrl = fileListToJson(fileUrlWidgetFileList.value);
};
// 劳务图集上传文件组件
const fileUrlWidget = useUploadWidget(5);
const { fileList: fileUrlWidgetFileList, maxCount: fileUrlWidgetMaxCount } = fileUrlWidget;
/**
 * 是否热点下拉数据获取函数
 */
const loadIsHotspotDropdownList = (): Promise<ListData<DictData>> => {
  return Promise.resolve({
    dataList: StaticDict.Hotspot.getList(),
  });
};
// 是否热点配置参数
const isHotspotOptions: DropdownOptions<DictData> = {
  loadData: loadIsHotspotDropdownList,
  isTree: false,
};
// 是否热点下拉组件
const isHotspotWidget = useDropdown(isHotspotOptions);
const { dropdownList: isHotspotWidgetDropdownList } = isHotspotWidget
/**
 * 是否启用下拉数据获取函数
 */
const loadEnableDropdownList = (): Promise<ListData<DictData>> => {
  return Promise.resolve({
    dataList: StaticDict.Enable.getList(),
  });
};
// 是否启用配置参数
const enableOptions: DropdownOptions<DictData> = {
  loadData: loadEnableDropdownList,
  isTree: false,
};
// 是否启用下拉组件
const enableWidget = useDropdown(enableOptions);
const { dropdownList: enableWidgetDropdownList } = enableWidget
/**
 * 是否急聘下拉数据获取函数
 */
const loadIsUrgentDropdownList = (): Promise<ListData<DictData>> => {
  return new Promise((resolve, reject) => {
    DictionaryController.dictGlobalDict({ dictCode: 'IsUrgent', itemIdType: 'Integer' }).then(res => {
      resolve({
        dataList: res.getList(),
      });
    }).catch(e => {
      reject(e);
    });
  });
};
// 是否急聘配置参数
const isUrgentOptions: DropdownOptions<DictData> = {
  loadData: loadIsUrgentDropdownList,
  isTree: false,
};
// 是否急聘下拉组件
const isUrgentWidget = useDropdown(isUrgentOptions);
const { dropdownList: isUrgentWidgetDropdownList } = isUrgentWidget
const content = ref();
const onUploadError = () => {
  ElMessage.error('文件上传失败');
};
const onUploadLimit = () => {
  ElMessage.error('已经超出最大上传个数限制');
};
const refreshFormEditJobProject = () => {
  // 刷新段落
};
/**
 * 重置过滤值
 */
const resetFormEditJobProject = () => {
  refreshFormEditJobProject();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormEditJobProject();
};
/**
 * 保存
 */
const onSubmitJobProjectClick = () => {
  // 更新富文本字段值
  formData.JobProject.jobContent.content = content.value?.getHtml();
  formEditJobProjectRef.value.validate((valid) => {
    if (!valid) return;
    // 级联操作
    if (!props.saveOnSubmit) {
      let formData = {
        ...formData.JobProject
      };
    formData.belongTabDictMap = findItemFromList(belongTabWidgetDropdownList, formData.belongTab, 'id');
    formData.sexDictMap = findItemFromList(sexWidgetDropdownList, formData.sex, 'id');
    formData.isHotspotDictMap = findItemFromList(isHotspotWidgetDropdownList, formData.isHotspot, 'id');
    formData.enableDictMap = findItemFromList(enableWidgetDropdownList, formData.enable, 'id');
    formData.isUrgentDictMap = findItemFromList(isUrgentWidgetDropdownList, formData.isUrgent, 'id');
  props.dialog?.submit(formData);
      return;
    }
    let params: ANY_OBJECT = {
      jobContentDto: formData.JobProject.jobContent,
      jobFileDto: formData.JobProject.jobFile,
      jobProjectDto: {
        contactCode: formData.JobProject.contactCode,
        id: formData.JobProject.id,
        salaryCap: formData.JobProject.salaryCap,
        salaryFloor: formData.JobProject.salaryFloor,
        salaryUnit: formData.JobProject.salaryUnit,
        city: formData.JobProject.city,
        address: formData.JobProject.address,
        pageViewCount: formData.JobProject.pageViewCount,
        dept: formData.JobProject.dept,
        sex: formData.JobProject.sex,
        ageCap: formData.JobProject.ageCap,
        ageFloor: formData.JobProject.ageFloor,
        favouriteCount: formData.JobProject.favouriteCount,
        isUrgent: formData.JobProject.isUrgent,
        jobTitle: formData.JobProject.jobTitle,
        jobLabel: formData.JobProject.jobLabel,
        belongTab: formData.JobProject.belongTab,
        jobArea: formData.JobProject.jobArea,
        jobPeople: formData.JobProject.jobPeople,
        jobEducation: formData.JobProject.jobEducation,
        jobLanguage: formData.JobProject.jobLanguage,
        jobContract: formData.JobProject.jobContract,
        jobDeadline: formData.JobProject.jobDeadline,
        jobRequire: formData.JobProject.jobRequire,
        jobWelfare: formData.JobProject.jobWelfare,
        jobTime: formData.JobProject.jobTime,
        jobPost: formData.JobProject.jobPost,
        jobAdvantage: formData.JobProject.jobAdvantage,
        jobUrl: formData.JobProject.jobUrl,
        isHotspot: formData.JobProject.isHotspot,
        showOrder: formData.JobProject.showOrder,
        enable: formData.JobProject.enable,
        remarks: formData.JobProject.remarks,
        createUserId: formData.JobProject.createUserId,
        createTime: formData.JobProject.createTime,
        updateUserId: formData.JobProject.updateUserId,
        updateTime: formData.JobProject.updateTime,
      }
    };

    let httpCall = isEdit.value ? JobProjectController.update : JobProjectController.add;
    httpCall(params).then(res => {
      ElMessage.success('保存成功');
      props.dialog?.submit();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  });
};
const formInit = () => {
  loadJobProjectData().then(res => {
    belongTabWidget.onVisibleChange(true).catch(e => {console.error(e)});
    sexWidget.onVisibleChange(true).catch(e => {console.error(e)});
    let contactCodeDownloadParams = {
      id: formData.JobProject.id,
      fieldName: 'contactCode',
      asImage: true
    };
    contactCodeWidgetFileList.value = parseUploadData(formData.JobProject.contactCode, contactCodeDownloadParams);
    let jobUrlDownloadParams = {
      id: formData.JobProject.id,
      fieldName: 'jobUrl',
      asImage: true
    };
    jobUrlWidgetFileList.value = parseUploadData(formData.JobProject.jobUrl, jobUrlDownloadParams);
    let fileUrlDownloadParams = {
      id: formData.JobProject.jobFile.id,
      fieldName: 'fileUrl',
      asImage: true
    };
    fileUrlWidgetFileList.value = parseUploadData(formData.JobProject.jobFile.fileUrl, fileUrlDownloadParams);
    isHotspotWidget.onVisibleChange(true).catch(e => {console.error(e)});
    enableWidget.onVisibleChange(true).catch(e => {console.error(e)});
    isUrgentWidget.onVisibleChange(true).catch(e => {console.error(e)});
    if (isEdit.value) refreshFormEditJobProject();
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};

onMounted(() => {
  formInit();
});
</script>
