<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form ref="formRef" @keyup.enter.native="searchQuery" :model="queryParam">
        <a-row :gutter="24">
          <a-col :xl="6" :sm="12">
            <select-site ref="selectSiteRef" @set-site-info="getSiteList" />
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item name="email">
              <a-input placeholder="请输入邮箱" v-model:value="queryParam.email" allowClear />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="10">
            <a-form-item name="blackOrWhite">
              <a-select v-model:value="queryParam.blackOrWhite" allowClear placeholder="请选择黑白名单">
                <a-select-option value="0">黑名单</a-select-option>
                <a-select-option value="1">白名单</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" preIcon="ant-design:search-outlined">查询</a-button>
              <a-button ghost type="primary" @click="searchReset" preIcon="ant-design:reload-outlined" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增 </a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="mdi:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>

      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>

      <template #bodyCell="{ column, record, index, text }">
        <template v-if="column.dataIndex === 'blackOrWhite'">
          <span>
            <a-tag v-if="record.blackOrWhite === 0" color="red">黑名单</a-tag>
            <a-tag v-else>白名单</a-tag>
          </span>
        </template>

        <template v-if="column.dataIndex === 'type'">
          <span>
            <a-tag v-if="text === '0'" color="cyan">通配</a-tag>
            <a-tag v-else color="blue">完全匹配</a-tag>
          </span>
        </template>

        <template v-if="column.dataIndex === 'wasteEnquiryNum'">
          <span>
            <span v-if="text === '' || text === null || text === 0">0</span>
            <a v-else @click="wasteNumVisible(record.email, record.type, record.siteId)">{{ text }}</a>
          </span>
        </template>
      </template>
    </BasicTable>

    <enquiry-site-black-email-modal ref="modalFormRef" @ok="handleSuccess" />
    <xp-recycle-bin-modal ref="XpRecycleBinModalRef" />
  </a-card>
</template>

<script lang="ts" setup name="EnquirySiteBlackEmailList">
  import '/@/assets/less/TableExpand.less';
  import '/@/assets/less/common.less';
  import EnquirySiteBlackEmailModal from './modules/EnquirySiteBlackEmailModal.vue';
  import SelectSite from '@/components/Adweb/selectSite.vue';
  import XpRecycleBinModal from './modules/XpRecycleBinModal.vue';
  import { nextTick, reactive, ref } from 'vue';
  import { useListPage } from '@/hooks/system/useListPage';
  import { batchDelete, deleteOne, getExportUrl, getImportUrl, list } from './siteBlackEmail.api';
  import { columns } from './siteBlackEmail.data';
  import { useCommonList } from '@/hooks/component/JeecgList';
  import { BasicTable, TableAction } from '/@/components/Table';

  const description = ref('enquiry_site_black_email管理页面');
  const formRef = ref();
  const modalFormRef = ref();
  const XpRecycleBinModalRef = ref();
  const dictOptions = reactive({});
  const superFieldList = reactive([]);
  const siteOptions = reactive([]);
  let queryParam = reactive<any>({});

  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '站点邮箱黑名单',
      api: list,
      columns: columns,
      canResize: false,
      useSearchForm: false,
      actionColumn: {
        width: 180,
        fixed: 'right',
      },
      striped: true,
      bordered: false,
      immediate: false, // 不直接触发，通过其他事件触发接口
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: '站点IP黑名单',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [
    registerTable,
    { reload, clearSelectedRowKeys, updateTableDataRecord, findTableDataRecord, getDataSource },
    { rowSelection, selectedRowKeys },
  ] = tableContext;

  const { handleEdit, getTableAction, batchHandleDelete, handleDetail, getDropDownAction, handleDelete, handleAdd, searchQuery, searchReset } =
    useCommonList({ tableContext, modalFormRef, formRef, batchDelete, deleteOne });

  function wasteNumVisible(email, type, siteId) {
    nextTick().then(() => {
      XpRecycleBinModalRef.value.init(email, type, siteId);
    });
  }

  function getSiteList(selectedSiteInfo: any) {
    queryParam.siteId = selectedSiteInfo.id;

    // 选择站点是api操作，所以需要nextTick
    nextTick().then(() => {
      reload();
    });
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    queryParam.siteId = localStorage.getItem('siteID');
    (selectedRowKeys.value = []) && reload();
  }
</script>
