<template>
    <div class="list-view">
        <div class="filters-container">
            <el-form size="mini" inline>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="考级活动名称">
                            <el-input placeholder="输入考级活动名称" v-model="search" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="考级机构名称"
                            ><el-input placeholder="输入考级机构名称" v-model="agency" clearable></el-input
                        ></el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="考级地点">
                            <el-select v-model="district" clearable placeholder="请选择区县">
                                <el-option
                                    v-for="item in districts"
                                    :key="item.id"
                                    :value="item.name"
                                    :label="item.name"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="承办单位名称">
                            <el-input placeholder="输入承办单位名称" v-model="organizer" clearable></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="14">
                        <el-form-item label="考级活动时间">
                            <el-date-picker
                                v-model="dateRange"
                                type="daterange"
                                value-format="yyyy-MM-dd"
                                start-placeholder="请选择开始时间"
                                end-placeholder="请选择结束时间"
                                range-separator="至"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item>
                            <el-button @click="getData" type="primary" icon="el-icon-search">查询 </el-button>
                            <el-button @click="showVideo" type="primary" icon="el-icon-video-camera"
                                >监控平台
                            </el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </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="examinationName" label="考级活动名称" min-width="150" sortable> </el-table-column>
            <el-table-column prop="examinationStartTime" label="考级活动时间" show-overflow-tooltip min-width="170">
                <template slot-scope="{ row }">
                    <span>{{ row.examinationStartTime }} 至 {{ row.examinationEndTime }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="examinationAgency" label="考级机构名称" min-width="150" sortable> </el-table-column>
            <el-table-column prop="organizer" label="承办单位名称" min-width="160" sortable> </el-table-column>
            <el-table-column prop="district" label="考级地点" min-width="70"> </el-table-column>
            <el-table-column prop="recordTime" label="备案时间" min-width="90"> </el-table-column>
            <el-table-column label="操作" align="right" fixed="right" min-width="260">
                <template slot-scope="{ row }">
                    <el-button @click="editRow(row)" size="mini" plain>查看</el-button>
                    <el-button @click="openDrawer(row.id)" type="warning" size="mini" plain>检查记录</el-button>
                    <el-button @click="openRoom(row)" type="primary" size="mini" plain>考场监控</el-button>
                    <!-- <el-button @click="deleteRow(row)" type="danger" size="mini" plain>删除</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>
        <record-log ref="dataRefs" :dialogVisible="isShow" @close="isShow = false"></record-log>
        <record-logs ref="dataRef" :dialogVisibles="isShows" @close="isShows = false"></record-logs>
        <regulation-drawer ref="drawerRefs" :drawerVisible="drawer"></regulation-drawer>
    </div>
</template>
<script>
import pageableTable from '@/mixins/pageableTable';
import RecordLog from '../../components/RecordLog';
import RecordLogs from '../../components/RecordLogs';
import format from 'date-fns/format';
import startOfDay from 'date-fns/startOfDay';
import endOfDay from 'date-fns/endOfDay';
import { th } from 'date-fns/locale';
import RegulationDrawer from '../../components/RegulationDrawer.vue';

export default {
    name: 'RecordRegulationList',
    mixins: [pageableTable],
    components: { RecordLog, RecordLogs, RegulationDrawer },
    created() {
        this.$http
            .post('/setting/byFlag', { flag: 2 })
            .then(res => {
                this.districts = res;
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.currentTime = format(new Date(), 'yyyy-MM-dd');
    },
    data() {
        return {
            multipleMode: false,
            isShow: false,
            isShows: false,
            search: '',
            url: '/record/all',
            downloading: false,
            categoryOptions: [{ label: '承办单位', value: 'ORGANIZER' }],
            districts: [],
            district: '',
            status: '',
            agency: '',
            organizer: '',
            dateRange: '',
            currentTime: '',
            tipsType: {
                examOrganization: '是否履行考前备案手续（此项为否，不再检查下面内容）',
                examSite: '是否在考场明显位置张贴《考级简章》',
                examRoom: '考场服务设施是否完善（候考室、指示标记、安全设施等）',
                environment: '考级时间与网上平台考前备案的考级时间是否一致',
                safetyq: '考场内有无相关专业考官且佩戴考官证',
                safetyw: '是否现场对考生艺术水平作出评定（美术专业除外）',
                safetye: '考级内容是否是所属考级机构教材确定的考级内容',
                safetyr: '是否在考场明显位置张贴《恢复开展社会艺术水平考级现场考级活动疫情防控措施指南》海报',
                safetyt: '考点是否配备测量体温设施设备，并有专人值守',
                safetys: '考级现场是否实施预约限流措施'
            },
            drawer: false
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        categoryFormatter(row, column, cellValue, index) {
            let selectedOption = this.categoryOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        beforeGetData() {
            let data = {
                sort: 'examinationEndTime,desc',
                query: {
                    del: false,
                    examinationTime: format(new Date(), 'yyyy-MM-dd')
                }
            };
            if (this.search) {
                data.search = this.search;
            }
            if (this.district) {
                data.query.district = this.district;
            }
            if (this.agency) {
                data.query.examinationAgency = this.agency;
            }
            if (this.organizer) {
                data.query.organizer = this.organizer;
            }
            return data;
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        editRow(row) {
            this.isShows = true;
            this.$refs.dataRef.init(row.id);
        },
        download() {
            this.downloading = true;
            this.$axios
                .get('/record/excel', {
                    responseType: 'blob',
                    params: {
                        size: 10000,
                        query: {
                            del: false
                        }
                    }
                })
                .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(`/record/del/${row.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.getData();
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        },
        openRoom(row) {
            this.$router.push({
                path: '/showRoomList',
                query: {
                    rid: row.id
                }
            });
        },
        showVideo() {
            this.$router.push({
                path: '/videoList'
            });
        },
        openDrawer(row) {
            this.drawer = true;
            this.$refs.drawerRefs.init(row);
        }
    }
};
</script>
<style lang="less" scoped>
/deep/.el-form-item--mini.el-form-item,
.el-form-item--small.el-form-item {
    margin-bottom: 10px;
}
</style>
