<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="canApply"
                >创建申请
            </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="createdAt" label="申请时间"></el-table-column>
            <el-table-column prop="year" label="年度"> </el-table-column>
            <el-table-column prop="status" label="状态" :formatter="statusFormatter1"> </el-table-column>
            <el-table-column prop="submit" label="提交">
                <template slot-scope="{ row }">
                    <span v-if="row.submit">已提交</span>
                    <span v-else>未提交</span>
                </template>
            </el-table-column>
            <el-table-column prop="score" label="分数">
                <template slot-scope="{ row }">
                    <span v-if="row.score">{{ row.score }}</span>
                    <span v-else>暂无</span>
                </template>
            </el-table-column>
            <el-table-column prop="grade" label="等级">
                <template slot-scope="{ row }">
                    <span v-if="row.grade == 'EXCELLENT'"><el-tag type="success">优秀</el-tag></span>
                    <span v-else-if="row.grade == 'ELIGIBLE'"><el-tag type="warning">合格</el-tag></span>
                    <span v-else-if="row.grade == 'NOT_ELIGIBLE'"><el-tag type="danger">不合格</el-tag></span>
                    <span v-else><el-tag type="info">暂无</el-tag></span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" min-width="150">
                <template slot-scope="{ row }">
                    <el-button @click="editRow(row)" type="primary" size="mini" plain>
                        <span v-if="row.status == 'FIRST_REVIEW_DENY' || !row.submit">编辑资料</span>
                        <span v-else>查看材料</span></el-button
                    >
                    <el-button
                        @click="onCancel(row)"
                        :loading="downloading"
                        type="danger"
                        size="mini"
                        plain
                        v-if="row.status == 'FIRST_REVIEW_PENDING' || row.status == 'FIRST_REVIEW_DENY'"
                        >撤回
                    </el-button>
                    <el-button
                        @click="word(row)"
                        type="primary"
                        :loading="downloading"
                        size="mini"
                        class="filter-item"
                        v-if="row.status == 'SUBMIT_PAPER_MATERIALS'"
                        >下载材料
                    </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 { mapState } from 'vuex';
import pageableTable from '@/mixins/pageableTable';
import rateStatus from '@/mixins/rateStatus';

export default {
    name: 'RateList',
    mixins: [pageableTable, rateStatus],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/rate/all',
            downloading: false,
            canApply: false
        };
    },
    created() {
        this.$http
            .get('/rate/canApply', { id: this.organization.id })
            .then(res => {
                this.canApply = res;
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        },
        ...mapState(['organization'])
    },
    methods: {
        beforeGetData() {
            return {
                search: this.search,
                sort: 'createdAt,desc',
                query: {
                    organizationId: this.organization.id
                }
            };
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        addRow() {
            this.$router.push({
                path: '/rateEdit',
                query: {
                    ...this.$route.query
                }
            });
        },
        editRow(row) {
            this.$router.push({
                path: '/organization1',
                query: {
                    id: row.id
                }
            });
        },
        download() {
            this.downloading = true;
            this.$axios
                .get('/rate/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(`/rate/del/${row.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.getData();
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        },
        word(row) {
            this.downloading = true;
            this.$axios
                .get('/rate/exportPdf/' + row.id, { responseType: 'blob' })
                .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', '申请材料.pdf');
                    document.body.appendChild(link);
                    link.click();
                    link.remove();
                })
                .catch(e => {
                    console.log(e);
                    this.downloading = false;
                    this.$message.error(e.error);
                });
            // this.$axios
            //     .get('/rate/export/' + row.id)
            //     .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', '申请材料.doc');
            //         document.body.appendChild(link);
            //         link.click();
            //         link.remove();
            //     })
            //     .catch(e => {
            //         console.log(e);
            //         this.downloading = false;
            //         this.$message.error(e.error);
            //     });
        },
        onCancel(row) {
            let data = { ...row };
            data.status = 'DRAFT';
            data.submit = false;

            this.$alert('确认要撤回么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post('/rate/save', data, { body: 'json' });
                })
                .then(() => {
                    this.$message.success('撤回成功');
                    this.getData();
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error((e || {}).error || '撤回失败');
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped></style>
