<template>
    <div class="list-view">
        <div class="filters-container">
            <el-form :model="form" inline size="small">
                <el-form-item label="考级活动名称">
                    <el-input placeholder="输入考级活动名称" v-model="search" clearable class="filter-item"></el-input>
                </el-form-item>
                <el-form-item label="考级机构名称">
                    <el-input placeholder="输入考级机构名称" v-model="agency" clearable class="filter-item"></el-input>
                </el-form-item>
                <el-form-item label="考级活动状态">
                    <el-select v-model="status" class="filter-item" clearable>
                        <el-option
                            v-for="item in statusOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="考级活动地址">
                    <el-select
                        class="filter-item"
                        v-model="districtId"
                        clearable
                        placeholder="请选择区县"
                        style="width: 200px"
                    >
                        <el-option
                            v-for="item in district"
                            :key="item.id"
                            :value="item.name"
                            :label="item.name"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="考级详细地址">
                    <el-input style="width: 200px" placeholder="输入考级详细地址" class="filter-item"></el-input>
                </el-form-item>
                <el-form-item 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-form-item>
                <el-col :span="24">
                    <el-button @click="getData" type="primary" icon="el-icon-search" class="filter-item"
                        >查询
                    </el-button>
                    <el-button
                        @click="download"
                        type="primary"
                        icon="el-icon-download"
                        :loading="downloading"
                        class="filter-item"
                        :disabled="totalElements <= 0"
                        >导出
                    </el-button>
                </el-col>
            </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="ID" width="100"> </el-table-column> -->
            <el-table-column prop="examinationName" label="考级活动名称" min-width="150"></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"></el-table-column>
            <el-table-column prop="district" label="考级地点" min-width="65"></el-table-column>
            <el-table-column prop="examinationAddress" label="考级详细地址" min-width="150"></el-table-column>
            <el-table-column label="考级状态" min-width="65">
                <template slot-scope="{ row }">
                    <el-link style="font-size: 13px" :underline="false" v-if="currentTime < row.examinationStartTime"
                        >未开考
                    </el-link>
                    <el-link style="font-size: 13px" :underline="false" v-else-if="currentTime > row.examinationEndTime"
                        >已开考
                    </el-link>
                    <el-link
                        style="font-size: 13px"
                        :underline="false"
                        v-else-if="currentTime >= row.examinationStartTime && currentTime <= row.examinationEndTime"
                        >考级中
                    </el-link>
                </template>
            </el-table-column>
            <!-- <el-table-column prop="recordTime" label="备案时间" min-width="90"></el-table-column> -->
            <el-table-column label="操作" align="center" fixed="right" min-width="180">
                <template slot-scope="{ row }">
                    <el-button @click="editRow(row)" size="mini" plain>查看</el-button>
                    <el-button
                        @click="showVideo(row)"
                        size="mini"
                        type="warning"
                        plain
                        v-if="currentTime < row.examinationStartTime"
                        >设置视频</el-button
                    >
                    <el-button
                        @click="showOnlineVideo(row)"
                        size="mini"
                        plain
                        type="success"
                        v-if="currentTime >= row.examinationStartTime && currentTime <= row.examinationEndTime"
                        >线上监控</el-button
                    >
                    <el-button
                        @click="showHistoryVideo(row)"
                        size="mini"
                        plain
                        v-if="currentTime > row.examinationEndTime"
                        type="primary"
                        >上传视频</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 { mapState } from 'vuex';
import { format } from 'date-fns';

export default {
    name: 'RecordOrganizationList',
    mixins: [pageableTable],
    created() {
        this.$http
            .get('/district/NJ')
            .then(res => {
                this.district = 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/byOrganization',
            downloading: false,
            district: [],
            districtId: '',
            recordDateRange: '',
            agency: '',
            organizer: '',
            dateRange: '',
            dialogVisible: false,
            currentTime: '',
            statusOptions: [
                { label: '未开考', value: 1 },
                { label: '已开考', value: 2 },
                { label: '考级中', value: 3 }
            ],
            status: '',
            form: {}
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        },
        ...mapState(['organization'])
    },
    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: 'recordTime,desc', query: { uscc: this.organization.uscc } };
            if (this.status) {
                data.query.status = this.status;
            }
            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
                }
            });
        },
        showVideo(row) {
            this.$router.push({
                path: '/examRoomList',
                query: {
                    rid: row.id
                }
            });
        },
        showOnlineVideo(row) {
            this.$router.push({
                path: '/showRoomList',
                query: {
                    rid: row.id
                }
            });
        },
        showHistoryVideo(row) {
            this.$router.push({
                path: '/historyRoomList',
                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);
                    }
                });
        },
        distribute() {
            this.$alert('已分发到各区县！', '分发', {
                confirmButtonText: '确定'
            });
        },
        update() {
            this.$http
                .get('/record/update')
                .then(res => {
                    this.getData();
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    }
};
</script>
<style lang="less" scoped>
/deep/.el-form-item--mini.el-form-item,
.el-form-item--small.el-form-item {
    margin-bottom: 3px;
}
</style>
