<template>
  <div style="min-height: 400px">
    <BasicForm @register="registerForm" />
    <div style="width: 100%; text-align: center" v-if="!formDisabled">
      <a-button @click="submitForm" pre-icon="ant-design:check" type="primary">提交</a-button>
    </div>
  </div>
</template>

<script lang="ts">
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { computed, defineComponent } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { propTypes } from '/@/utils/propTypes';
  import { getBpmFormSchema } from '../SchoolMedia.data';
  import { saveOrUpdate } from '../SchoolMedia.api';

  export default defineComponent({
    name: 'SchoolMediaForm',
    components: {
      BasicForm,
    },
    props: {
      formData: propTypes.object.def({}),
      formBpm: propTypes.bool.def(true),
    },
    setup(props) {
      const [registerForm, { setFieldsValue, setProps, getFieldsValue }] = useForm({
        labelWidth: 150,
        schemas: getBpmFormSchema(props.formData),
        showActionButtonGroup: false,
        baseColProps: { span: 24 },
      });

      const formDisabled = computed(() => {
        if (props.formData.disabled === false) {
          return false;
        }
        return true;
      });

      let loadedFormData: Recordable = {};
      const queryByIdUrl = '/school/media/queryById';

      async function initFormData() {
        const params = { id: props.formData.dataId };
        const data = await defHttp.get({ url: queryByIdUrl, params });
        loadedFormData = { ...data };
        await setFieldsValue(loadedFormData);
        await setProps({ disabled: formDisabled.value });
      }

      async function submitForm() {
        const data = getFieldsValue();
        const params = Object.assign({}, loadedFormData, data);
        await saveOrUpdate(params, true);
      }

      initFormData();

      return {
        registerForm,
        formDisabled,
        submitForm,
      };
    },
  });
</script>

