<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="roomId">
                    <el-input
                        v-model="queryParams.roomId"
                        placeholder="请输入直播间ID"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="观众账号" prop="viewerAccount">
                    <el-input
                        v-model="queryParams.viewerAccount"
                        placeholder="请输入观众账号"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </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:imLiveViewer:add']"
                    >
                        新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['service:imLiveViewer:edit']"
                    >
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['service:imLiveViewer:remove']"
                    >
                        删除
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['service:imLiveViewer: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="imLiveViewerList"
                @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="roomId" />
                <el-table-column label="观众账号" align="center" prop="viewerAccount" />
                <el-table-column
                    label="观众类型：1-真实用户 2-虚拟用户"
                    align="center"
                    prop="viewerType"
                />
                <el-table-column label="观众昵称" align="center" prop="nickname" />
                <el-table-column label="头像URL" align="center" prop="avatarUrl" />
                <el-table-column label="进入时间" align="center" prop="enterTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.enterTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="离开时间" align="center" prop="leaveTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.leaveTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="观看时长(秒)" align="center" prop="watchDuration" />
                <el-table-column label="是否禁言：0-否 1-是" align="center" prop="isMuted" />
                <el-table-column label="是否被踢出：0-否 1-是" align="center" prop="isKicked" />
                <el-table-column label="点赞数" align="center" prop="likeCount" />
                <el-table-column label="送礼数" align="center" prop="giftCount" />
                <el-table-column
                    label="最后活跃时间"
                    align="center"
                    prop="lastActiveTime"
                    width="180"
                >
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.lastActiveTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="状态：1-观看中 2-已离开" align="center" prop="status" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button
                            link
                            type="primary"
                            icon="Edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['service:imLiveViewer:edit']"
                        >
                            修改
                        </el-button>
                        <el-button
                            link
                            type="primary"
                            icon="Delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['service:imLiveViewer: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="imLiveViewerRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="直播间ID" prop="roomId">
                    <el-input v-model="form.roomId" placeholder="请输入直播间ID" />
                </el-form-item>
                <el-form-item label="观众账号" prop="viewerAccount">
                    <el-input v-model="form.viewerAccount" placeholder="请输入观众账号" />
                </el-form-item>
                <el-form-item label="观众昵称" prop="nickname">
                    <el-input v-model="form.nickname" placeholder="请输入观众昵称" />
                </el-form-item>
                <el-form-item label="头像URL" prop="avatarUrl">
                    <el-input v-model="form.avatarUrl" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="进入时间" prop="enterTime">
                    <el-date-picker
                        clearable
                        v-model="form.enterTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择进入时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="离开时间" prop="leaveTime">
                    <el-date-picker
                        clearable
                        v-model="form.leaveTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择离开时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="观看时长(秒)" prop="watchDuration">
                    <el-input v-model="form.watchDuration" placeholder="请输入观看时长(秒)" />
                </el-form-item>
                <el-form-item label="是否禁言：0-否 1-是" prop="isMuted">
                    <el-input v-model="form.isMuted" placeholder="请输入是否禁言：0-否 1-是" />
                </el-form-item>
                <el-form-item label="是否被踢出：0-否 1-是" prop="isKicked">
                    <el-input v-model="form.isKicked" placeholder="请输入是否被踢出：0-否 1-是" />
                </el-form-item>
                <el-form-item label="点赞数" prop="likeCount">
                    <el-input v-model="form.likeCount" placeholder="请输入点赞数" />
                </el-form-item>
                <el-form-item label="送礼数" prop="giftCount">
                    <el-input v-model="form.giftCount" placeholder="请输入送礼数" />
                </el-form-item>
                <el-form-item label="最后活跃时间" prop="lastActiveTime">
                    <el-date-picker
                        clearable
                        v-model="form.lastActiveTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择最后活跃时间"
                    ></el-date-picker>
                </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="ImLiveViewer">
import {
    listImLiveViewer,
    getImLiveViewer,
    delImLiveViewer,
    addImLiveViewer,
    updateImLiveViewer,
} from '@/api/service/imLiveViewer';

const { proxy } = getCurrentInstance();

const imLiveViewerList = 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,
        roomId: null,
        viewerAccount: null,
        viewerType: null,
        nickname: null,
        avatarUrl: null,
        enterTime: null,
        leaveTime: null,
        watchDuration: null,
        isMuted: null,
        isKicked: null,
        likeCount: null,
        giftCount: null,
        lastActiveTime: null,
        status: null,
    },
    rules: {
        roomId: [{ required: true, message: '直播间ID不能为空', trigger: 'blur' }],
        viewerAccount: [{ required: true, message: '观众账号不能为空', trigger: 'blur' }],
        viewerType: [
            {
                required: true,
                message: '观众类型：1-真实用户 2-虚拟用户不能为空',
                trigger: 'change',
            },
        ],
        enterTime: [{ required: true, message: '进入时间不能为空', trigger: 'blur' }],
        watchDuration: [{ required: true, message: '观看时长(秒)不能为空', trigger: 'blur' }],
        isMuted: [{ required: true, message: '是否禁言：0-否 1-是不能为空', trigger: 'blur' }],
        isKicked: [{ required: true, message: '是否被踢出：0-否 1-是不能为空', trigger: 'blur' }],
        likeCount: [{ required: true, message: '点赞数不能为空', trigger: 'blur' }],
        giftCount: [{ required: true, message: '送礼数不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '状态：1-观看中 2-已离开不能为空', trigger: 'change' }],
        createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
        updateTime: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }],
    },
});

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

/** 查询直播观众列表 */
function getList() {
    loading.value = true;
    listImLiveViewer(queryParams.value).then(response => {
        imLiveViewerList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

// 表单重置
function reset() {
    form.value = {
        id: null,
        roomId: null,
        viewerAccount: null,
        viewerType: null,
        nickname: null,
        avatarUrl: null,
        enterTime: null,
        leaveTime: null,
        watchDuration: null,
        isMuted: null,
        isKicked: null,
        likeCount: null,
        giftCount: null,
        lastActiveTime: null,
        status: null,
        createTime: null,
        updateTime: null,
    };
    proxy.resetForm('imLiveViewerRef');
}

/** 搜索按钮操作 */
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;
    getImLiveViewer(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = '修改直播观众';
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs['imLiveViewerRef'].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateImLiveViewer(form.value).then(response => {
                    proxy.$modal.msgSuccess('修改成功');
                    open.value = false;
                    getList();
                });
            } else {
                addImLiveViewer(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 delImLiveViewer(_ids);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
}

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

getList();
</script>
