<template>
    <div class="list-view">
        <div class="filters-container">
            <el-input placeholder="名称" v-model="search" clearable class="filter-item"></el-input>
            <el-select v-model="reviewStatus" clearable placeholder="评审状态" class="filter-item">
                <el-option
                    v-for="item in reviewStatusOptions"
                    :label="item.label"
                    :value="item.value"
                    :key="item.value"
                ></el-option>
            </el-select>
            <el-select v-model="passStatus" clearable placeholder="审核状态" class="filter-item">
                <el-option
                    v-for="item in passStatusOptions"
                    :label="item.label"
                    :value="item.value"
                    :key="item.value"
                ></el-option>
            </el-select>
            <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"
                >导出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"
            v-loading="fetchingData"
        >
            <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="name" label="项目名称"> </el-table-column>
            <el-table-column prop="pics" label="图片">
                <template slot-scope="{ row }">
                    <el-image
                        v-if="row.pics && row.pics[0]"
                        style="width: 30px; height: 30px"
                        :src="row.pics[0]"
                        fit="cover"
                        :preview-src-list="row.pics"
                    ></el-image>
                </template>
            </el-table-column>
            <el-table-column prop="intro" label="项目简介"> </el-table-column>
            <el-table-column prop="attachment" label="附件"> </el-table-column>
            <el-table-column prop="reviewStatus" label="评审状态"> </el-table-column>
            <el-table-column prop="passStatus" label="审核状态"> </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="150">
                <template slot-scope="{ row }">
                    <el-button @click="editRow(row)" type="primary" size="mini" plain>查看</el-button>
                    <el-button @click="review(row)" type="primary" size="mini" v-if="row.reviewStatus === '待评审'">
                        评审
                    </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 :visible.sync="showDialog" width="60vw" top="5vh" title="项目详情" class="review-dialog">
            <div class="title">项目名称</div>
            <el-divider></el-divider>
            <div class="name">{{ selected.name }}</div>
            <div class="title">项目简介</div>
            <el-divider></el-divider>
            <div class="intro" v-html="selected.intro"></div>
            <div slot="footer">
                <el-button @click="showDialog = false">关闭</el-button>
                <el-button
                    @click="review(selected)"
                    type="primary"
                    v-if="selected.reviewStatus === '待评审' || selected.passStatus === '驳回'"
                >
                    评审
                </el-button>
            </div>
        </el-dialog>

        <el-dialog :visible.sync="showReviewDialog" title="评审">
            <el-input type="textarea" :rows="10" v-model="reviewContent" placeholder="请输入评审意见"></el-input>
            <div slot="footer">
                <el-button @click="showReviewDialog = false" :disabled="reviewSaving">取消</el-button>
                <el-button @click="onSaveReview" :loading="reviewSaving" type="primary">保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { mapState } from 'vuex';
import pageableTable from '@/mixins/pageableTable';

export default {
    name: 'ProjectManagementList',
    mixins: [pageableTable],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/projectManagement/all',
            downloading: false,
            statusOptions: [
                { label: '待评审', value: '待评审' },
                { label: '已评审', value: '已评审' }
            ],
            reviewStatusOptions: [
                { label: '待评审', value: '待评审' },
                { label: '已评审', value: '已评审' }
            ],
            passStatusOptions: [
                { label: '待审核', value: '待审核' },
                { label: '通过', value: '通过' },
                { label: '驳回', value: '驳回' }
            ],
            reviewStatus: null,
            passStatus: null,
            showDialog: false,
            selected: {},
            showReviewDialog: false,
            reviewContent: '',
            reviewSaving: false
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        statusFormatter(row, column, cellValue, index) {
            let selectedOption = this.statusOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        beforeGetData() {
            return {
                search: this.searchj,
                query: {
                    reviewStatus: this.reviewStatus,
                    passStatus: this.passStatus
                }
            };
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        addRow() {
            this.$router.push({
                path: '/projectManagementEdit',
                query: {
                    ...this.$route.query
                }
            });
        },
        editRow(row) {
            this.selected = row;
            this.showDialog = true;
        },
        download() {
            this.downloading = true;
            this.$axios
                .get('/projectManagement/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',
                        decodeURIComponent(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(`/projectManagement/del/${row.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.getData();
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        },
        review(row) {
            this.selected = row;
            this.reviewContent = '';
            this.showReviewDialog = true;
        },
        onSaveReview() {
            if (!this.reviewContent) {
                this.$message.error('请输入评审意见');
                return;
            }
            this.reviewSaving = true;
            this.$http
                .post('/projectManagement/review', { id: this.selected.id, review: this.reviewContent })
                .then(res => {
                    this.reviewSaving = false;
                    this.showReviewDialog = false;
                    this.$message.success('保存成功');
                    this.getData();
                })
                .catch(e => {
                    this.reviewSaving = false;
                    this.$message.error(e.error || '保存失败');
                });
        }
    }
};
</script>
<style lang="less" scoped>
/deep/ .review-dialog {
    .el-dialog__body {
        height: calc(90vh - 185px);
        overflow: auto;
    }
    .el-divider {
        margin: 15px 0 !important;
    }
    .intro {
        img {
            max-width: 100%;
        }
    }
    .title {
        font-size: 14px;
        font-weight: bold;
    }
    .name {
        font-size: 18px;
        margin-bottom: 40px;
    }
}
</style>
