<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { computed, ref, unref } from 'vue';
  import { changeOrRepeal } from '../StPlanMan.api';
  import { formSchema } from '../StPlanManChange.data';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  const handleFlag = ref('');
  const isDetail = ref(false);
  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    //labelWidth: 150,
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    handleFlag.value = data.planFlag;
    isDetail.value = !data?.showFooter;
    if (unref(isUpdate)) {
      if (!unref(isDetail)) {
        if (unref(handleFlag) == '4') {
          await updateSchema({
            label: '变更原因',
            field: 'optDes',
            component: 'InputTextArea',
            itemProps: { labelCol: { xs: { span: 24 }, sm: { span: 3 } }, wrapperCol: { xs: { span: 24 }, sm: { span: 21 } } },
            dynamicRules: ({ model, schema }) => {
              return [{ required: true, message: '请输入变更原因!' }];
            },
            dynamicDisabled: false,
            show: true,
          });
        } else if (unref(handleFlag) == '7') {
          setProps({ disabled: true });
          await updateSchema({
            label: '废止原因',
            field: 'optDes',
            component: 'InputTextArea',
            itemProps: { labelCol: { xs: { span: 24 }, sm: { span: 3 } }, wrapperCol: { xs: { span: 24 }, sm: { span: 21 } } },
            dynamicRules: ({ model, schema }) => {
              return [{ required: true, message: '请输入废止原因!' }];
            },
            dynamicDisabled: false,
            show: true,
          });
        }
        await updateSchema({
          field: 'approveDes',
          show: false,
        });
        data.record['optDes'] = '';
        await setFieldsValue({
          ...data.record,
        });
      } else {
        if (data.record['planFlag'] == '3') {
          await updateSchema({
            field: 'optDes',
            show: false,
          });
          await updateSchema({
            field: 'approveDes',
            show: false,
          });
        } else if (data.record['planFlag'] == '4') {
          await updateSchema({
            label: '变更原因',
            field: 'optDes',
            dynamicDisabled: true,
            show: true,
          });
          await updateSchema({
            field: 'approveDes',
            show: false,
          });
        } else if (data.record['planFlag'] == '7') {
          updateSchema({
            label: '废止原因',
            field: 'optDes',
            dynamicDisabled: true,
            show: true,
          });
          await updateSchema({
            field: 'approveDes',
            show: false,
          });
        } else if (data.record['planFlag'] == '5' || data.record['planFlag'] == '6') {
          await updateSchema({
            label: '变更原因',
            field: 'optDes',
            dynamicDisabled: true,
            show: true,
          });
          await updateSchema({
            field: 'approveDes',
            show: true,
          });
        } else if (data.record['planFlag'] == '8' || data.record['planFlag'] == '9') {
          updateSchema({
            label: '废止原因',
            field: 'optDes',
            dynamicDisabled: true,
            show: true,
          });
          await updateSchema({
            field: 'approveDes',
            show: true,
          });
        }
        //表单赋值
        data.record['optDes'] = data.record['optDesbak'];
        await setFieldsValue({
          ...data.record,
        });
        setProps({ disabled: !data?.showFooter });
      }
    } else {
      // 隐藏底部时禁用整个表单
      setProps({ disabled: !data?.showFooter });
    }
    if (!data?.showFooter) {
      updateSchema({ field: 'fileUploadPath', componentProps: { buttonVisible: false } });
    } else {
      updateSchema({ field: 'fileUploadPath', componentProps: { buttonVisible: true } });
    }
  });
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '新建' : unref(handleFlag) == '4' ? '变更' : unref(isDetail) ? '详情' : '废止'));
  //表单提交事件
  async function handleSubmit(v) {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      values['planFlag'] = unref(handleFlag);
      //提交表单
      await changeOrRepeal(values, unref(handleFlag) == '4');
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped>
  /** 时间和数字输入框样式 */
  :deep(.ant-input-number) {
    width: 100%;
  }

  :deep(.ant-calendar-picker) {
    width: 100%;
  }
</style>
