<template>
    <div class="list-view">
        <div class="filters-container">
            <el-col :span="8">
                <span class="span-size">考级活动名称</span>
                <el-input placeholder="输入考级活动名称" v-model="search" clearable class="filter-item"></el-input>
            </el-col>
            <el-col :span="8">
                <span class="span-size">考级机构名称</span>
                <el-input placeholder="输入考级机构名称" v-model="agency" clearable class="filter-item"></el-input>
            </el-col>
            <el-col :span="8">
                <label class="span-size">考级地点</label>
                <el-select class="filter-item" 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-col>
            <el-col :span="8">
                <span class="span-size">承办单位名称</span>
                <el-input placeholder="输入承办单位名称" v-model="organizer" clearable class="filter-item"></el-input>
            </el-col>

            <el-col :span="14">
                <label class="span-size">考级活动时间</label>
                <el-date-picker
                    v-model="dateRange"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    start-placeholder="请选择开始时间"
                    end-placeholder="请选择结束时间"
                    range-separator="至"
                    class="filter-item"
                >
                </el-date-picker>
            </el-col>
            <el-col :span="12">
                <div style="padding-left: 20px">
                    <el-button @click="getData" type="primary" icon="el-icon-search" class="filter-item"
                        >查询
                    </el-button>
                    <el-button @click="showVideo" type="primary" icon="el-icon-video-camera" class="filter-item"
                        >监控平台
                    </el-button>
                </div>
            </el-col>
        </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"
            @expand-change="expand"
        >
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-card v-if="checkMap.has(props.row.id)">
                        <div v-if="checkMap.get(props.row.id).length === 0">
                            <div style="text-align:center">暂无数据</div>
                        </div>
                        <el-timeline reverse v-else>
                            <el-timeline-item
                                :timestamp="item.createdAt"
                                placement="top"
                                v-for="item in checkMap.get(props.row.id)"
                                :key="item.id"
                            >
                                <div>
                                    {{ item.content }}
                                </div>

                                <div style="margin-top:5px">
                                    <el-image
                                        v-for="(img, index) in item.file"
                                        :key="index"
                                        :src="img"
                                        fit="fill"
                                        :lazy="true"
                                        style="width:60px;height:60px"
                                        :preview-src-list="item.file"
                                    >
                                    </el-image>
                                </div>
                                <!-- content -->
                            </el-timeline-item>
                        </el-timeline>
                        <div style="margin-left: 26px">
                            <el-button @click="saveRow(props.row)" size="mini" plain>编辑</el-button>
                        </div>
                    </el-card>
                </template>
            </el-table-column>
            <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="checkRow(row)" 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>
    </div>
</template>
<script>
import pageableTable from '@/mixins/pageableTable';
import format from 'date-fns/format';
import startOfDay from 'date-fns/startOfDay';
import endOfDay from 'date-fns/endOfDay';
import { th } from 'date-fns/locale';

export default {
    name: 'RecordRegulationList',
    mixins: [pageableTable],
    created() {
        this.$http
            .get('/district/NJ')
            .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,
            search: '',
            url: '/record/all',
            downloading: false,
            categoryOptions: [{ label: '承办单位', value: 'ORGANIZER' }],
            districts: [],
            district: '',
            city: '南京市',
            status: '',
            recordDateRange: '',
            agency: '',
            organizer: '',
            dateRange: '',
            currentTime: '',
            checkMap: new Map()
        };
    },
    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();
            }
        },
        addRow() {
            this.$router.push({
                path: '/recordEdit',
                query: {
                    ...this.$route.query
                }
            });
        },
        editRow(row) {
            this.$router.push({
                path: '/recordEdit',
                query: {
                    rid: row.id
                }
            });
        },
        checkRow(row) {
            this.$refs.table.toggleRowExpansion(row, true);
        },
        saveRow(row) {
            this.$router.push({
                path: '/recordCheck',
                query: {
                    rid: 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);
                    }
                });
        },
        update() {
            this.$http
                .get('/record/update')
                .then(res => {
                    this.getData();
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        },
        openRoom(row) {
            this.$router.push({
                path: '/showRoomList',
                query: {
                    rid: row.id
                }
            });
        },
        showVideo() {
            this.$router.push({
                path: '/videoList'
            });
        },
        expand(row) {
            let checks = new Map([...this.checkMap]);
            if (checks.has(row.id)) {
                return;
            }
            this.$http
                .post(
                    'recordCheck/all',
                    {
                        size: 20,
                        sort: 'createdAt,asc',
                        query: { recordId: row.id }
                    },
                    { body: 'json' }
                )
                .then(res => {
                    if (!res.empty) {
                        checks.set(row.id, res.content);
                    } else {
                        return Promise.reject();
                    }
                })
                .catch(e => {
                    checks.set(row.id, []);
                })
                .then(() => {
                    this.checkMap = new Map(checks);
                });
        }
    }
};
</script>
<style lang="less" scoped>
.span-size {
    font-size: 14px;
    color: #565b66;
    line-height: 15px;
    padding: 0 10px 0 10px;
}
.span-width {
    width: 330px;
    display: inline-block;
    text-align: right;
}
.span-width2 {
    width: 500px;
    // float: left;
    text-align: right;
    display: inline-block;
}
</style>
