<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="BgPartymatterForm">
          <a-row>
						<a-col :span="12">
							<a-form-item label="年份" v-bind="validateInfos.year" id="BgPartymatterForm-year" name="year">
								<a-input v-model:value="formData.year" placeholder="请输入年份"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
            <a-col :span="12">
              <a-form-item label="密级" v-bind="validateInfos.secretLevel" id="BgPartymatterForm-secretLevel" name="secretLevel">
                <a-input v-model:value="formData.secretLevel" placeholder="请输入密级"  allow-clear ></a-input>
              </a-form-item>
            </a-col>
						<a-col :span="12">
							<a-form-item label="序号" v-bind="validateInfos.number" id="BgPartymatterForm-number" name="number">
								<a-input v-model:value="formData.number" placeholder="请输入序号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="会议时间" v-bind="validateInfos.matterTime" id="BgPartymatterForm-matterTime" name="matterTime">
								<a-date-picker placeholder="请选择会议时间"  v-model:value="formData.matterTime" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="事项名称" v-bind="validateInfos.title" id="BgPartymatterForm-title" name="title">
								<a-input v-model:value="formData.title" placeholder="请输入事项名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="会议决议" v-bind="validateInfos.content" id="BgPartymatterForm-content" name="content">
								<a-textarea v-model:value="formData.content" :rows="4" placeholder="请输入会议决议" />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="事项目录编码" v-bind="validateInfos.matterCode" id="BgPartymatterForm-matterCode" name="matterCode">
								<a-input v-model:value="formData.matterCode" placeholder="请输入事项目录编码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="责任部门" v-bind="validateInfos.responDeptid" id="BgPartymatterForm-responDeptid" name="responDeptid">
								<j-select-dept v-model:value="formData.responDeptid"       :multiple="true" checkStrictly  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="实际执行情况" v-bind="validateInfos.actualExect" id="BgPartymatterForm-actualExect" name="actualExect">
								<a-input v-model:value="formData.actualExect" placeholder="请输入实际执行情况"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="完成状态" v-bind="validateInfos.bpmStatus" id="BgPartymatterForm-bpmStatus" name="bpmStatus">
								<a-input v-model:value="formData.bpmStatus" placeholder="请输入完成状态"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="督办次数" v-bind="validateInfos.superviseCount" id="BgPartymatterForm-superviseCount" name="superviseCount">
								<a-input v-model:value="formData.superviseCount" placeholder="请输入督办次数"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="要求完成日期" v-bind="validateInfos.deadline" id="BgPartymatterForm-deadline" name="deadline">
								<a-date-picker placeholder="请选择要求完成日期"  v-model:value="formData.deadline" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="紧急程度" v-bind="validateInfos.urgencyLevel" id="BgPartymatterForm-urgencyLevel" name="urgencyLevel">
								<a-input v-model:value="formData.urgencyLevel" placeholder="请输入紧急程度"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="是否需要所办领导审批" v-bind="validateInfos.isNeedAppro" id="BgPartymatterForm-isNeedAppro" name="isNeedAppro">
								<j-switch v-model:value="formData.isNeedAppro"  ></j-switch>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="是否抄送所领导" v-bind="validateInfos.isNeedSuoleader" id="BgPartymatterForm-isNeedSuoleader" name="isNeedSuoleader">
								<j-switch v-model:value="formData.isNeedSuoleader"  ></j-switch>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="抄送所领导" v-bind="validateInfos.suoleaderid" id="BgPartymatterForm-suoleaderid" name="suoleaderid">
						<j-select-user v-model:value="formData.suoleaderid"      allow-clear />
							</a-form-item>
						</a-col>
            <a-col v-if="showFlowSubmitButton" :span="24" style="width: 100%;text-align: center;">
              <a-button preIcon="ant-design:check-outlined" style="width: 126px" type="primary" @click="submitForm">提 交</a-button>
            </a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JSwitch from '/@/components/Form/src/jeecg/components/JSwitch.vue';
  import JSelectDept from '/@/components/Form/src/jeecg/components/JSelectDept.vue';
  import JSelectUser from '/@/components/Form/src/jeecg/components/JSelectUser.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../BgPartymatter.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';

  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({}) },
    formBpm: { type: Boolean, default: true }
  });
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
        year: '',
        number: '',
        matterTime: '',
        title: '',
        content: '',
        matterCode: '',
        responDeptid: '',
        actualExect: '',
        bpmStatus: '',
        superviseCount: '',
        deadline: '',
        urgencyLevel: '',
        isNeedAppro: '',
        isNeedSuoleader: '',
        suoleaderid: '',
        delFlag: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });
  const formRef = ref();
  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  onMounted(()=>{
    initFormData();
  });
  //渲染流程表单数据
  const queryByIdUrl = '/bgpartymatter/bgPartymatter/queryById';
  async function initFormData(){
    if(props.formBpm === true){
      let params = {id: props.formData.dataId};
      const data = await defHttp.get({url: queryByIdUrl, params});
      //设置表单的值
      edit({...data});
    }
  }
  // 是否显示提交按钮
  const showFlowSubmitButton = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return true
      }
    }
    return false
  });

  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData,tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
