<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="download"
                type="primary"
                icon="el-icon-download"
                :loading="downloading"
                class="filter-item"
                >导出EXCEL
            </el-button>
            <el-button
                @click="importData"
                icon="el-icon-upload2"
                :loading="downloading"
                :disabled="fetchingData"
                class="filter-item"
            >
                导入
            </el-button>
            <el-button
                @click="template"
                icon="el-icon-notebook-2"
                :loading="downloading"
                :disabled="fetchingData"
                class="filter-item"
            >
                模版
            </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="orgName" label="单位名称" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="orgType" label="单位性质" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="publicCompany" label="上市公司" width="70">
                <template v-slot="{ row }">{{ row.publicCompany ? '是' : '否' }}</template>
            </el-table-column>
            <el-table-column prop="parentOrg" label="隶属单位" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="addressDetail" label="详细地址" min-width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="location" label="地理位置" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="createdAt" label="提交日期" width="150"> </el-table-column>
            <el-table-column prop="intro" label="企业介绍" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="license" label="营业执照" width="80" align="center">
                <template v-slot="{ row }">
                    <el-image
                        style="width: 50px; height: 30px"
                        fit="contain"
                        :src="row.license"
                        :preview-src-list="[row.license]"
                    >
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column prop="contactName" label="联系人" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="contactIdNo" label="身份证号" min-width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="contactPhone" label="手机号" min-width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="contactEmail" label="电子邮箱" min-width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="attach" label="信息报送承诺书" min-width="110" show-overflow-tooltip>
                <template v-slot="{ row }">
                    <el-link type="primary" v-if="row.attach" :href="row.attach" target="_blank">下载</el-link>
                </template>
            </el-table-column>
            <el-table-column prop="nickname" label="用户名" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="status" label="状态" :formatter="statusFormatter" width="80"> </el-table-column>
            <el-table-column prop="auditTime" label="审核时间" width="150"></el-table-column>
            <el-table-column label="操作" align="center" fixed="right" min-width="150">
                <template slot-scope="{ row }">
                    <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';

export default {
    name: 'OrgInfoList',
    mixins: [pageableTable],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/orgInfo/all',
            downloading: false,
            sortStr: 'createdAt,desc',
            statusOptions: [
                { label: '未审核', value: 'PENDING' },
                { label: '审核通过', value: 'PASS' },
                { label: '驳回', value: 'DENY' }
            ]
        };
    },
    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() {
            if (this.search) {
                return { search: this.search };
            }
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        addRow() {
            this.$router.push({
                path: '/orgInfoEdit',
                query: {
                    ...this.$route.query
                }
            });
        },
        editRow(row) {
            this.$router.push({
                path: '/orgInfoEdit',
                query: {
                    id: row.id
                }
            });
        },
        download() {
            this.downloading = true;
            this.$axios
                .get('/orgInfo/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(`/orgInfo/del/${row.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.getData();
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        },
        pass(row) {},
        deny(row) {
            this.$prompt('请输入拒绝原因', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /.+/,
                inputErrorMessage: '请输入拒绝原因',
                beforeClose: (action, instance, done) => {
                    console.log(action, instance, done);
                    if (action === 'confirm') {
                        // instance.confirmButtonLoading = true;
                        // instance.confirmButtonText = '执行中...';
                        this.$http
                            .post('/orgInfo/check', {
                                id: row.id,
                                status: 'DENY',
                                comment: instance.inputValue
                            })
                            .then(res => {
                                done();
                                this.getData();
                            })
                            .catch(e => {
                                done();
                                this.$message.error(e.error || '操作失败');
                                console.log(e);
                            });
                    } else {
                        done();
                    }
                }
            })
                .then(({ value }) => {})
                .catch(() => {});
        }
    }
};
</script>
<style lang="less" scoped></style>
