<template>
    <div class="list-view">
        <div class="filters-container">
            <!-- <el-input placeholder="输入关键字" v-model="search" clearable class="filter-item"></el-input>
            <el-button @click="getData" type="primary" icon="el-icon-search" class="filter-item">搜索 </el-button> -->
            <el-button @click="addRow" type="primary" icon="el-icon-plus" class="filter-item" v-if="canSign"
                >添加报名
            </el-button>
            <!-- <el-button
                @click="download"
                type="primary"
                icon="el-icon-download"
                :loading="downloading"
                class="filter-item"
                >导出EXCEL
            </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="composition" label="作品名称"> </el-table-column>
            <el-table-column prop="artType" label="专业"> </el-table-column>
            <el-table-column prop="teacher" label="指导老师"> </el-table-column>
            <el-table-column prop="organizer" label="选送机构"> </el-table-column>
            <el-table-column prop="contact" label="联系人"> </el-table-column>
            <el-table-column prop="phone" label="联系电话"> </el-table-column>
            <el-table-column prop="status" label="状态" :formatter="statusFormatter"> </el-table-column>
            <el-table-column prop="showTime" label="表演时间" min-width="140" show-overflow-tooltip
                ><template slot-scope="{ row }">
                    <span v-if="row.showStartTime">{{ row.showStartTime }} 至 {{ row.showEndTime }}</span>
                    <span v-else>暂无</span>
                </template></el-table-column
            >
            <el-table-column label="操作" align="left" fixed="right" min-width="220">
                <template slot-scope="{ row }">
                    <!-- <el-button @click="deleteRow(row)" type="danger" size="mini" plain>删除</el-button> -->
                    <el-button @click="showDialog(row)" type="primary" size="mini" plain>参演人员维护</el-button>
                    <el-button @click="editRow(row)" type="primary" size="mini" plain v-if="row.status != 'PASS'">
                        编辑
                    </el-button>
                    <el-button @click="showCode(row)" type="primary" size="mini" plain v-if="row.status == 'PASS'">
                        二维码
                    </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 title="参演人员" :visible.sync="dialogVisible" width="500px" center>
            <div>
                <!-- <span class="span-size">姓名</span> <span class="span-size">手机号</span>
                <div v-for="item in person" :key="item.id" style="padding: 10px 10px 10px 100px">
                    <span class="span-size">{{ item.name }}</span> <span class="span-size">{{ item.phone }}</span>
                </div> -->
                <el-table :data="person">
                    <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                    <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
                    <el-table-column label="操作" align="center"
                        ><template slot-scope="{ row }">
                            <el-link style="font-size: 13px;" @click="deletePerson(row)" type="danger" size="mini" plain
                                >删除</el-link
                            >
                        </template></el-table-column
                    >
                </el-table>
                <div style="margin: 10px 170px 0">
                    <el-button class="dia-mar" type="primary" @click="add = true" v-if="!add" size="mini"
                        >添加人员</el-button
                    >
                </div>
                <div v-if="add">
                    <el-form :model="form" ref="form" size="small">
                        <el-input
                            class="dia-mar"
                            v-model="form.name"
                            placeholder="请输入姓名"
                            style="width: 130px"
                        ></el-input>
                        <el-input
                            class="dia-mar"
                            v-model="form.phone"
                            placeholder="请输入手机号"
                            style="width: 160px"
                        ></el-input>
                        <el-button class="dia-mar" size="mini" @click="onSave">确定</el-button>
                        <el-button class="dia-mar" size="mini" @click="add = false">取消</el-button>
                    </el-form>
                </div>
            </div>
        </el-dialog>
        <el-dialog title="二维码" :visible.sync="dialogCode" width="400px" center>
            <div style="margin-left: 70px;">
                <qrcode-vue :value="dialogUrl" :size="200" level="H" />
            </div>
        </el-dialog>
        <el-dialog title="自动编排" :visible.sync="dialogArrangement" width="400px" center>
            <div style="margin-left: 70px;">
                <img style="width: 200px; heght: 200px;" src="@/assets/arrangement.png" />
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { mapState } from 'vuex';
import pageableTable from '@/mixins/pageableTable';
import QrcodeVue from 'qrcode.vue';

export default {
    name: 'PerformanceApplyList',
    mixins: [pageableTable],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/performanceApply/all',
            downloading: false,
            statusOptions: [
                { label: '审核中', value: 'PENDING' },
                { label: '通过', value: 'PASS' },
                { label: '拒绝', value: 'DENY' }
            ],
            dialogVisible: false,
            person: [],
            form: {
                performanceApplyId: '',
                name: '',
                value: ''
            },
            add: false,
            dialogCode: false,
            canSign: false,
            applyId: '',
            dialogArrangement: false,
            dialogUrl: ''
        };
    },
    created() {
        if (this.$route.query.perforStatus === 'APPLY') {
            this.canSign = true;
        }
    },
    components: {
        QrcodeVue
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        showCode(row) {
            this.dialogCode = true;
            this.dialogUrl =
                'http://wljtest.izouma.com/h5/home?performanceId=' +
                row.performanceId +
                '&performanceApplyId=' +
                row.id;
        },
        statusFormatter(row, column, cellValue, index) {
            let selectedOption = this.statusOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        beforeGetData() {
            return {
                search: this.search,
                query: {
                    performanceId: Number(this.$route.query.perforId)
                }
            };
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        addRow() {
            this.$router.push({
                path: '/performanceApplyEdit',
                query: {
                    ...this.$route.query
                }
            });
        },
        editRow(row) {
            this.$router.push({
                path: '/performanceApplyEdit',
                query: {
                    id: row.id
                }
            });
        },
        download() {
            this.downloading = true;
            this.$axios
                .get('/performanceApply/excel', {
                    responseType: 'blob',
                    params: { size: 10000 }
                })
                .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');
        },
        deleteRow(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/performanceApply/del/${row.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.getData();
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        },
        showDialog(row) {
            this.dialogVisible = true;
            this.applyId = row.id;
            this.form.performanceApplyId = row.id;
            this.getPerson(row.id);
        },
        getPerson(id) {
            this.$http
                .post('/person/all', { size: 100, query: { del: false, performanceApplyId: id } }, { body: 'json' })
                .then(res => {
                    this.person = res.content;
                    this.form.performanceApplyId = id;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        },
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    let data = { ...this.form };
                    this.saving = true;
                    this.$http
                        .post('/person/save', data, { body: 'json' })
                        .then(res => {
                            this.saving = false;
                            this.$message.success('成功');
                            this.form = {
                                name: '',
                                phone: ''
                            };
                            this.add = false;
                            this.getPerson(this.applyId);
                            // this.$router.go(-1);
                        })
                        .catch(e => {
                            console.log(e);
                            this.saving = false;
                            this.$message.error(e.error);
                        });
                } else {
                    return false;
                }
            });
        },
        deletePerson(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/person/del/${row.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.getPerson(this.applyId);
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped>
.dia-mar {
    margin: 8px 6px;
}
.span-size {
    margin: 20px;
    font-size: 15px;
}
.right {
    margin-right: 60px;
}
</style>
