<template>
    <div>
        <div class="filters-container">

            <el-input placeholder="关键字" size="small" v-model="filter1" clearable class="filter-item"></el-input>

            <el-button @click="searchData" type="primary" size="small" icon="el-icon-search" class="filter-item">搜索
            </el-button>
            <!-- <el-button @click="$router.push('/feedback')" type="primary" size="small" icon="el-icon-edit" class="filter-item">添加
            </el-button> -->
            <el-dropdown trigger="click" size="medium" class="table-column-filter">
                <span>
                    筛选数据
                    <i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown" class="table-column-filter-wrapper">
                    <el-checkbox v-for="item in tableColumns" :key="item.value" v-model="item.show">{{item.label}}
                    </el-checkbox>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
        <el-table :data="tableData" :height="tableHeight" row-key="id" ref="table">
            <el-table-column v-if="multipleMode" align="center" type="selection" width="50">
            </el-table-column>
            <el-table-column type="index" min-width="50" align="center">
            </el-table-column>
            <el-table-column v-if="isColumnShow('userId')" prop="userId" label="用户ID" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('userName')" prop="userName" label="用户名" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('remark')" prop="remark" label="描述" min-width="100">
            </el-table-column>
            <!-- <el-table-column v-if="isColumnShow('image')" prop="image" label="图片" min-width="100">

            </el-table-column> -->
            <el-table-column v-if="isColumnShow('telephone')" prop="telephone" label="联系电话" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('createTime')" prop="createTime" :formatter="DateTimeFormatter" label="创建时间" min-width="100">
            </el-table-column>
            <el-table-column v-if="isColumnShow('updateTime')" prop="updateTime" :formatter="DateTimeFormatter" label="更新时间" min-width="100">
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="150">
                <template slot-scope="scope">
                    <el-button @click="editRow(scope.row)" type="primary" size="mini" plain>编辑</el-button>
                    <el-button @click="getImages(scope.row)" type="primary" size="mini" plain>图片</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="pagination-wrapper">
            <el-pagination background @size-change="pageSizeChange" @current-change="currentPageChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalNumber">
            </el-pagination>
        </div>

        <el-dialog title="查看图片" :visible.sync="showDialog">
            <div>
                <img style="width: 200px" v-for="item in images" :src="item" @click="showImg(item)">

            </div>

            <span slot="footer" class="dialog-footer">
                <el-button @click="showDialog = false">确定</el-button>
            </span>
        </el-dialog>

        <el-dialog :visible.sync="imageDialogVisible" size="small">
            <img width="100%" :src="imgSrc" alt="">
        </el-dialog>

    </div>

</template>
<script>
import { mapState } from 'vuex'
import { format } from 'date-fns'
import zh from 'date-fns/locale/zh_cn'

export default {
    created() {
        this.getData();
    },
    data() {
        return {
            totalNumber: 0,
            totalPage: 10,
            currentPage: 1,
            pageSize: 20,
            tableData: [],
            filter1: '',
            filter2: '',
            tableColumns: [
                {
                    label: '用户ID',
                    value: 'userId',
                    show: true
                },
                {
                    label: '用户名',
                    value: 'userName',
                    show: true
                },
                {
                    label: '描述',
                    value: 'remark',
                    show: true
                },
                // {
                //     label: '图片',
                //     value: 'image',
                //     show: false
                // },
                {
                    label: '联系电话',
                    value: 'telephone',
                    show: true
                },
                {
                    label: '创建时间',
                    value: 'createTime',
                    show: true
                },
                {
                    label: '更新时间',
                    value: 'updateTime',
                    show: true
                },
            ],
            multipleMode: false,
            showDialog: false,
            images: [],
            imgSrc: '',
            imageDialogVisible: false,

        }
    },
    computed: {
        ...mapState(['tableHeight']),
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        pageSizeChange(size) {
            this.currentPage = 1;
            this.pageSize = size;
            this.getData();
        },
        currentPageChange(page) {
            this.currentPage = page;
            this.getData();
        },
        getData() {
            this.$http.get({
                url: '/feedback/page',
                data: {
                    currentPage: this.currentPage,
                    pageNumber: this.pageSize,
                    searchKey: this.filter1,
                }
            }).then(res => {
                if (res.success) {
                    this.totalNumber = res.data.page.totalNumber;
                    this.tableData = res.data.pp;
                }
            })
        },
        isColumnShow(column) {
            var row = this.tableColumns.find(i => i.value === column);
            return row ? row.show : false;
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        editRow(row) {
            this.$router.push({
                path: '/feedback',
                query: {
                    id: row.id
                }
            })
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },
        getImages(row) {
            if (row.image) {

                this.images = row.image.split(',');
            }
            this.showDialog = true;
        },
        showImg(img) {
            this.imgSrc = img;
            this.imageDialogVisible = true;
        },
        DateTimeFormatter(row, column, cellValue) {
            if (cellValue) {
                return format(cellValue, 'YYYY/MM/DD HH:mm', { locale: zh })
            }

        },
        searchData() {
            this.currentPage = 1;
            this.getData();
        }
    }
}
</script>
<style lang="less" scoped>
</style>
