<template>
    <div class="list-view">
        <div class="filters-container">
            <el-form :model="form" inline size="mini" label-width="100px">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="展演活动名称">
                            <el-select
                                v-model="performanceId"
                                filterable
                                placeholder="展演活动名称"
                                style="width: 100%"
                            >
                                <el-option
                                    v-for="item in performances"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="参赛专业">
                            <el-cascader
                                ref="artCascader"
                                style="width: 100%"
                                v-model="form.specialtyId"
                                :props="optionProps"
                                :options="artTypes"
                                :show-all-levels="false"
                                placeholder="请选择专业"
                                clearable
                            >
                            </el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="奖项">
                            <el-select v-model="form.awardId" clearable>
                                <el-option
                                    v-for="item in awards"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-collapse-transition>
                    <div v-show="showMore">
                        <el-row>
                            <el-col :span="8">
                                <el-form-item label="考级机构名称">
                                    <el-select
                                        v-model="form.gradingOrganizationId"
                                        clearable
                                        filterable
                                        placeholder="考级机构"
                                        style="width: 100%"
                                    >
                                        <el-option
                                            v-for="item in gradingOrganizationIdOptions"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        >
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="承办单位名称">
                                    <el-select
                                        v-model="form.organizationId"
                                        clearable
                                        filterable
                                        placeholder="承办单位"
                                        style="width: 100%"
                                    >
                                        <el-option
                                            v-for="item in organizationIdOptions"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        >
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                            <el-col :span="8">
                                <el-form-item label="节目名称">
                                    <el-input placeholder="节目名称" v-model="form.name" clearable></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="8">
                                <el-form-item label="节目编号">
                                    <el-input placeholder="节目编号" v-model="form.id" clearable></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                </el-collapse-transition>
                <div style="width:100%;textAlign:center;margin-bottom:10px;">
                    <el-button
                        class="more"
                        round
                        type="primary"
                        :plain="!showMore"
                        :icon="showMore ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                        @click="showMore = !showMore"
                        size="mini"
                        >{{ showMore ? '隐藏查询区域' : '显示查询区域' }}</el-button
                    >
                </div>
                <el-form-item>
                    <el-button @click="getData" type="primary" icon="el-icon-search">查询 </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"
            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="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="参赛组别" min-width="70"> </el-table-column>
            <!-- <el-table-column prop="level" label="参赛级别" min-width="70"> </el-table-column> -->
            <el-table-column prop="quantity" label="参赛人数" min-width="70"> </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 prop="score" label="分数" min-width="70" v-if="display"> </el-table-column>
            <el-table-column prop="award" label="获奖情况" min-width="120">
                <template slot-scope="{ row }">
                    <span v-if="!row.second">{{ row.award }}</span>
                    <el-select v-else v-model="row.awardId" size="mini" :controls="true" style="width: 90px" clearable>
                        <el-option
                            v-for="item in awards"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        ></el-option>
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="left" fixed="right" :min-width="display ? 235 : 100">
                <template slot-scope="{ row, $index }">
                    <el-button
                        @click="playVideo(row, $index)"
                        size="mini"
                        plain
                        type="primary"
                        style="margin-right:10px"
                        >查看作品</el-button
                    >
                    <span v-if="performance.status == 'RATE' && display">
                        <el-button @click="row.second = true" size="mini" plain type="warning" v-if="!row.second"
                            >修改</el-button
                        >
                        <span v-else>
                            <el-button @click="saveRow(row)" size="mini" plain type="success">保存</el-button>
                            <el-button @click="row.second = false" size="mini" plain type="danger">取消</el-button>
                        </span>
                    </span>
                </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="45vw"
        >
            <div class="dialog-content">
                <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>
            <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 || index >= pageSize - 1"
                    >下一个</el-button
                >
                <el-button @click="showViedo = false" size="mini">关闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import delChild from '@/mixins/delChild';
import { mapState } from 'vuex';
import pageableTable from '@/mixins/pageableTable';

export default {
    name: 'ProgrammeOrgList',
    mixins: [pageableTable, delChild],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/programme/result',
            downloading: false,
            competitionGroupOptions: [
                { label: '个人', value: 'SINGLE' },
                { label: '集体', value: 'COLLECTIVE' }
            ],
            form: {},
            competitionGroup: '',
            levelSettingId: '',
            gradingOrganizationIdOptions: [],
            gradingOrganizationId: '',
            performanceId: '',
            performances: [],
            performance: {},
            artTypes: [],
            optionProps: {
                value: 'id',
                label: 'name',
                children: 'children',
                multiple: false,
                emitPath: false,
                checkStrictly: true,
                expandTrigger: 'hover'
            },
            specialtyId: '',
            showMore: false,
            organizationIdOptions: [],
            awards: [],
            showViedo: false,
            programme: {},
            index: 0,
            display: false
        };
    },
    created() {
        this.getAdmin();
        if (this.$route.query.pid) {
            this.performanceId = Number(this.$route.query.pid);
        }
        this.$http
            .post('/gradingOrganization/all', { size: 1000, query: { del: false } }, { body: 'json' })
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.gradingOrganizationIdOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post('/organization/all', { size: 1000, query: { del: false } }, { body: 'json' })
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.organizationIdOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post(
                '/performance/all',
                {
                    size: 1000,
                    sort: 'year,desc',
                    query: {
                        publish: true
                    }
                },
                { body: 'json' }
            )
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.performances.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                    if (!this.$route.query.pid) {
                        this.performanceId = res.content[0].id;
                        this.performance = res.content[0];
                    }
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post('/artType/allList')
            .then(res => {
                this.artTypes = this.delChild(res);
            })
            .catch(e => {
                console.log(e);
            });
        this.$http
            .post('/award/all', { size: 20, query: { performance: this.performanceId } }, { body: 'json' })
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.awards.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
            });
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        },
        ...mapState(['userInfo'])
    },
    methods: {
        competitionGroupFormatter(row, column, cellValue, index) {
            let selectedOption = this.competitionGroupOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        getBefore() {
            let data = {
                sort: 'awardId,asc;score,desc',
                query: {
                    programmeStatus: 'SUBMIT'
                }
            };
            if (this.form.gradingOrganizationId) {
                data.query.gradingOrganizationId = this.form.gradingOrganizationId;
            }
            if (this.form.organizationId) {
                data.query.organizationId = this.form.organizationId;
            }
            if (this.form.specialtyId) {
                data.query.code = this.getCode(this.form.specialtyId);
            }
            if (this.performanceId) {
                data.query.performanceId = this.performanceId;
            }
            if (this.form.name) {
                data.query.name = this.form.name;
            }
            if (this.form.id) {
                data.query.id = this.form.id;
            }
            if (this.form.awardId) {
                data.query.awardId = this.form.awardId;
            }
            return data;
        },
        beforeGetData() {
            return this.getBefore();
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        addRow() {
            this.$router.push({
                path: '/programmeEdit',
                query: {
                    ...this.$route.query
                }
            });
        },
        download() {
            this.downloading = true;

            let data = this.getBefore();
            data.size = 2000;
            this.$axios
                .get('/programme/excelResult', {
                    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);
                });
        },
        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;
        },
        saveRow(row) {
            this.$http
                .post('/programme/saveAward', {
                    id: row.id,
                    awardId: row.awardId
                })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.getData();
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        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;
        },
        getAdmin() {
            let data = this.userInfo.authorities;
            data.forEach(element => {
                if (element.name === 'ROLE_ADMIN') {
                    this.display = true;
                }
            });
        }
    }
};
</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;
        }
    }
}
.uploader {
    display: inline-block;
    margin: 0 10px;
}
</style>
