<!-- 合伙人列表 -->
<template>
    <div class="partner-list">
        <!-- 搜索表单 -->
        <partner-search ref="searchRef" @search="handleSearch" />

        <!-- 表格 -->
        <CommonTable
            ref="tableRef"
            :pageConfig="pageConfig"
            :columns="columns"
            :bodyStyle="{ padding: 0 }"
        >
            <template #toolbar>
                <el-button
                    type="primary"
                    plain
                    v-permission="'marketing:partner:batchPass'"
                    @click="handleBatchPass(null)"
                >
                    批量通过
                </el-button>
                <el-button
                    type="danger"
                    plain
                    v-permission="'marketing:partner:batchReject'"
                    @click="handleBatchReject(null)"
                >
                    批量驳回
                </el-button>
                <el-button
                    type="success"
                    plain
                    v-permission="'data:universities:export'"
                    @click="handleExportExcel"
                    :icon="DownloadOutlined"
                >
                    导出EXCEL
                </el-button>
            </template>

            <template #status="{ row }">
                <dict-data
                    code="partner_status"
                    type="tag"
                    :model-value="row.status"
                />
            </template>
            <template #action="{ row }">
                <div class="action-buttons">
                    <!-- 根据状态显示对应的操作按钮 -->
                    <el-button
                        type="success"
                        link
                        v-if="row.status === '0' || row.status === '2'"
                        v-permission="'marketing:partner:pass'"
                        @click="handleBatchPass(row)"
                    >
                        [通过]
                    </el-button>
                    <el-button
                        type="danger"
                        link
                        v-if="row.status === '0'"
                        v-permission="'marketing:partner:reject'"
                        @click="handleBatchReject(row)"
                    >
                        [驳回]
                    </el-button>

                    <el-button
                        type="primary"
                        link
                        v-if="row.status !== '0'"
                        v-permission="'marketing:partner:edit'"
                        @click="handleEdit(row)"
                    >
                        [编辑]
                    </el-button>
                    <el-button
                        type="danger"
                        link
                        v-if="row.status === '1'"
                        v-permission="'marketing:partner:addBlack'"
                        @click="handleAddBlack(row)"
                    >
                        [加入黑名单]
                    </el-button>

                    <el-button
                        type="warning"
                        link
                        v-if="row.status === '3'"
                        v-permission="'marketing:partner:removeBlack'"
                        @click="handleRemoveBlack(row)"
                    >
                        [移出黑名单]
                    </el-button>
                </div>
            </template>
        </CommonTable>

        <PartnerBlacklist ref="addBlack" @refresh="reload()" />
        <PartnerEdit ref="editRef" @success="reload()" />
    </div>
</template>

<script setup>
    import { ref, reactive, getCurrentInstance } from 'vue';
    import { EleMessage } from 'ele-admin-plus/es';
    import CommonTable from '@/components/CommonPage/CommonTable.vue';
    import PartnerSearch from './partner-search.vue';
    import PartnerBlacklist from './partner-blacklist.vue';
    import PartnerEdit from './partner-edit.vue';
    import request from '@/utils/request';
    import { DownloadOutlined, PlusOutlined } from '@/components/icons';
    // 表格实例
    const tableRef = ref(null);
    const searchRef = ref(null);
    const editRef = ref(null);

    // 页面配置
    const pageConfig = reactive({
        pageUrl: '/user/userPartnerInfo/pagelist',
        exportUrl: '/user/userPartnerInfo/export',
        fileName: '合伙人列表',
        cacheKey: 'partnerListTable',
        rowKey: 'userId'
    });

    // 表格列配置
    const columns = ref([
        {
            type: 'selection',
            columnKey: 'selection',
            width: 50,
            align: 'center',
            fixed: 'left'
        },
        { label: '用户ID', prop: 'userId', width: 100 },
        { label: '姓名', prop: 'name', minWidth: 100 },
        { label: '手机号', prop: 'mobile', width: 120 },
        { label: '地区', prop: 'address', minWidth: 180 },
        { label: '学校', prop: 'school', minWidth: 180 },
        { label: '院系', prop: 'faculties', minWidth: 180 },
        { label: '专业', prop: 'specialty', minWidth: 120 },
        { label: '年级', prop: 'grade', width: 100 },
        { label: '状态', prop: 'status', slot: 'status', width: 100 },
        { label: '申请时间', prop: 'applyTime', width: 160 },
        { label: '处理时间', prop: 'checkTime', width: 160 },
        {
            columnKey: 'action',
            label: '操作',
            width: 180,
            align: 'center',
            slot: 'action',
            fixed: 'right'
        }
    ]);

    // 搜索
    const handleSearch = (where) => {
        let data = JSON.parse(JSON.stringify(where));
        delete data.timeRange;
        tableRef.value?.reload(data);
    };

    // 导出Excel
    const handleExportExcel = () => {
        tableRef.value?.exportData();
    };

    //加入黑名单
    const addBlack = ref(null);
    function handleAddBlack(row) {
        addBlack.value?.handleOpen(row.userId);
    }

    // 通过审核
    const handleBatchPass = (row) => {
        let datas = row ? [row] : tableRef.value?.getSelections();
        if (!datas.length) return EleMessage.warning('请至少选择一条数据');
        let userIds = datas.map((v) => v.userId);
        tableRef.value?.messageBoxConfirm({
            message: '确认通过该合伙人申请？',
            fetch: () => request.post('/user/userPartnerInfo/pass', { userIds })
        });
    };

    //驳回审核
    const handleBatchReject = (row) => {
        let datas = row ? [row] : tableRef.value?.getSelections();
        if (!datas.length) return EleMessage.warning('请至少选择一条数据');
        let userIds = datas.map((v) => v.userId);
        tableRef.value?.messageBoxConfirm({
            message: '确认驳回该合伙人申请？',
            fetch: () =>
                request.post('/user/userPartnerInfo/reject', { userIds })
        });
    };

    // 编辑
    const handleEdit = (row) => {
        editRef.value?.handleOpen(row.userId);
    };

    // 移除黑名单操作
    const handleRemoveBlack = (row) => {
        tableRef.value?.messageBoxConfirm({
            message: '确认移除黑名单？',
            fetch: () =>
                request.post(`/user/userPartnerInfo/removeBlack/${row.userId}`)
        });
    };

    const reload = () => {
        tableRef.value?.reload();
    };

    defineExpose({
        reload
    });
</script>
