<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-input
                        v-model="queryParams.roomId"
                        placeholder="请输入房间号"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="任务ID" prop="taskId">
                    <el-input
                        v-model="queryParams.taskId"
                        placeholder="请输入录制任务ID"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker
                        clearable
                        v-model="queryParams.startTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择录制开始时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker
                        clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择录制结束时间"
                    ></el-date-picker>
                </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:imLiveVideoFile:add']"
                    >
                        新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['service:imLiveVideoFile:edit']"
                    >
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['service:imLiveVideoFile:remove']"
                    >
                        删除
                    </el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['service:imLiveVideoFile: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="imLiveVideoFileList"
                @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="房间号" align="center" prop="roomId" />
                <el-table-column label="录制任务ID" align="center" prop="taskId" />
                <el-table-column label="文件名" align="center" prop="fileName" />
                <!-- <el-table-column label="COS文件存储key" align="center" prop="fileKey" /> -->
                <!-- <el-table-column label="文件大小(字节)" align="center" prop="fileSize" /> -->
                <!-- <el-table-column label="视频时长(秒)" align="center" prop="duration" /> -->
                <el-table-column label="录制开始时间" align="center" prop="startTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="录制结束时间" align="center" prop="endTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="COS存储地域" align="center" prop="cosRegion" /> -->
                <!-- <el-table-column label="COS存储桶" align="center" prop="cosBucket" /> -->
                <!-- <el-table-column label="下载URL" align="center" prop="downloadUrl" /> -->
                <el-table-column label="预览URL" align="center" prop="previewUrl">
                    <template #default="{ row }">
                        <el-link :href="row.previewUrl" target="_blank" type="primary">
                            视频预览链接
                        </el-link>
                    </template>
                </el-table-column>
                <el-table-column label="视频状态" align="center" prop="videoStatus">
                    <template #default="{ row }">
                        {{ row.videoStatus == 1 ? '已生成' : '已上传' }}
                    </template>
                </el-table-column>

                <!-- <el-table-column label="是否删除" align="center" prop="isDeleted" /> -->
                <!-- <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:imLiveVideoFile:edit']"
                        >
                            修改
                        </el-button>
                        <el-button
                            link
                            type="primary"
                            icon="Delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['service:imLiveVideoFile: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="imLiveVideoFileRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="房间号" prop="roomId">
                    <el-input v-model="form.roomId" placeholder="请输入房间号" />
                </el-form-item>
                <el-form-item label="录制任务ID" prop="taskId">
                    <el-input v-model="form.taskId" placeholder="请输入录制任务ID" />
                </el-form-item>
                <el-form-item label="文件名" prop="fileName">
                    <el-input v-model="form.fileName" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="COS文件存储key" prop="fileKey">
                    <el-input v-model="form.fileKey" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="文件大小(字节)" prop="fileSize">
                    <el-input v-model="form.fileSize" placeholder="请输入文件大小(字节)" />
                </el-form-item>
                <el-form-item label="视频时长(秒)" prop="duration">
                    <el-input v-model="form.duration" placeholder="请输入视频时长(秒)" />
                </el-form-item>
                <el-form-item label="录制开始时间" prop="startTime">
                    <el-date-picker
                        clearable
                        v-model="form.startTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择录制开始时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="录制结束时间" prop="endTime">
                    <el-date-picker
                        clearable
                        v-model="form.endTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择录制结束时间"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="COS存储地域" prop="cosRegion">
                    <el-input v-model="form.cosRegion" placeholder="请输入COS存储地域" />
                </el-form-item>
                <el-form-item label="COS存储桶" prop="cosBucket">
                    <el-input v-model="form.cosBucket" placeholder="请输入COS存储桶" />
                </el-form-item>
                <el-form-item label="下载URL" prop="downloadUrl">
                    <el-input v-model="form.downloadUrl" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="预览URL" prop="previewUrl">
                    <el-input v-model="form.previewUrl" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="是否删除" prop="isDeleted">
                    <el-input v-model="form.isDeleted" 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="ImLiveVideoFile">
import {
    listImLiveVideoFile,
    getImLiveVideoFile,
    delImLiveVideoFile,
    addImLiveVideoFile,
    updateImLiveVideoFile,
} from '@/api/service/imLiveVideoFile';

const { proxy } = getCurrentInstance();

const imLiveVideoFileList = 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,
        taskId: null,
        fileName: null,
        fileKey: null,
        fileSize: null,
        duration: null,
        startTime: null,
        endTime: null,
        cosRegion: null,
        cosBucket: null,
        downloadUrl: null,
        previewUrl: null,
        videoStatus: null,
        isDeleted: null,
    },
    rules: {
        roomId: [{ required: true, message: '房间号不能为空', trigger: 'blur' }],
        taskId: [{ required: true, message: '录制任务ID不能为空', trigger: 'blur' }],
        fileName: [{ required: true, message: '文件名不能为空', trigger: 'blur' }],
        fileKey: [{ required: true, message: 'COS文件存储key不能为空', trigger: 'blur' }],
        videoStatus: [
            {
                required: true,
                message: '视频状态: 0-录制中,1-已生成,2-已上传,3录制结束不能为空',
                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;
    listImLiveVideoFile(queryParams.value).then(response => {
        imLiveVideoFileList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

// 表单重置
function reset() {
    form.value = {
        id: null,
        roomId: null,
        taskId: null,
        fileName: null,
        fileKey: null,
        fileSize: null,
        duration: null,
        startTime: null,
        endTime: null,
        cosRegion: null,
        cosBucket: null,
        downloadUrl: null,
        previewUrl: null,
        videoStatus: null,
        createTime: null,
        updateTime: null,
        isDeleted: null,
    };
    proxy.resetForm('imLiveVideoFileRef');
}

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

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

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

getList();
</script>
