<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar> </template>
      <template #role="{ record }">
        {{ renderRoleType(record.role) }}
      </template>
      <template #status="{ record }">
        {{ renderStatus(record.status) }}
      </template>
      <!-- <template #createTime="{ record }">
        <Time v-if="record.createTime" :value="record.createTime" mode="datetime" />
      </template>             {
              icon: 'mdi:printer-outline',
              label: t('common.print'),
              color: 'error',
              onClick: () => {
                createMessage.info(t('common.notConnect'));
              },
              icon: 'mdi:information-outline',
            },-->
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: t('routes.staff.setpaswd'),
              onClick: handleOpenModal.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <SetpaswordModal @register="register" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, computed } from 'vue';
  import { BasicTable, useTable, BasicColumn, FormProps, TableAction } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { uploadApi } from '/@/api/sys/upload';
  import { useModal } from '/@/components/Modal';
  // import { Switch } from 'ant-design-vue';
  // import { h } from 'vue';
  import SetpaswordModal from './setpaswordModal.vue';
  import { ListApi } from '/@/api/staff/list';
  import { useI18n } from '/@/hooks/web/useI18n';
  // import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
  import { useGo } from '/@/hooks/web/usePage';
  // import { Time } from '/@/components/Time';
  import { useLocaleStore } from '/@/store/modules/locale';
  const localeStore = useLocaleStore();

  const isJA = computed(() => localeStore.getLocale === 'ja');

  export default defineComponent({
    components: { BasicTable, TableAction, SetpaswordModal },
    setup() {
      const [register, { openModal }] = useModal();
      const { createMessage } = useMessage();
      const go = useGo();
      const { t } = useI18n();
      const columns: BasicColumn[] = [
        {
          title: 'ID',
          dataIndex: 'id',
          fixed: 'left',
          width: 60,
        },
        {
          title: t('routes.staff.deptName'),
          dataIndex: 'companyName',
          width: 100,
        },
        {
          title: t('routes.staff.userName'),
          dataIndex: 'userName',
          width: 200,
        },
        {
          title: t('routes.staff.nickName'),
          dataIndex: 'nickName',
          width: 200,
        },
        {
          title: t('common.roleName'),
          dataIndex: 'roleName',
          // slots: { customRender: 'role' },
          customRender: ({ record }) => {
            const { roleName } = record;
            const obj = {
              公司管理员: '企業管理者',
              公司员工: '一般',
            };
            return isJA.value ? obj[roleName] : roleName;
          },
          sorter: true,
          width: 80,
        },
        // {
        //   title: t('common.state'),
        //   dataIndex: 'status',
        //   slots: { customRender: 'status' },
        //   sorter: true,
        //   width: 80,
        // },

        {
          title: t('routes.staff.createTime'),
          dataIndex: 'registerTime',
          // slots: { customRender: 'createTime' },
          width: 130,
        },
        {
          title: '操作',
          dataIndex: '',
          slots: { customRender: 'action' },
          fixed: 'right',
          align: 'center',
          width: isJA.value ? 160 : 80,
        },
      ];

      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        schemas: [
          {
            field: 'phoneNum',
            label: t('routes.staff.userName'),
            component: 'Input',
            colProps: {
              xl: 5,
              xxl: 5,
            },
          },
        ],
      };

      const [registerTable] = useTable({
        title: t('routes.staff.staffList'),
        api: ListApi,
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: false,
        rowKey: 'id',
        pagination: { pageSize: 20 },
        ellipsis: false,
        bordered: true,
      });

      function renderRoleType(type: number): string {
        switch (type) {
          case 0:
            return t('routes.staff.roleType.0');
          case 1:
            return t('routes.staff.roleType.1');
          default:
            return '';
        }
      }
      function renderStatus(type: number): string {
        switch (type) {
          case 1:
            return t('common.normal');
          case 0:
            return t('common.unNormal');
          default:
            return '';
        }
      }
      function handleOpenModal(record: Recordable) {
        openModal(true, record);
      }
      return {
        registerTable,
        createMessage,
        t,
        go,
        renderRoleType,
        renderStatus,
        uploadApi: uploadApi as any,
        handleOpenModal,
        register,
      };
    },
  });
</script>
