<template>
  <BasicModal
    v-bind="$attrs"
    destroyOnClose
    @register="registerDrawer"
    :title="getTitle"
    width="35%"
    @ok="handleSubmit"
    wrapClassName="custom-modal-box"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { BasicModal, useModalInner } from '/@/components/Modal';

  import { useMessage } from '/@/hooks/web/useMessage';
  // import { useI18n } from 'vue-i18n';

  import { addLanguage, getLanguageTypeList, updateLanguage } from '/@/api/system/language';
  import { useI18n } from '/@/hooks/web/useI18n';
  export default defineComponent({
    name: 'MenuDrawer',
    components: {
      BasicModal,
      BasicForm,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const { notification } = useMessage();
      const rowId = ref('');

      const { t } = useI18n();

      const [registerForm, { resetFields, setFieldsValue, appendSchemaByField, validate }] =
        useForm({
          schemas: [],
          showActionButtonGroup: false,
          layout: 'vertical',
        });

      const [registerDrawer, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        const treeData = await getLanguageTypeList();

        treeData.forEach((o) => {
          appendSchemaByField(
            {
              field: o.code,
              label: o.name,
              component: 'Input',
              required: true,
              colProps: { lg: 24, md: 24 },
            },
            '',
          );
        });

        if (unref(isUpdate)) {
          rowId.value = data.record.mapId;

          setFieldsValue({
            ...data.record,
          });
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? t('新增翻译内容') : t('编辑翻译内容')));

      async function handleSubmit() {
        try {
          const values = await validate();
          let arr: { languageType; languageContent }[] = [];
          for (let i in values) {
            let obj: any = {
              languageType: i,
              languageContent: values[i],
            };
            if (unref(isUpdate)) obj.mapId = rowId.value;
            arr.push(obj);
          }
          setModalProps({ confirmLoading: true });
          // TODO custom api
          if (!unref(isUpdate)) {
            //false 新增
            await addLanguage(arr);
            notification.success({
              message: t('提示'),
              description: t('新增成功'),
            }); //提示消息
          } else {
            await updateLanguage(arr);
            notification.success({
              message: t('提示'),
              description: t('编辑成功'),
            }); //提示消息
          }
          closeModal();
          emit('success');
        } catch (error) {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerDrawer,
        registerForm,
        getTitle,
        handleSubmit,
      };
    },
  });
</script>
<style scoped></style>
