<template>
    <div class="app-container">
        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-form
                :model="queryParams"
                ref="queryRef"
                :inline="true"
                v-show="showSearch"
                label-width="68px"
            >
                <el-form-item label="用户ID" prop="userId">
                    <el-input
                        v-model="queryParams.userId"
                        placeholder="请输入用户ID"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="真实姓名" prop="realName">
                    <el-input
                        v-model="queryParams.realName"
                        placeholder="请输入真实姓名"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="身份证号" prop="idCard">
                    <el-input
                        v-model="queryParams.idCard"
                        placeholder="请输入身份证号"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="联系电话" prop="phone">
                    <el-input
                        v-model="queryParams.phone"
                        placeholder="请输入联系电话"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input
                        v-model="queryParams.email"
                        placeholder="请输入邮箱"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="主播昵称" prop="nickname">
                    <el-input
                        v-model="queryParams.nickname"
                        placeholder="请输入主播昵称"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="是否推荐" prop="isRecommend">
                    <el-select
                        v-model="queryParams.isRecommend"
                        placeholder="请选择是否推荐主播"
                        clearable
                    >
                        <el-option
                            v-for="dict in sys_general_yes_no"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="审核状态" prop="status">
                    <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
                        <el-option label="待审核" :value="0" />
                        <el-option label="审核通过" :value="1" />
                        <el-option label="审核拒绝" :value="2" />
                        <el-option label="已封禁" :value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAdd"
                        v-hasPermi="['service:liveAnchor:add']"
                    >
                        新增
                    </el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['service:liveAnchor:edit']"
                    >
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['service:liveAnchor:remove']"
                    >
                        删除
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['service:liveAnchor:export']"
                    >
                        导出
                    </el-button>
                </el-col>
                <right-toolbar
                    v-model:showSearch="showSearch"
                    @queryTable="getList"
                ></right-toolbar>
            </el-row>
        </div>

        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-table
                v-loading="loading"
                :data="liveAnchorList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="主播ID" align="center" prop="id" />
                <el-table-column label="用户ID" align="center" prop="userId" />
                <el-table-column label="真实姓名" align="center" prop="realName" />
                <el-table-column label="身份证号" align="center" prop="idCard" />
                <el-table-column label="身份证正面照" align="center" prop="idCardFront" width="100">
                    <template #default="scope">
                        <image-preview :src="scope.row.idCardFront" :width="50" :height="50" />
                    </template>
                </el-table-column>
                <el-table-column label="身份证背面照" align="center" prop="idCardBack" width="100">
                    <template #default="scope">
                        <image-preview :src="scope.row.idCardBack" :width="50" :height="50" />
                    </template>
                </el-table-column>
                <el-table-column label="联系电话" align="center" prop="phone" />
                <el-table-column label="邮箱" align="center" prop="email" />
                <!-- <el-table-column label="主播昵称" align="center" prop="nickname" /> -->
                <!-- <el-table-column label="主播头像" align="center" prop="avatar" width="100">
                    <template #default="scope">
                        <image-preview :src="scope.row.avatar" :width="50" :height="50" />
                    </template>
                </el-table-column> -->
                <!-- <el-table-column label="性别：0-未知 1-男 2-女" align="center" prop="gender" /> -->
                <el-table-column label="审核状态" align="center" prop="status">
                    <template #default="{ row }">
                        {{
                            row.status == 0
                                ? '待审核'
                                : row.status == 1
                                ? '审核通过'
                                : row.status == 2
                                ? '审核拒绝'
                                : '已封禁'
                        }}
                    </template>
                </el-table-column>
                <!-- <el-table-column label="拒绝原因" align="center" prop="rejectReason" /> -->
                <!-- <el-table-column label="封禁原因" align="center" prop="banReason" /> -->
                <!-- <el-table-column label="封禁结束时间" align="center" prop="banEndTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.banEndTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column> -->
                <!-- <el-table-column label="总直播时长(小时)" align="center" prop="totalLiveHours" /> -->
                <!-- <el-table-column label="总直播次数" align="center" prop="totalLiveCount" /> -->
                <!-- <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="审核管理员ID" align="center" prop="auditAdminId" />
                <el-table-column label="审核管理员姓名" align="center" prop="auditAdminName" /> -->
                <el-table-column label="是否推荐主播" align="center" prop="isRecommend">
                    <template #default="scope">
                        <dict-tag :options="sys_general_yes_no" :value="scope.row.isRecommend" />
                    </template>
                </el-table-column>
                <el-table-column label="推荐排序" align="center" prop="recommendOrder" />
                <!-- <el-table-column
                    label="最后直播时间"
                    align="center"
                    prop="lastLiveTime"
                    width="180"
                >
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.lastLiveTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column> -->
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                    fixed="right"
                >
                    <template #default="scope">
                        <el-button
                            link
                            type="primary"
                            icon="Edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['service:liveAnchor:edit']"
                        >
                            审核
                        </el-button>
                        <el-button
                            link
                            type="primary"
                            icon="Delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['service:liveAnchor:remove']"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
            />
        </div>

        <!-- 添加或修改直播主播对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form
                ref="liveAnchorRef"
                :model="form"
                :rules="rules"
                label-width="80px"
                label-position="top"
            >
                <el-form-item label="用户ID" prop="userId">
                    <el-input v-model="form.userId" disabled placeholder="请输入用户ID" />
                </el-form-item>
                <el-form-item label="真实姓名" prop="realName">
                    <el-input v-model="form.realName" disabled placeholder="请输入真实姓名" />
                </el-form-item>
                <el-form-item label="身份证号" prop="idCard">
                    <el-input v-model="form.idCard" disabled placeholder="请输入身份证号" />
                </el-form-item>
                <el-form-item label="身份证正面照" prop="idCardFront">
                    <image-upload v-model="form.idCardFront" :limit="1" />
                </el-form-item>
                <el-form-item label="身份证背面照" prop="idCardBack">
                    <image-upload v-model="form.idCardBack" :limit="1" />
                </el-form-item>
                <el-form-item label="联系电话" prop="phone">
                    <el-input v-model="form.phone" placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email" placeholder="请输入邮箱" />
                </el-form-item>
                <!-- <el-form-item label="主播昵称" prop="nickname">
                    <el-input v-model="form.nickname" placeholder="请输入主播昵称" />
                </el-form-item>
                <el-form-item label="主播头像" prop="avatar">
                    <image-upload v-model="form.avatar" />
                </el-form-item> -->
                <el-form-item label="审核状态" prop="status">
                    <el-select v-model="form.status" placeholder="请选择审核状态" clearable>
                        <el-option label="待审核" :value="0" />
                        <el-option label="审核通过" :value="1" />
                        <el-option label="审核拒绝" :value="2" />
                        <el-option label="已封禁" :value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="拒绝原因" prop="rejectReason" v-if="form.status == 2">
                    <el-input
                        v-model="form.rejectReason"
                        type="textarea"
                        placeholder="请输入内容"
                    />
                </el-form-item>
                <el-form-item label="封禁结束时间" prop="banEndTime" v-if="form.status == 3">
                    <el-date-picker
                        clearable
                        v-model="form.banEndTime"
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="请选择封禁结束时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="封禁原因" prop="banReason" v-if="form.status == 3">
                    <el-input v-model="form.banReason" type="textarea" placeholder="请输入内容" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="LiveAnchor">
import {
    listLiveAnchor,
    getLiveAnchor,
    delLiveAnchor,
    addLiveAnchor,
    updateLiveAnchor,
} from '@/api/service/liveAnchor';

const { proxy } = getCurrentInstance();
const { sys_general_yes_no } = proxy.useDict('sys_general_yes_no');

const liveAnchorList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        realName: null,
        idCard: null,
        idCardFront: null,
        idCardBack: null,
        phone: null,
        email: null,
        nickname: null,
        avatar: null,
        signature: null,
        gender: null,
        status: null,
        rejectReason: null,
        banReason: null,
        banEndTime: null,
        fansCount: null,
        followCount: null,
        totalLiveHours: null,
        totalLiveCount: null,
        totalIncome: null,
        auditTime: null,
        auditAdminId: null,
        auditAdminName: null,
        isRecommend: null,
        recommendOrder: null,
        lastLiveTime: null,
    },
    rules: {
        userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
        realName: [{ required: true, message: '真实姓名不能为空', trigger: 'blur' }],
        idCard: [{ required: true, message: '身份证号不能为空', trigger: 'blur' }],
        banEndTime: [{ required: true, message: '封禁结束时间不能为空', trigger: 'blur' }],
        banReason: [{ required: true, message: '封禁原因不能为空', trigger: 'blur' }],
        rejectReason: [{ required: true, message: '拒绝原因不能为空', trigger: 'blur' }],
        status: [
            {
                required: true,
                message: '审核状态：0-待审核 1-审核通过 2-审核拒绝 3-已封禁不能为空',
                trigger: 'change',
            },
        ],
    },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询直播主播列表 */
function getList() {
    loading.value = true;
    listLiveAnchor(queryParams.value).then(response => {
        liveAnchorList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        userId: null,
        realName: null,
        idCard: null,
        idCardFront: null,
        idCardBack: null,
        phone: null,
        email: null,
        nickname: null,
        avatar: null,
        signature: null,
        gender: null,
        status: null,
        rejectReason: null,
        banReason: null,
        banEndTime: null,
        fansCount: null,
        followCount: null,
        totalLiveHours: null,
        totalLiveCount: null,
        totalIncome: null,
        auditTime: null,
        auditAdminId: null,
        auditAdminName: null,
        isRecommend: null,
        recommendOrder: null,
        lastLiveTime: null,
        createTime: null,
        updateTime: null,
    };
    proxy.resetForm('liveAnchorRef');
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm('queryRef');
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = '添加直播主播';
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value;
    getLiveAnchor(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = '修改直播主播';
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs['liveAnchorRef'].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateLiveAnchor(form.value).then(response => {
                    proxy.$modal.msgSuccess('修改成功');
                    open.value = false;
                    getList();
                });
            } else {
                addLiveAnchor(form.value).then(response => {
                    proxy.$modal.msgSuccess('新增成功');
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal
        .confirm('是否确认删除直播主播编号为"' + _ids + '"的数据项？')
        .then(function () {
            return delLiveAnchor(_ids);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download(
        'service/liveAnchor/export',
        {
            ...queryParams.value,
        },
        `liveAnchor_${new Date().getTime()}.xlsx`
    );
}

getList();
</script>
