<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="直播间" prop="roomId">
                    <el-select
                        v-model="queryParams.roomId"
                        placeholder="请输入直播间名称"
                        filterable
                        remote
                        clearable
                        :remote-method="remoteMethodQuery"
                    >
                        <el-option
                            v-for="dict in imLiveRoomList"
                            :key="dict.roomId"
                            :label="dict.roomName"
                            :value="dict.roomId"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否启用" prop="isActive">
                    <el-select
                        v-model="queryParams.isActive"
                        placeholder="请选择是否启用：0-否 1-是"
                        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>
                    <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:imLiveVirtualConfig:add']"
                    >
                        新增
                    </el-button>
                </el-col>
                <!-- <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['service:imLiveVirtualConfig:edit']"
                    >
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['service:imLiveVirtualConfig:remove']"
                    >
                        删除
                    </el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['service:imLiveVirtualConfig: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="imLiveVirtualConfigList"
                @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="virtualCount" />
                <!-- <el-table-column label="虚拟用户昵称前缀" align="center" prop="nicknamePrefix" />
                <el-table-column label="头像池JSON数组" align="center" prop="avatarPool" />
                <el-table-column label="进入间隔(秒)" align="center" prop="enterInterval" />
                <el-table-column label="离开间隔(秒)" align="center" prop="leaveInterval" />
                <el-table-column label="最小观看时间(秒)" align="center" prop="minWatchTime" />
                <el-table-column label="最大观看时间(秒)" align="center" prop="maxWatchTime" />
                <el-table-column label="点赞概率" align="center" prop="likeProbability" />
                <el-table-column label="送礼概率" align="center" prop="giftProbability" /> -->
                <el-table-column label="是否启用" align="center" prop="isActive">
                    <template #default="scope">
                        <dict-tag :options="sys_general_yes_no" :value="scope.row.isActive" />
                    </template>
                </el-table-column>
                <!-- <el-table-column
                    label="最后同步时间"
                    align="center"
                    prop="lastSyncTime"
                    width="180"
                >
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.lastSyncTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column> -->
                <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:imLiveVirtualConfig:edit']"
                        >
                            修改
                        </el-button>
                        <!-- <el-button
                            link
                            type="primary"
                            icon="Delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['service:imLiveVirtualConfig: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="imLiveVirtualConfigRef"
                :model="form"
                :rules="rules"
                label-width="80px"
                label-position="top"
            >
                <el-form-item label="直播间ID" prop="roomId">
                    <el-select
                        v-model="form.roomId"
                        placeholder="请输入直播间名称"
                        filterable
                        remote
                        :remote-method="remoteMethod"
                    >
                        <el-option
                            v-for="dict in imLiveRoomList"
                            :key="dict.roomId"
                            :label="dict.roomName"
                            :value="dict.roomId"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="虚拟观众数量" prop="virtualCount">
                    <el-input v-model="form.virtualCount" placeholder="请输入虚拟观众数量" />
                </el-form-item>
                <el-form-item label="是否启用" prop="isActive">
                    <el-select v-model="form.isActive" placeholder="请选择是否启用：0-否 1-是">
                        <el-option
                            v-for="dict in sys_general_yes_no"
                            :key="dict.value"
                            :label="dict.label"
                            :value="parseInt(dict.value)"
                        ></el-option>
                    </el-select>
                </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="ImLiveVirtualConfig">
import {
    listImLiveVirtualConfig,
    getImLiveVirtualConfig,
    delImLiveVirtualConfig,
    addImLiveVirtualConfig,
    updateImLiveVirtualConfig,
} from '@/api/service/imLiveVirtualConfig';
import { listImLiveRoom } from '@/api/service/imLiveRoom';

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

const imLiveVirtualConfigList = ref([]);
const imLiveRoomList = 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,
        virtualCount: null,
        nicknamePrefix: null,
        avatarPool: null,
        enterInterval: null,
        leaveInterval: null,
        minWatchTime: null,
        maxWatchTime: null,
        likeProbability: null,
        giftProbability: null,
        isActive: null,
        lastSyncTime: null,
    },
    rules: {
        roomId: [{ required: true, message: '直播间ID不能为空', trigger: 'blur' }],
        virtualCount: [{ required: true, message: '虚拟观众数量不能为空', trigger: 'blur' }],
        enterInterval: [{ required: true, message: '进入间隔(秒)不能为空', trigger: 'blur' }],
        leaveInterval: [{ required: true, message: '离开间隔(秒)不能为空', trigger: 'blur' }],
        minWatchTime: [{ required: true, message: '最小观看时间(秒)不能为空', trigger: 'blur' }],
        maxWatchTime: [{ required: true, message: '最大观看时间(秒)不能为空', trigger: 'blur' }],
        likeProbability: [{ required: true, message: '点赞概率不能为空', trigger: 'blur' }],
        giftProbability: [{ required: true, message: '送礼概率不能为空', trigger: 'blur' }],
        isActive: [{ required: true, message: '是否启用：0-否 1-是不能为空', trigger: 'blur' }],
        createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
        updateTime: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }],
    },
});

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

/** 查询虚拟观众配置列表 */
function getList() {
    loading.value = true;
    listImLiveVirtualConfig(queryParams.value).then(response => {
        imLiveVirtualConfigList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

// 表单重置
function reset() {
    form.value = {
        id: null,
        roomId: null,
        virtualCount: null,
        nicknamePrefix: null,
        avatarPool: null,
        enterInterval: null,
        leaveInterval: null,
        minWatchTime: null,
        maxWatchTime: null,
        likeProbability: null,
        giftProbability: null,
        isActive: null,
        lastSyncTime: null,
        createTime: null,
        updateTime: null,
    };
    proxy.resetForm('imLiveVirtualConfigRef');
}

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

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

const remoteMethod = query => {
    if (query) {
        getLiveList(query, 1);
    } else {
        imLiveRoomList.value = [];
    }
};

const remoteMethodQuery = query => {
    if (query) {
        getLiveList(query, null);
    } else {
        imLiveRoomList.value = [];
    }
};

/** 查询直播间列表 */
function getLiveList(roomName, status) {
    let params = proxy.CloneData({
        pageNum: 1,
        pageSize: 20,
        roomName: roomName,
        status: status,
    });
    listImLiveRoom(params).then(response => {
        imLiveRoomList.value = response.rows;
    });
}

// 多选框选中数据
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;
    getImLiveVirtualConfig(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = '修改虚拟观众配置';
    });
}

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

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

getList();
</script>
