<template>
    <div>
        <search-layout :needBox="true">
            <template slot="searchItem1">
                <span class="label">场景名称</span>
                <el-input
                    v-model="searchParams.sceneName"
                    size="small"
                    maxlength="60"
                    clearable
                    placeholder="请输入"
                    @keyup.enter.native="doSearch"
                >
                </el-input>
            </template>
            <template slot="searchItem2">
                <span class="label">法规类型</span>
                <el-select v-model="searchParams.regulationType" multiple>
                    <el-option
                        v-for="item in regulationTypeList"
                        :label="item.caption"
                        :value="item.code"
                        :key="item.code"
                    ></el-option>
                </el-select>
            </template>
            <template slot="searchItem3">
                <span class="label">标准类型</span>
                <el-input
                    v-model="searchParams.standardType"
                    size="small"
                    maxlength="60"
                    clearable
                    placeholder="请输入"
                    @keyup.enter.native="doSearch"
                >
                </el-input>
            </template>
            <template slot="searchItem27">
                <el-checkbox-group v-model="labels">
                    <el-checkbox-button label="ISO" :key="1" class="labelA"
                        >ISO</el-checkbox-button
                    >
                    <el-checkbox-button label="GB" :key="2" class="labelB"
                        >GB</el-checkbox-button
                    >
                    <el-checkbox-button label="C-NCAP" :key="3" class="labelC"
                        >C-NCAP</el-checkbox-button
                    >
                    <el-checkbox-button label="E-NCAP" :key="4" class="labelD"
                        >E-NCAP</el-checkbox-button
                    >
                </el-checkbox-group>
            </template>
            <template slot="searchBtn1">
                <el-button type="primary" @click="doSearch">查询</el-button>
            </template>
            <template slot="searchBtn2">
                <el-button type="primary" @click="doReset">重置</el-button>
            </template>
        </search-layout>

        <div class="btnsPanel">
            <el-button
                type="primary"
                plain
                icon="el-icon-upload"
                @click="openFileDia"
                >上传文件</el-button
            >
        </div>

        <file-dialog
            ref="fileDialog"
            type="2"
            :objectPath="objectPath"
            :api="$api.sceneLibrary.saveStandardsRegulations"
            @fileClose="fileClose"
        ></file-dialog>

        <tableList
            ref="table"
            style="margin: 30px"
            :columns="columns"
            :getDataWay="getDataWay"
            :pagination="pagination"
            index
        >
            <el-table-column
                label="操作"
                slot="cgInfos"
                align="center"
                width="180"
            >
                <template v-slot="scope">
                    <i
                        @click="viewRow(scope.row)"
                        class="el-icon-view elIcon cursor"
                        title="查看"
                    >
                    </i>
                    <i
                        @click="delRow(scope.row)"
                        class="el-icon-delete elIcon"
                        title="删除"
                    >
                    </i>
                </template>
            </el-table-column>
        </tableList>

        <el-dialog
            :title="videoDiaTitle"
            :visible.sync="dialogVisible"
            width="690px"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :before-close="diaClose"
        >
            <div class="videoBox">
                <video :autoplay="autoplay" :src="videoSrc"></video>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import searchLayout from "@/components/grid/searchLayout";
import tableList from "@/components/grid/TableList";
import fileDialog from "./components/fileDialog";
import { mapState } from "vuex";

export default {
    name: "standardRegulationSimulationScenarioList", // 标准法规仿真场景
    components: { searchLayout, tableList, fileDialog },
    data() {
        return {
            searchParams: {
                //搜索参数
                sceneName: "", //场景名称
                regulationType: [], //法规类型
                standardType: "", //标准类型
                label: [],
            },
            labels: [], // 标签
            regulationTypeList: [],
            columns: [
                //表格列
                {
                    label: "场景名称",
                    prop: "sceneName",
                },
                {
                    label: "法规类型",
                    prop: "regulationType",
                },
                {
                    label: "标准类型",
                    prop: "standardType",
                },
                {
                    label: "标签",
                    prop: "regulationType",
                },
                {
                    label: "操作",
                    prop: "cgInfos",
                    template: true,
                    // videoAddress
                },
            ],
            pagination: {
                //分页使用
                currentPage: 1,
                pageSize: 10,
                position: "right",
                pageSizes: [10, 30, 50, 100, 200],
                layout: "sizes, total, prev, pager, next, jumper",
            },
            getDataWay: {
                //加载表格数据
                dataType: "url",
                type: "post",
                // firstRequest: false,
                data: this.$api.sceneLibrary.queryStandardsRegulationsList,
                param: {},
            },
            dialogVisible: false,
            autoplay: false,
            videoSrc: "",
            objectPath: "",
            videoDiaTitle: "",
        };
    },

    computed: {
        ...mapState(["fileHost", "fileUrl"]),
    },

    methods: {
        doSearch() {
            this.searchParams.label = [...this.labels];
            this.refreshList(this.searchParams);
        },
        //刷新table
        refreshList(param) {
            param
                ? this.$refs["table"].loadData(param)
                : this.$refs["table"].loadData();
        },
        doReset() {
            this.searchParams = {
                sceneName: "",
                regulationType: [],
                standardType: "",
                label: [],
            };
            this.labels = [];
            this.doSearch();
        },
        viewRow(row) {
            this.dialogVisible = true;
            this.videoDiaTitle = row.sceneName;

            let url = "";
            if (process.env.VUE_APP_IS_DEV == "true") {
                url = this.fileHost + this.fileUrl;
            } else {
                url = this.fileUrl;
            }

            let token = localStorage.getItem("Authorization").split(" ")[1];
            this.videoSrc = `${url}?objectName=${row.videoAddress}&access_token=${token}`;
            this.autoplay = true;
        },
        delRow(row) {
            this.$confirm("确认是否删除？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.$axios({
                    method: "post",
                    url: this.$api.sceneLibrary.deleteStandardsRegulations,
                    data: {
                        regulationsId: row.regulationsId,
                    },
                }).then((res) => {
                    if (res.code == 200) {
                        this.$message.success("删除成功");
                    } else {
                        this.$message.error(res.message || "删除失败");
                    }
                    this.doSearch();
                });
            });
        },
        diaClose(done) {
            this.autoplay = false;
            this.videoSrc = "";
            done();
        },
        openFileDia() {
            this.objectPath = Math.random().toString();
            this.$refs.fileDialog.fileVisible = true;
        },
        fileClose() {
            this.objectPath = "";
            this.doSearch();
        },
    },

    async mounted() {
        await this.$dicsListsInit({
            regulationTypeList: "regulationType",
        });
    },
};
</script>

<style lang='less' scoped>
@import "./common/util.less";
.btnsPanel {
    text-align: right;
}
</style>