<template>
    <div class="list-view">
        <div class="filters-container">
            <el-form :model="form" inline size="mini" label-width="100px">
                <el-form-item>
                    <!-- <el-button @click="getData" type="primary" icon="el-icon-search">查询 </el-button>
                    <el-button @click="clearSearch" type="primary">清空 </el-button> -->
                    <!-- <el-button @click="addRow" type="primary" icon="el-icon-plus">添加 </el-button> -->
                    <el-button
                        @click="download"
                        type="primary"
                        icon="el-icon-download"
                        :loading="downloading"
                        :disabled="totalElements < 1"
                        >导出
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table
            :data="tableData"
            row-key="id"
            ref="table"
            empty-text="暂无数据"
            header-row-class-name="table-header-row"
            header-cell-class-name="table-header-cell"
            row-class-name="table-row"
            cell-class-name="table-cell"
            :height="tableHeight"
        >
            <el-table-column v-if="multipleMode" align="center" type="selection" width="50"> </el-table-column>
            <el-table-column prop="id" label="节目编号" width="80" fixed="left"> </el-table-column>
            <el-table-column prop="name" label="节目名称" fixed="left"> </el-table-column>
            <el-table-column prop="specialty" label="参赛专业"> </el-table-column>
            <el-table-column
                prop="competitionGroup"
                label="参赛组别"
                :formatter="competitionGroupFormatter"
                min-width="70"
            >
            </el-table-column>
            <el-table-column prop="level" label="参赛级别" min-width="70"> </el-table-column>
            <!-- <el-table-column prop="durationOfWork" label="作品时长" min-width="70"> </el-table-column> -->
            <el-table-column prop="quantity" label="参赛人数" min-width="70"> </el-table-column>
            <el-table-column prop="contact" label="联系人" min-width="68"> </el-table-column>
            <el-table-column prop="gradingOrganizationId" label="考级机构" show-overflow-tooltip min-width="160">
                <template slot="header" slot-scope="{ column }">
                    <sortable-header :column="column" :current-sort="sort" @changeSort="changeSort"> </sortable-header>
                </template>
                <template slot-scope="{ row }">
                    <span>{{ row.gradingOrganization }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="organizationId" label="承办单位" min-width="160">
                <template slot="header" slot-scope="{ column }">
                    <sortable-header :column="column" :current-sort="sort" @changeSort="changeSort"> </sortable-header>
                </template>
                <template slot-scope="{ row }">
                    <span>{{ row.organization }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="examPoint" label="考级点" min-width="160">
                <template slot="header" slot-scope="{ column }">
                    <sortable-header :column="column" :current-sort="sort" @changeSort="changeSort"> </sortable-header>
                </template>
            </el-table-column>
            <el-table-column
                prop="programmeStatus"
                label="节目状态"
                min-width="100"
                :formatter="programmeStatusFormatter"
            ></el-table-column>
            <el-table-column label="操作" align="left" fixed="right" min-width="180">
                <template slot-scope="{ row, $index }">
                    <el-button @click="showRow(row)" size="mini" plain>查看</el-button>
                    <el-button type="warning" @click="playVideo(row, $index)" size="mini" plain>查看作品</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-wrapper">
            <el-pagination
                background
                @size-change="onSizeChange"
                @current-change="onCurrentChange"
                :current-page="page"
                :page-sizes="[10, 20, 30, 40, 50]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalElements"
            >
            </el-pagination>
        </div>
        <el-dialog
            :title="programme.name"
            class="videoDialog"
            destroy-on-close
            center
            append-to-body
            :visible.sync="showViedo"
            width="auto"
        >
            <video
                :src="programme.video"
                controls
                style="max-height: 600px; max-width: 100%; margin: 0 auto"
                v-if="programme.video"
            >
                您的浏览器不支持 video 标签。
            </video>
            <img
                style="max-height: 600px; max-width: 100%; display: block; margin: auto"
                :src="programme.annex"
                alt=""
                v-if="programme.annex"
            />
            <div style="display: block; margin: 10px;">
                <el-button size="mini" @click="move(-1)" :disabled="index == 0">上一个</el-button>
                <el-button size="mini" @click="move(1)" :disabled="(page - 1) * pageSize + index == totalElements - 1"
                    >下一个</el-button
                >
                <el-button @click="showViedo = false" size="mini">关闭</el-button>
            </div>
        </el-dialog>
        <programme-log :dialogVisible="isShow" @close="isShow = false" ref="public"></programme-log>
    </div>
</template>
<script>
import delChild from '@/mixins/delChild';
import { mapState } from 'vuex';
import pageableTable from '@/mixins/pageableTable';
import ProgrammeLog from '@/components/ProgrammeLog1.vue';
export default {
    name: 'ProgrammeList',
    mixins: [pageableTable, delChild],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/programme/showAll',
            downloading: false,
            competitionGroupOptions: [
                { label: '个人', value: 'SINGLE' },
                { label: '集体', value: 'COLLECTIVE' }
            ],
            form: {},
            levelSingleOptions: [],
            levelCollectiveOptions: [],
            dialogUrl: '',
            dialogCode: false,
            isShow: false,
            gradingOrganizationIdOptions: [],
            organizationIdOptions: [],
            performanceId: '',
            performances: [],
            performance: {},
            artTypes: [],
            optionProps: {
                value: 'id',
                label: 'name',
                children: 'children',
                multiple: false,
                emitPath: false,
                checkStrictly: true,
                expandTrigger: 'hover'
            },
            showViedo: false,
            showImg: false,
            videoUrl: '',
            annex: '',
            programmeStatusOptions: [
                { label: '未提交', value: 'INITIAL' },
                { label: '已提交', value: 'SUBMIT' },
                { label: '初选未通过', value: 'AUDIT_FAILED' },
                { label: '审核未通过', value: 'REVIEW_FAILED' }
            ],
            showMore: false,
            programme: {},
            index: 0
        };
    },
    created() {},
    components: {
        ProgrammeLog
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        programmeStatusFormatter(row, column, cellValue, index) {
            let selectedOption = this.programmeStatusOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        competitionGroupFormatter(row, column, cellValue, index) {
            let selectedOption = this.competitionGroupOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        beforeGetData() {
            let data = {
                query: {}
            };
            if (this.$route.query.gid) {
                data.query.gradingOrganizationId = this.$route.query.gid;
            } else {
                data.query.gradingOrganizationId = 0;
            }
            return data;
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        addRow() {
            this.$router.push({
                path: '/programmeEdit',
                query: {
                    ...this.$route.query
                }
            });
        },
        showRow(row) {
            this.isShow = true;
            this.$refs.public.dataApi(row.id);
        },
        download() {
            this.downloading = true;

            let data = {
                sort: 'gradingOrganizationId,asc;organizationId,asc;examPoint,asc',
                size: 2000,
                query: {
                    programmeStatus: 'SUBMIT'
                }
            };
            if (this.$route.query.gid) {
                data.query.gradingOrganizationId = this.$route.query.gid;
            } else {
                data.query.gradingOrganizationId = 0;
            }
            this.$axios
                .get('/programme/excelGO', {
                    responseType: 'blob',
                    params: data
                })
                .then(res => {
                    console.log(res);
                    this.downloading = false;
                    const downloadUrl = window.URL.createObjectURL(new Blob([res.data]));
                    const link = document.createElement('a');
                    link.href = downloadUrl;
                    link.setAttribute('download', res.headers['content-disposition'].split('filename=')[1]);
                    document.body.appendChild(link);
                    link.click();
                    link.remove();
                })
                .catch(e => {
                    console.log(e);
                    this.downloading = false;
                    this.$message.error(e.error);
                });
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },
        clearSearch() {
            this.form = {};
            this.getData();
        },
        getCode(value) {
            return this.forTree(this.artTypes, value).code;
        },
        forTree(list, value) {
            var result = null;
            if (!list) {
                return;
            }
            for (var i in list) {
                if (result !== null) {
                    break;
                }
                var item = list[i];
                if (item.id == value) {
                    result = item;
                    break;
                } else if (item.children && item.children.length > 0) {
                    result = this.forTree(item.children, value);
                }
            }
            return result;
        },
        // closeEvent() {
        //     document.exitPictureInPicture();
        // },
        // playVideo(row) {
        //     if (row.video) {
        //         this.showViedo = true;
        //         this.videoUrl = row.video;
        //     } else {
        //         this.$message.success('暂无视频');
        //     }
        // },
        playImg(row) {
            if (row.annex) {
                this.showImg = true;
                this.annex = row.annex;
            } else {
                this.$message.success('暂无图片');
            }
        },
        playVideo(row, index) {
            this.index = index;
            this.showViedo = true;
            this.programme = row;
        },
        move(direction) {
            const end = direction + this.index;
            this.programme = { ...this.tableData[end] };
            this.index = end;
        }
    }
};
</script>
<style lang="less" scoped>
.right {
    float: right;
}
/deep/.el-form-item--mini.el-form-item,
.el-form-item--small.el-form-item {
    margin-bottom: 10px;
}
.videoDialog {
    display: flex;
    align-items: center;
    justify-content: center;
    .el-dialog {
        max-width: 900px;
        margin-top: 0px;

        .close {
            position: absolute;
            right: 0px;
            top: -42px;
            width: 71px;
            height: 32px;
            background: #00000015;

            font-size: 12px;
            color: #fdffff;
            line-height: 32px;
            text-align: center;
            cursor: pointer;

            &:hover {
                background: #00000055;
            }
        }
    }
    .el-dialog__header {
        display: none;
    }

    .el-dialog__body {
        padding: 0;

        video {
            display: block;
            height: auto;
            width: 100%;
            outline: none;
        }
    }
}
</style>
