<template>
  <BasicModal width="800px" v-bind="$attrs" @register="register" @ok="confirm" :title="title">
    <BasicForm @register="registerForm" :model="model" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, PropType, reactive, ref, toRefs } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { schemas } from './data';
  interface PopupData {
    title: string;
  }
  interface Role {
    id: string | number;
  }

  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      popupData: {
        type: Object as PropType<PopupData>,
        default: () => {},
      },
    },
    emits: ['saveData'],
    setup(props, { emit }) {
      const popupData = props.popupData as PopupData;
      const modelRef = ref({});
      const role = reactive<Role>({
        id: 0,
      });
      const { createMessage } = useMessage();
      const { error } = createMessage;

      const [registerForm, { updateSchema, setFieldsValue, resetFields, validate }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      //   const [register, { closeModal }] = useModalInner((data) => {
      //     modelRef.value = data;

      //
      //   });
      const [register, { closeModal }] = useModalInner((data) => {
        resetFields();
        if (data.id !== 0) {
          role.id = data.id;
          updateSchema([
            {
              field: 'money',
              componentProps: {
                disabled: true,
              },
            },
            {
              field: 'isdues',
              componentProps: {
                disabled: true,
              },
            },
            {
              field: 'type',
              componentProps: {
                disabled: true,
              },
            },
          ]);
        } else {
          role.id = 0;
          updateSchema([
            {
              field: 'money',
              componentProps: {
                disabled: false,
              },
            },
            {
              field: 'type',
              componentProps: {
                disabled: false,
              },
            },
          ]);
        }
        // data.account = 3
        setFieldsValue(data);
      });

      async function confirm() {
        try {
          const data = await validate();
          if (data.account === data.inaccount) {
            error('交易账户和流入账户不能是同一个！');
            return;
          }
          data.id = role.id;
          if (typeof data.account === 'number') {
            data.accountId = data.account;
          }
          if (typeof data.inaccount === 'number') {
            data.accountInsideId = data.inaccount;
          }
          if (typeof data.typename === 'number') {
            data.typeId = data.typename;
          }
          data.userId = data.user.id;
          delete data.account;
          delete data.inaccount;
          delete data.typename;
          delete data.user;
          if (data.year.year) {
            data.year = data.year.year;
          }
          if (data.duesyear) {
            data.duesId = data.duesyear.id;
          }
          const popupData = { closeModal, data };
          emit('saveData', popupData);
        } catch (err: any) {
          error(err.errorFields[0].errors[0]);
        }
      }
      return {
        register,
        schemas,
        registerForm,
        model: modelRef,
        confirm,
        ...toRefs(popupData),
      };
    },
  });
</script>
<style scoped>
  .ant-form-item-label {
    overflow: hidden;
    text-align: center !important;

    /* white-space: pre-wrap !important; */
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  @media (max-width: 639px) {
    .ant-form-item-label {
      line-height: 2.5715 !important;
      text-align: center !important;
    }
  }
</style>
