<template>
  <PageWrapper fixedHeight contentFullHeight>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" v-auth="'unitConversion:add'" @click="handleCreate" class="ml-4">
          {{ t('新增') }}
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'state'">
          <a-switch v-model:checked="record.state" :checkedValue="1" :unCheckedValue="0" disabled />
        </template>
        <template v-if="column.dataIndex == 'isStandard'">
          <span>{{ record.isStandard ? '是' : '否' }}</span>
        </template>
        <template v-if="column.dataIndex == 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                auth: 'unitConversion:edit',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                auth: 'unitConversion:delete',
                color: 'error',
                onClick: handleDelete.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <ConfigModal @register="registerModal" @success="reload" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { BasicTable, useTable, TableAction, BasicColumn, FormSchema } from '/@/components/Table';

  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { PageWrapper } from '/@/components/Page';
  import { getConversionPageList, deleteConversionList } from '/@/api/erp/unit/conversion';
  import { getUnitTypeList } from '/@/api/erp/unit/type';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import ConfigModal from './ConfigModal.vue';
  import { usePermission } from '/@/hooks/web/usePermission';

  const { t } = useI18n();
  const columns: BasicColumn[] = [
    {
      title: '单位类型',
      dataIndex: 'unitTypeName',
    },
    {
      title: '基准单位',
      dataIndex: 'standardName',
    },
    {
      title: '换算关系',
      dataIndex: 'quantity',
    },
    {
      title: '状态',
      dataIndex: 'state',
    },
  ];

  const searchFormSchema: FormSchema[] = [
    {
      field: 'unitTypeId',
      label: '单位类型',
      component: 'ApiSelect',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请选择单位类型',
        api: getUnitTypeList,
        labelField: 'name',
        valueField: 'id',
        getPopupContainer: () => document.body,
      },
    },
  ];
  const existUniTypeIdList = ref<string[]>([]);
  const { notification } = useMessage();
  const { hasPermission } = usePermission();

  const [registerModal, { openModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    title: '单位换算配置',
    api: getConversionPageList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      showResetButton: false,
    },
    afterFetch: (data) => {
      existUniTypeIdList.value = [...new Set(data?.map((x) => x.unitTypeId))] as string[];
    },
    useSearchForm: true,
    showTableSetting: true,
    needDivider: true,
    striped: false,
    actionColumn: {
      width: 88,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    customRow: (record) => {
      return {
        ondblclick: () => {
          if (hasPermission('unitConversion:edit')) {
            handleEdit(record);
          }
        },
      };
    },
    tableSetting: {
      size: false,
    },
  });

  const handleCreate = () => {
    openModal(true, {
      isUpdate: false,
      existUniTypeIdList: existUniTypeIdList.value,
    });
  };

  const handleEdit = (record) => {
    openModal(true, {
      id: record.id,
      isUpdate: true,
      existUniTypeIdList: existUniTypeIdList.value,
    });
  };

  const handleDelete = (record) => {
    Modal.confirm({
      title: t('提示信息'),
      icon: createVNode(ExclamationCircleOutlined),
      content: t('是否确认删除？'),
      okText: t('确认'),
      cancelText: t('取消'),
      async onOk() {
        await deleteConversionList(record.id);
        notification.success({
          message: t('提示'),
          description: t('删除成功'),
        });
        reload();
      },
      onCancel() {},
    });
  };
</script>
<style lang="less" scoped>
  :deep(.vben-page-wrapper-content) {
    margin: 0;
  }

  :deep(.vben-basic-table-form-container) {
    margin-right: 0;
  }

  :deep(.ant-table-wrapper) {
    height: calc(100% - 10px);
  }
</style>
