<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined" v-auth="'zjgl-zjkgl-add'"> 添加</a-button>
        <a-button type="primary" @click="generate" preIcon="ant-design:user-add-outlined" v-auth="'zjgl-zjkgl-sczh'"> 生成账户</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls" v-auth="'zjgl-zjkgl-dc'">导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls" v-auth="'zjgl-zjkgl-dr'">导入</j-upload-button>
        <a-button type="primary" @click.stop="handleExportXls('专家库模板', getDownloadZjk)">下载模板</a-button>
      </template>
      <!-- 专业方向 -->
      <template #form-zyfx="{ model, field }">
        <a-checkbox-group v-model:value="model[field]" name="checkboxgroup" :options="plainOptions" />
      </template>
      <!-- 启用停用样式调整 -->
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'status'">
          <a-switch v-model:checked="record.checked" @change="change($event, record.id)" checked-children="启用" un-checked-children="停用" />
        </template>
        <!-- <template v-if="column.dataIndex === 'phone'">
          <div>{{ dec(record) }}</div>
        </template> -->
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>

    <!-- 表单区域 -->
    <GZjcybModal @register="registerModal" @success="handleSuccess"></GZjcybModal>
  </div>
</template>

<script lang="ts" name="zjgl-gZjcyb" setup>
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import GZjcybModal from './modules/GZjcybModal.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useMethods } from '/@/hooks/system/useMethods';
  import { columns, searchFormSchema, useSpeciality } from './GZjcyb.data';
  import { list, view, sczh, updateStatus, deleteOne, getImportUrl, getExportUrl, getDownloadZjk } from './GZjcyb.api';

  //注册model
  const [registerModal, { openModal }] = useModal();
  // 提示
  const { createMessage } = useMessage();

  const { handleExportXls } = useMethods();
  //注册table数据
  const { tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '专家成员表',
      api: list,
      columns,
      canResize: false,
      // clickToRowSelect: true,
      ellipsis: false,
      rowKey: 'id',
      afterFetch: (params) => {
        params.forEach(async (v) => {
          if (v.status == 1) v.checked = true;
          else v.checked = false;
        });
      },
      // rowSelection: { type: 'radio' },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        autoAdvancedCol: 10, //超过几行显示折叠默认是3
      },
      actionColumn: {
        width: 120,
      },
    },
    exportConfig: {
      name: '专家成员表',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });
  const plainOptions = useSpeciality().data;
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 编辑事件
   */
  async function handleEdit(record: Recordable) {
    const records = await view({ id: record.id });
    openModal(true, {
      record: records,
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 详情
   */
  async function handleDetail(record: Recordable) {
    const records = await view({ id: record.id });
    openModal(true, {
      record: records,
      isUpdate: true,
      showFooter: false,
    });
  }
  // 启用停用
  async function change(v, id) {
    const params = {
      id: id,
      status: v ? 1 : 2,
    };
    await updateStatus(params);
    reload();
  }
  // 生成账户
  async function generate() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容');
      return;
    }
    await sczh({ zjIds: selectedRowKeys.value.toString() });
    reload();
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, reload);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }
</script>
<style scoped></style>
