<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="TaskApprovalOpinionForm">
          <a-row>
						<a-col :span="24">
							<a-form-item label="记录类型：PROCESS流程意见、SUPPLEMENT补充反馈、SYSTEM系统记录" v-bind="validateInfos.recordType" id="TaskApprovalOpinionForm-recordType" name="recordType">
								<a-input v-model:value="formData.recordType" placeholder="请输入记录类型：PROCESS流程意见、SUPPLEMENT补充反馈、SYSTEM系统记录"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="事项任务ID，对应task_task.id" v-bind="validateInfos.taskTaskId" id="TaskApprovalOpinionForm-taskTaskId" name="taskTaskId">
								<a-input v-model:value="formData.taskTaskId" placeholder="请输入事项任务ID，对应task_task.id"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="关联业务ID" v-bind="validateInfos.businessId" id="TaskApprovalOpinionForm-businessId" name="businessId">
								<a-input v-model:value="formData.businessId" placeholder="请输入关联业务ID"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="流程编码" v-bind="validateInfos.flowCode" id="TaskApprovalOpinionForm-flowCode" name="flowCode">
								<a-input v-model:value="formData.flowCode" placeholder="请输入流程编码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="流程实例ID" v-bind="validateInfos.processInstId" id="TaskApprovalOpinionForm-processInstId" name="processInstId">
								<a-input v-model:value="formData.processInstId" placeholder="请输入流程实例ID"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="Flowable任务ID" v-bind="validateInfos.flowTaskId" id="TaskApprovalOpinionForm-flowTaskId" name="flowTaskId">
								<a-input v-model:value="formData.flowTaskId" placeholder="请输入Flowable任务ID"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="流程节点Key" v-bind="validateInfos.taskDefKey" id="TaskApprovalOpinionForm-taskDefKey" name="taskDefKey">
								<a-input v-model:value="formData.taskDefKey" placeholder="请输入流程节点Key"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="流程节点名称" v-bind="validateInfos.taskName" id="TaskApprovalOpinionForm-taskName" name="taskName">
								<a-input v-model:value="formData.taskName" placeholder="请输入流程节点名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="操作类型：START发起、APPROVE办理、REJECT驳回、CALLBACK取回、INVALID作废、FEEDBACK反馈" v-bind="validateInfos.actionType" id="TaskApprovalOpinionForm-actionType" name="actionType">
								<a-input v-model:value="formData.actionType" placeholder="请输入操作类型：START发起、APPROVE办理、REJECT驳回、CALLBACK取回、INVALID作废、FEEDBACK反馈"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="操作结果，如同意、不同意、驳回、发起流程、补充反馈" v-bind="validateInfos.actionResult" id="TaskApprovalOpinionForm-actionResult" name="actionResult">
								<a-input v-model:value="formData.actionResult" placeholder="请输入操作结果，如同意、不同意、驳回、发起流程、补充反馈"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="审批意见或反馈内容" v-bind="validateInfos.opinion" id="TaskApprovalOpinionForm-opinion" name="opinion">
								<a-textarea v-model:value="formData.opinion" :rows="4" placeholder="请输入审批意见或反馈内容" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="办理人账号" v-bind="validateInfos.opUserId" id="TaskApprovalOpinionForm-opUserId" name="opUserId">
								<a-input v-model:value="formData.opUserId" placeholder="请输入办理人账号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="办理人姓名" v-bind="validateInfos.opUserName" id="TaskApprovalOpinionForm-opUserName" name="opUserName">
								<a-input v-model:value="formData.opUserName" placeholder="请输入办理人姓名"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="办理人部门ID" v-bind="validateInfos.opDeptId" id="TaskApprovalOpinionForm-opDeptId" name="opDeptId">
								<a-input v-model:value="formData.opDeptId" placeholder="请输入办理人部门ID"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="办理人部门名称" v-bind="validateInfos.opDeptName" id="TaskApprovalOpinionForm-opDeptName" name="opDeptName">
								<a-input v-model:value="formData.opDeptName" placeholder="请输入办理人部门名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="办理人部门编码" v-bind="validateInfos.opDeptCode" id="TaskApprovalOpinionForm-opDeptCode" name="opDeptCode">
								<a-input v-model:value="formData.opDeptCode" placeholder="请输入办理人部门编码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="办理时间" v-bind="validateInfos.opTime" id="TaskApprovalOpinionForm-opTime" name="opTime">
								<a-date-picker placeholder="请选择办理时间"  v-model:value="formData.opTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</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 { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../TaskApprovalOpinion.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 formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    recordType: '',   
    taskTaskId: '',   
    businessId: '',   
    flowCode: '',   
    processInstId: '',   
    flowTaskId: '',   
    taskDefKey: '',   
    taskName: '',   
    actionType: '',   
    actionResult: '',   
    opinion: '',   
    opUserId: '',   
    opUserName: '',   
    opDeptId: '',   
    opDeptName: '',   
    opDeptCode: '',   
    opTime: '',   
    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({
    recordType: [{ required: true, message: '请输入记录类型：PROCESS流程意见、SUPPLEMENT补充反馈、SYSTEM系统记录!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  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>
