<template>
  <PageWrapper dense fixedHeight contentFullHeight>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" v-auth="'translation:add'" @click="handleCreate">
          {{ t('新增') }}
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                auth: 'translation:edit',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                auth: 'translation:delete',
                color: 'error',
                popConfirm: {
                  title: t('是否确认删除'),
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <LgDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { onMounted, ref } from 'vue';

  import { BasicTable, useTable, TableAction, BasicColumn, FormSchema } from '/@/components/Table';

  import LgDrawer from './components/LgDrawer.vue';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { usePermission } from '/@/hooks/web/usePermission';
  // import { useI18n } from 'vue-i18n';
  import { PageWrapper } from '/@/components/Page';
  import { deleteLanguage, getLanguagePageList, getLanguageTypeList } from '/@/api/system/language';
  import { useModal } from '/@/components/Modal';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: '',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请输入名称或编码'),
      },
    },
  ];

  const { notification } = useMessage();
  const [registerDrawer, { openModal }] = useModal();
  const dataRef = ref([]);
  const { hasPermission } = usePermission();

  const [registerTable, { reload, setColumns, setProps, setPagination }] = useTable({
    title: t('翻译管理列表'),
    columns: [],
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      showResetButton: false,
    },
    handleSearchInfoFn(info) {
      const filterData = dataRef.value.filter((item) => {
        for (const key in item) {
          if ((item[key] as string).indexOf(info.keyword) > -1) {
            return item;
          }
        }
      });
      setProps({ dataSource: filterData });
      return info;
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    pagination: { pageSize: 20 },
    actionColumn: {
      width: 88,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    tableSetting: {
      size: false,
    },
    customRow: (record) => {
      return {
        ondblclick: () => {
          if (hasPermission('translation:edit')) {
            handleEdit(record);
          }
        },
      };
    },
  });
  onMounted(async () => {
    const treeData = await getLanguageTypeList();
    dataRef.value = await getLanguagePageList();
    const columns: BasicColumn[] = [];
    treeData.forEach((o) => {
      columns.push({
        title: o.name,
        dataIndex: o.code,
        align: 'center',
        resizable: true,
      });
    });
    setColumns(columns);
    setProps({ dataSource: dataRef.value });
    setPagination({ total: dataRef.value.length });
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete(record: Recordable) {
    await deleteLanguage([record.mapId]);

    dataRef.value = await getLanguagePageList();
    setProps({ dataSource: dataRef.value });
    reload();
    notification.success({
      message: t('提示'),
      description: t('删除成功'),
    }); //提示消息
  }

  async function handleSuccess() {
    dataRef.value = await getLanguagePageList();
    setProps({ dataSource: dataRef.value });
    reload();
  }
</script>
