<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm" class="!pt-4">
      <template #functionName="{ model, field }">
        <a-input
          v-model:value="model[field]"
          :placeholder="t('点击选择功能名称')"
          :disabled="isUpdate"
          @click="showModal"
        >
          <template #suffix>
            <Icon icon="ant-design:ellipsis-outlined" />
          </template>
        </a-input>
      </template>
    </BasicForm>
  </BasicModal>
  <FunctionModal @register="registerFunctionModal" @success="handleSuccess" />
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { FormSchema } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { updateFunctionAuth } from '/@/api/system/functionAuthority';
  import { getAuthList } from '/@/api/system/authorize';
  import Icon from '/@/components/Icon/index';
  import FunctionModal from './FunctionModal.vue';
  import { cloneDeep } from 'lodash-es';

  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const accountFormSchema: FormSchema[] = [
    {
      field: 'menuName',
      label: '功能名称',
      component: 'Input',
      required: true,
      colProps: { span: 24 },
      slot: 'functionName',
    },
    {
      field: 'dataAuthIdList',
      label: '数据权限',
      component: 'ApiSelect',
      required: true,
      componentProps: {
        mode: 'multiple',
        placeholder: '请选择数据权限',
        api: getAuthList,
        labelField: 'name',
        valueField: 'id',
        getPopupContainer: () => document.body,
      },
      colProps: { span: 24 },
    },
    {
      field: 'remark',
      label: '备注',
      component: 'InputTextArea',
      componentProps: {
        placeholder: t('请填写备注'),
      },
      colProps: { span: 24 },
    },
  ];

  const emit = defineEmits(['success', 'register']);

  const { notification } = useMessage();
  const isUpdate = ref(true);
  const record = ref();
  const tableName = ref('');

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

  const [registerFunctionModal, { openModal }] = useModal();
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false, width: 600 });
    isUpdate.value = !!data?.isUpdate;
    tableName.value = '';
    if (unref(isUpdate)) {
      record.value = cloneDeep(data.record);
      tableName.value = data.record.tableName;
      setFieldsValue({
        ...record.value,
      });
    }
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增功能权限' : '编辑功能权限'));

  function showModal() {
    openModal(true, {
      selectedName: tableName.value,
    });
  }

  function handleSuccess(record) {
    setFieldsValue({
      menuName: record.menuName,
    });
    tableName.value = record.tableName;
  }

  async function handleSubmit() {
    try {
      const values = await validate();
      values.tableName = tableName.value;

      setModalProps({ confirmLoading: true });
      await updateFunctionAuth(values);
      notification.success({
        message: t('提示'),
        description: `${unref(isUpdate) ? '编辑' : '新增'}功能权限成功！`,
      });
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
