<template>
  <div>
    <BasicTable
      @register="registerTable"
      @selection-change="handleSelectionChange"
    >
      <template #toolbar>
        <a-button
          type="primary"
          :disabled="state.multiple"
          @click="handleDel()"
        >移除粉丝</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '查看',
                icon: 'fa6-regular:pen-to-square',
                onClick: handleViewEdit.bind(null, record)
              },
              {
                label: '移除',
                icon: 'ant-design:delete-outlined',
                color: 'error',
                disabled: record?.cfType == 1,
                onClick: handleDel.bind(null, record)
              }]"
          />
        </template>
      </template>
    </BasicTable>
    <ConcernModal @register="registerModal" @success="handleRefreshTable"/>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, toRaw } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { listPushConcernFan, delPushConcernFan } from '/@/api/platform/common/controller/pushConcernFan';
  import { useModal } from '/@/components/Modal';
  import ConcernModal from '../pushConcern/ConcernModal.vue';
  import { columns, searchFormSchema } from '../pushConcern/concern.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useUserStore } from '/@/store/modules/user';

  const userStore = useUserStore();
  const userInfoStore = userStore.getUserInfo;

  interface TableState {
    single: boolean;
    multiple: boolean;
  }

  const state = reactive<TableState>({
    // 非单个禁用
    single: true,
    // 非多个禁用
    multiple: true
  });
  const { createConfirm, createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, clearSelectedRowKeys, getSelectRowKeys }] = useTable({
    title: '粉丝列表',
    api: listPushConcernFan,
    rowKey: 'id',
    columns,
    formConfig: {
      compact: true,
      labelWidth: 80,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      autoAdvancedLine: 3,
    },
    rowSelection: { type: 'checkbox' },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    clickToRowSelect: false,
    showIndexColumn: false,
    actionColumn: {
      width: 220,
      title: '操作',
      dataIndex: 'action',
      fixed: false
    },
    searchInfo: {
      status: '1',
      concernUserId: userInfoStore.id
    },
    handleSearchInfoFn: () => clearSelectedRowKeys()
  });

  /** 处理多选框选中数据 */
  function handleSelectionChange(selection?: Recordable) {
    const rowSelection = toRaw(selection?.keys) || [];
    state.single = rowSelection.length != 1;
    state.multiple = !rowSelection.length;
  }

  /** 查看按钮操作,行内查看 */
  function handleViewEdit(record?: Recordable) {
    record = record || { id: getSelectRowKeys() };
    openModal(true, { _tag: 'view', record });
  }

  /** 删除按钮操作,行内删除 */
  async function handleDel(record?: Recordable) {
    const ids = record?.id || getSelectRowKeys();
    createConfirm({
      iconType: 'warning',
      title: '警告',
      content: `是否确认移除编号为${ids}的数据?`,
      onOk: async () => {
        await delPushConcernFan(ids);
        createMessage.success('移除成功!');
        handleRefreshTable();
      }
    });
  }

  /** 处理表格刷新 */
  function handleRefreshTable() {
    clearSelectedRowKeys();
    reload();
  }

</script>
