<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="jeecg-basic-table-form-container">
      <a-form ref="formRef" @keyup.enter.native="searchQuery" :model="queryParam">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-item name="email">
              <template #label><span title="邮箱">邮箱</span></template>
              <a-input name="email" placeholder="输入邮箱查询" v-model:value="queryParam.email" allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="10">
            <a-form-item name="blackOrWhite">
              <a-select allow-clear v-model:value="queryParam.blackOrWhite" 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" preIcon="ant-design:search-outlined" @click="searchQuery">查询</a-button>
              <a-button type="primary" preIcon="ant-design:reload-outlined" @click="searchReset" style="margin-left: 8px"> 重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <!--引用表格-->
    <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 === 'wasteEnquiryNum'">
          <span>
            <span v-if="text === '' || text === null || text === 0">0</span>
            <a v-else @click="wasteNumVisible(record.email)">{{ text }}</a>
          </span>
        </template>

        <template v-if="column.dataIndex === 'action'">
          <span>
            <a @click="handleEdit(record)">编辑</a>

            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <template #overlay>
                <a-menu>
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </span>
        </template>
      </template>
    </BasicTable>

    <enquiry-public-black-email-modal ref="modalFormRef" @ok="reload()" />
    <xp-recycle-bin-modal ref="XpRecycleBinModalRef" />
  </a-card>
</template>

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

  const XpRecycleBinModalRef = ref();
  const formRef = ref();
  const modalFormRef = ref();
  const queryParam = reactive<any>({});
  const superFieldList = ref([]);

  //注册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,
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: '询盘列表',
      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 });

  onMounted(() => {
    getSuperFieldList();
  });

  function getSuperFieldList() {
    let fieldList = [];
    fieldList.push({ type: 'string', value: 'email', text: '邮箱', dictCode: '' });
    superFieldList.value = fieldList;
  }

  async function wasteNumVisible(email) {
    await nextTick();

    if (XpRecycleBinModalRef.value && XpRecycleBinModalRef.value.init) {
      XpRecycleBinModalRef.value.init(email);
    }
  }

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