<template>
  <PageWrapper dense contentFullHeight fixedHeight>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" v-auth="'organization:sync'" @click="handleSync"> 同步 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'name'">
          <a-tag color="processing" v-if="record.departmentType === 1">公司</a-tag>
          <a-tag color="warning" v-else-if="record.departmentType === 0">部门</a-tag>
          &nbsp;{{ record.name }}
        </template>
        <template v-else-if="column.dataIndex == 'wechatDeptId'">
          <a-tag color="success" v-if="!!record.wechatDeptId">同步成功</a-tag>
        </template>
      </template>
    </BasicTable>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, BasicColumn, FormSchema } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getDepartmentsList, updateSyncDept } from '/@/api/system/wechat';

  const columns: BasicColumn[] = [
    {
      title: '名称',
      dataIndex: 'name',
      align: 'left',
      //sorter: true,
      resizable: true,
    },
    {
      title: '编码',
      dataIndex: 'code',
      align: 'center',
      width: '25%',
      //sorter: true,
      resizable: true,
    },
    {
      title: '同步状态',
      dataIndex: 'wechatDeptId',
      align: 'center',
      width: '25%',
      //sorter: true,
      resizable: true,
    },
  ];

  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: '',
      component: 'Input',
      colProps: { lg: 8, md: 12, sm: 12 },
      componentProps: {
        placeholder: '请输入关键字',
      },
    },
  ];

  const { notification } = useMessage();
  const [registerTable, { reload, getSelectRowKeys }] = useTable({
    title: '企业号组织列表',
    api: getDepartmentsList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      actionColOptions: { span: 16 },
      showResetButton: false,
    },
    striped: false,
    useSearchForm: true,
    showTableSetting: true,
    showIndexColumn: false,
    tableSetting: {
      size: false,
    },
    rowSelection: {
      type: 'radio',
    },
  });

  async function handleSync() {
    if (import.meta.env.VITE_GLOB_PRODUCTION === 'true') {
      notification.warning({
        message: '提示',
        description: '由于您当前处于体验状态，无法操作进行同步',
      });
      return false;
    }
    const id = getSelectRowKeys().length ? getSelectRowKeys()[0] : '';
    await updateSyncDept(id);
    notification.success({
      message: '提示',
      description: '同步成功',
    });
    reload();
  }
</script>
