<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="groupId">
                    <el-input
                        v-model="queryParams.groupId"
                        placeholder="请输入腾讯云群组ID"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="群组名称" prop="groupName">
                    <el-input
                        v-model="queryParams.groupName"
                        placeholder="请输入群组名称"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <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="ownerAccount">
                    <el-input
                        v-model="queryParams.ownerAccount"
                        placeholder="请输入群主账号"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="加群方式" prop="applyJoinOption">
                    <el-select
                        v-model="queryParams.applyJoinOption"
                        placeholder="请选择申请加群方式"
                    >
                        <el-option label="自由加入" value="FreeAccess" />
                        <el-option label="需要验证" value="NeedPermission" />
                        <el-option label="禁止加群" value="DisableApply" />
                    </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:imGroup:add']"
                    >
                        新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['service:imGroup:edit']"
                    >
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['service:imGroup:remove']"
                    >
                        删除
                    </el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['service:imGroup: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="imGroupList"
                @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="groupId" />
                <el-table-column label="群组名称" align="center" prop="groupName" />
                <el-table-column label="群组类型" align="center" prop="groupType">
                    <template #default="scope">
                        {{
                            scope.row.groupType == 'Private'
                                ? '好友工作群'
                                : scope.row.groupType == 'Public'
                                  ? '陌生人社交群'
                                  : '直播群'
                        }}
                    </template>
                </el-table-column>
                <el-table-column label="群主用户id" align="center" prop="userId" />
                <el-table-column label="群主账号" align="center" prop="ownerAccount" />
                <!-- <el-table-column label="群简介" align="center" prop="introduction" /> -->
                <!-- <el-table-column label="群公告" align="center" prop="notification" /> -->
                <el-table-column label="群头像URL" align="center" prop="faceUrl">
                    <template #default="scope">
                        <image-preview :src="scope.row.faceUrl"></image-preview>
                    </template>
                </el-table-column>
                <el-table-column label="最大成员数" align="center" prop="maxMemberCount" />
                <el-table-column label="申请加群方式" align="center" prop="applyJoinOption">
                    <template #default="scope">
                        {{
                            scope.row.applyJoinOption == 'FreeAccess'
                                ? '自由加入'
                                : scope.row.applyJoinOption == 'NeedPermission'
                                  ? '需要验证'
                                  : '禁止加群'
                        }}
                    </template>
                </el-table-column>
                <el-table-column label="当前成员数" align="center" prop="currentMemberCount" />
                <el-table-column label="状态" align="center" prop="status">
                    <template #default="scope">
                        {{
                            scope.row.status == 0 ? '解散' : scope.row.status == 1 ? '正常' : '封禁'
                        }}
                    </template>
                </el-table-column>
                <el-table-column label="版本号" align="center" prop="version" />
                <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:imGroup:edit']"
                        >
                            修改
                        </el-button>
                        <el-button
                            link
                            type="primary"
                            icon="Delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['service:imGroup:remove']"
                        >
                            删除
                        </el-button> -->
                        <el-button
                            link
                            type="primary"
                            icon="Edit"
                            @click="sendGroupNotice(scope.row)"
                            v-hasPermi="['service:imGroup:edit']"
                        >
                            群通知
                        </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>

        <!-- 添加或修改腾讯云IM群组对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form
                ref="imGroupRef"
                :model="form"
                :rules="rules"
                label-width="80px"
                label-position="top"
            >
                <el-form-item label="腾讯云群组ID" prop="groupId">
                    <el-input v-model="form.groupId" disabled placeholder="请输入腾讯云群组ID" />
                </el-form-item>
                <el-form-item label="群组名称" prop="groupName">
                    <el-input v-model="form.groupName" placeholder="请输入群组名称" />
                </el-form-item>
                <el-form-item label="群主用户id" prop="userId">
                    <el-input v-model="form.userId" disabled placeholder="请输入群主用户id" />
                </el-form-item>
                <el-form-item label="群主账号" prop="ownerAccount">
                    <el-input v-model="form.ownerAccount" disabled placeholder="请输入群主账号" />
                </el-form-item>
                <el-form-item label="群简介" prop="introduction">
                    <el-input
                        v-model="form.introduction"
                        type="textarea"
                        placeholder="请输入内容"
                    />
                </el-form-item>
                <el-form-item label="群公告" prop="notification">
                    <el-input
                        v-model="form.notification"
                        type="textarea"
                        placeholder="请输入内容"
                    />
                </el-form-item>
                <el-form-item label="群头像URL" prop="faceUrl">
                    <image-upload v-model="form.faceUrl" />
                </el-form-item>
                <el-form-item label="最大成员数" prop="maxMemberCount">
                    <el-input v-model="form.maxMemberCount" placeholder="请输入最大成员数" />
                </el-form-item>
                <el-form-item label="申请加群方式" prop="applyJoinOption">
                    <el-select v-model="form.applyJoinOption" placeholder="请选择申请加群方式">
                        <el-option label="自由加入" value="FreeAccess" />
                        <el-option label="需要验证" value="NeedPermission" />
                        <el-option label="禁止加群" value="DisableApply" />
                    </el-select>
                </el-form-item>
                <el-form-item label="当前成员数" prop="currentMemberCount">
                    <el-input v-model="form.currentMemberCount" 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="ImGroup">
import {
    listImGroup,
    getImGroup,
    delImGroup,
    addImGroup,
    updateImGroup,
    sendGroupNotif,
} from '@/api/service/imGroup';
import { el } from 'element-plus/es/locales.mjs';

const { proxy } = getCurrentInstance();

const imGroupList = 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,
        groupId: null,
        groupName: null,
        groupType: null,
        userId: null,
        ownerAccount: null,
        introduction: null,
        notification: null,
        faceUrl: null,
        maxMemberCount: null,
        applyJoinOption: null,
        currentMemberCount: null,
        status: null,
        version: null,
    },
    rules: {
        groupId: [{ required: true, message: '腾讯云群组ID不能为空', trigger: 'blur' }],
        groupName: [{ required: true, message: '群组名称不能为空', trigger: 'blur' }],
        groupType: [
            {
                required: true,
                message: '群组类型：Private/Public/ChatRoom/AVChatRoom不能为空',
                trigger: 'change',
            },
        ],
        userId: [{ required: true, message: '群主用户id不能为空', trigger: 'blur' }],
        createTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
        updateTime: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }],
    },
});

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

/** 查询腾讯云IM群组列表 */
function getList() {
    loading.value = true;
    listImGroup(queryParams.value).then(response => {
        imGroupList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 发送群组通知
function sendGroupNotice(row) {
    proxy.$modal
        .prompt('')
        .then(({ value }) => {
            sendGroupNotif({ groupId: row.groupId, content: value }).then(response => {
                proxy.$modal.msgSuccess(response.message);
            });
        })
        .catch(() => {});
}

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

// 表单重置
function reset() {
    form.value = {
        id: null,
        groupId: null,
        groupName: null,
        groupType: null,
        userId: null,
        ownerAccount: null,
        introduction: null,
        notification: null,
        faceUrl: null,
        maxMemberCount: null,
        applyJoinOption: null,
        currentMemberCount: null,
        status: null,
        createTime: null,
        updateTime: null,
        version: null,
    };
    proxy.resetForm('imGroupRef');
}

/** 搜索按钮操作 */
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 = '添加腾讯云IM群组';
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value;
    getImGroup(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = '修改腾讯云IM群组';
    });
}

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

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

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

getList();
</script>
