<template>
    <div class="list-view">
        <div class="filters-container">
            <el-table
                :data="arrange"
                row-key="id"
                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="80"
            >
                <el-table-column prop="address" label="活动地点"> </el-table-column>
                <el-table-column prop="name" label="分组名称" min-width="100"> </el-table-column>
                <el-table-column label="时间安排" min-width="100">
                    <template slot-scope="{ row }"> {{ getTime(row.startTime) }}-{{ getTime(row.endTime) }} </template>
                </el-table-column>
                <el-table-column prop="quantity" label="节目数量"> </el-table-column>
                <!-- <el-table-column prop="duration" label="节目时长"> </el-table-column> -->
                <el-table-column label="参赛专业">
                    <template slot-scope="{ row }">
                        {{ row.specialtyName.join(',') }}
                    </template>
                </el-table-column>
                <el-table-column prop="writerDirector" label="编导姓名"> </el-table-column>
                <el-table-column prop="phone" label="联系电话"> </el-table-column>
            </el-table>
            <el-button style="margin: 10px" @click="intoGroup" type="primary">添加节目</el-button>
        </div>
        <el-table
            :data="tableData"
            row-key="id"
            ref="table"
            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="ID" width="100"> </el-table-column> -->
            <el-table-column prop="name" label="节目名称"> </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="phone" label="联系电话" min-width="95"> </el-table-column>
            <el-table-column prop="gradingOrganization" label="考级机构" show-overflow-tooltip min-width="160">
            </el-table-column>
            <el-table-column prop="organization" label="承办单位" min-width="160"> </el-table-column>
            <el-table-column prop="examPoint" label="考级点" min-width="160"> </el-table-column>
            <el-table-column label="操作" align="left" fixed="right" min-width="280">
                <template slot-scope="{ row }">
                    <el-button @click="showRow(row)" size="mini" plain>查看</el-button>
                    <el-button @click="removeRow(row)" type="danger" size="mini" plain>移除</el-button>
                    <el-button
                        type="warning"
                        @click="playVideo(row)"
                        size="mini"
                        plain
                        v-if="row.video && row.video.src"
                        >浏览视频</el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-wrapper">
            <!-- <div class="multiple-mode-wrapper">
                <el-button v-if="!multipleMode" @click="toggleMultipleMode(true)">批量编辑</el-button>
                <el-button-group v-else>
                    <el-button @click="operation1">批量操作1</el-button>
                    <el-button @click="operation2">批量操作2</el-button>
                    <el-button @click="toggleMultipleMode(false)">取消</el-button>
                </el-button-group>
            </div> -->
            <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
            class="videoDialog"
            destroy-on-close
            center
            append-to-body
            :visible.sync="showViedo"
            @close="closeEvent"
            width="70%"
        >
            <video
                :src="videoUrl.src"
                controlsList="nodownload noremote footbar"
                controls
                style="height: 100%; max-width: 100%"
                oncontextmenu="return false;"
                onmouseleave="leaveVideo(this)"
                ref="video"
                v-if="showViedo"
            >
                您的浏览器不支持 video 标签。
            </video>
        </el-dialog>

        <el-dialog :visible.sync="showProgramme" width="80%" title="未分组节目" center>
            <el-table
                :data="unGrouped"
                row-key="id"
                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="300"
            >
                <el-table-column prop="name" label="节目名称"> </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="phone" label="联系电话" min-width="95"> </el-table-column>
                <el-table-column prop="gradingOrganization" label="考级机构" show-overflow-tooltip min-width="160">
                </el-table-column>
                <el-table-column prop="organization" label="承办单位" min-width="160"> </el-table-column>
                <el-table-column prop="examPoint" label="考级点" min-width="160"> </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="{ row }">
                        <el-button @click="choose(row)" size="mini" type="primary">增加</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>
<script>
import delChild from '@/mixins/delChild';
import { mapState } from 'vuex';
import pageableTable from '@/mixins/pageableTable';
import QrcodeVue from 'qrcode.vue';
import { time } from 'echarts';

export default {
    name: 'ProgrammeList',
    mixins: [pageableTable, delChild],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/programme/backAll',
            downloading: false,
            competitionGroupOptions: [
                { label: '个人', value: 'SINGLE' },
                { label: '集体', value: 'COLLECTIVE' }
            ],
            dialogUrl: '',
            dialogCode: false,
            artTypes: [],
            showViedo: false,
            videoUrl: '',
            arrange: [],
            showProgramme: false,
            unGrouped: [],
            performanceId: ''
        };
    },
    created() {
        if (this.$route.query.aid) {
            this.$http
                .post('/arrange/all', { query: { id: this.$route.query.aid } }, { body: 'json' })
                .then(res => {
                    this.arrange = res.content;
                    this.performanceId = res.content[0].performanceId;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        competitionGroupFormatter(row, column, cellValue, index) {
            let selectedOption = this.competitionGroupOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        beforeGetData() {
            let data = {
                sort: 'gradingOrganizationId,asc;organizationId,asc;',
                query: {
                    arrangeId: Number(this.$route.query.aid)
                }
            };
            return data;
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        showRow(row) {
            this.$router.push({
                path: '/programmeShow',
                query: {
                    id: row.id
                }
            });
        },
        download() {
            this.downloading = true;

            let data = {
                sort: 'gradingOrganizationId,asc;organizationId,asc;',
                query: {
                    arrangeId: Number(this.$route.query.aid)
                }
            };

            this.$axios
                .get('/programme/excel', {
                    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');
        },
        removeRow(row) {
            this.$alert('移除将无法恢复，确认要移除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/programme/removeGroup/${row.id}`);
                })
                .then(() => {
                    this.$message.success('移除成功');
                    this.getData();
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        },
        showCode(row) {
            this.dialogCode = true;
            this.dialogUrl =
                'http://wljtest.izouma.com/h5/home?performanceId=' +
                row.performanceId +
                '&performanceApplyId=' +
                row.id;
        },
        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) {
            this.showViedo = true;
            this.videoUrl = row.video;
            console.log(row.video);
        },
        intoGroup() {
            this.showProgramme = true;
            this.$http
                .post('/programme/unGrouped', { performanceId: this.performanceId })
                .then(res => {
                    this.unGrouped = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        },
        choose(row) {
            this.$http
                .post('/programme/intoGroup', { id: row.id, arrangeId: this.$route.query.aid })
                .then(res => {
                    this.showProgramme = false;
                    this.$message.success('添加成功');
                    this.getData();
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    }
};
</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>
