<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleRole" v-if="getCheckPerm('spares_AddType')"> 新增备件类型 </a-button>
        <a-button type="primary" @click="handleIn" v-if="getCheckPerm('spares_In')"> 备件入库 </a-button>
        <a-button type="primary" @click="handleCreate" v-if="getCheckPerm('spares_InList')"> 入库记录 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction  v-if="record.id != 1"
          :actions="[
            {
              label: t('common.edit'),
              ifShow:getCheckPerm('spares_edit'),
              onClick: handleRole.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <RoleDrawer @register="registerTableModal" @success="handleSuccess" />
    <SpareModal @register="register" @update="reload" />
    <SpareInModal @register="registerIn" @update="reload" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, nextTick } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { partList } from '/@/api/spares';//deleteRoleApi
  import RoleDrawer from './RoleDrawer.vue';
  import SpareModal from './spareModal.vue';
  import SpareInModal from './spareInModal.vue';
  import { useModal } from '/@/components/Modal';
  import { usePermissionStore } from '/@/store/modules/permission';
  // import { useUserStore } from '/@/store/modules/user';

  import { columns, searchFormSchema } from './role.data';

  export default defineComponent({
    name: 'RoleManagement',
    components: { BasicTable, TableAction,  RoleDrawer, SpareModal, SpareInModal },// 
    setup() {
      const { t } = useI18n();
      const [registerTableModal, { openModal:openTableModal }] = useModal();
      const [register, { openModal }] = useModal();
      const [registerIn, { openModal:openInModal }] = useModal();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      // const userStore = useUserStore();
      // const { getCheckRole } = userStore;
      const [registerTable, { reload }] = useTable({
        api: partList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        canResize: true,
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        actionColumn: {
          // ifShow: getCheckRole('super'),
          width: 110,
          title: t('common.operating'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

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

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

      function handleIn(record: Recordable) {
        openInModal(true, {});
      }

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

      async function handleDelete(record: Recordable) {
        try {
          console.log('roleId', [record.roleId]);
          // const result = await deleteRoleApi({id:record.id});
          // console.log('result', result);
          nextTick(reload);
        } catch (error) {
          console.log('error', error);
        }
      }

      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        registerTableModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleRole,
        handleIn,
        register,
        registerIn,
        reload,
        getCheckPerm,
        t,
      };
    },
  });
</script>
