<template>
    <div class="container">
        <div class="list">
            <template v-if="tableData.length>0">
                <comment-item style="border-bottom:1px solid #ebebeb;" :type='"store"' :info='item' v-for="item in tableData"></comment-item>
            </template>
            <div v-else class="nothing">
                <img src="../assetsPre/nothingImg.png" alt="">
                <p>还没有评论哦</p>
            </div>

        </div>

        <div class="pagination-wrapper" v-if="totalNumber>6" style="padding:20px;">

            <el-pagination background @current-change="currentPageChange" :current-page="currentPage" :page-size="6" layout="total, prev, pager, next" :total="totalNumber">
            </el-pagination>
        </div>

    </div>
</template>
<script>
import { mapState } from 'vuex'
import { format } from 'date-fns'
import zh from 'date-fns/locale/zh_cn'
import commentItem from '../pagesPre/CommentItem'
export default {
    created() {
        this.getData();
    },
    data() {
        return {
            totalNumber: 0,
            totalPage: 10,
            currentPage: 1,
            pageSize: 6,
            tableData: []
        }
    },
    computed: {
        ...mapState(['userInfo', 'myStoreInfo']),
        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() {

            var data = {
                currentPage: this.currentPage,
                pageNumber: this.pageSize,
                searchKey: this.filter1,
                advancedQuery: this.advancedQuerySearchKey,
                orderByStr: this.orderByStr,
                // userId: this.userInfo.id
                storeId: this.myStoreInfo.id,
                typeFlag: 0
            }

            if (this.$route.query.column) {
                var tempColumn = this.$route.query.column;
                data[tempColumn.split(',')[1]] = tempColumn.split(',')[0];
            }

            this.$http.get({
                url: '/productComment/page',
                data: data
            }).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: '/productComment',
                query: {
                    id: row.id,
                    column: this.$route.query.column,
                }
            })
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },
        addField() {
            this.advancedQueryFields.push({
                link: 'AND',
                name: '',
                searchMethod: '=',
                value: '',
            });
        },
        removeField(i) {
            if (this.advancedQueryFields.length > 0) {
                this.advancedQueryFields.splice(i, 1);
            }
        },
        advancedQuery() {

            this.advancedQuerySearchKey = '';

            if (this.advancedQueryFields.length > 0) {

                var templist = [];

                this.advancedQueryFields.forEach(item => {
                    if (item.link && item.name && item.searchMethod && item.value) {
                        var tempItem = item.link + '_,' + item.name + '_,' + item.searchMethod + '_,' + item.value;
                        templist.push(tempItem);
                    }
                })

                if (templist.length > 0) {

                    this.advancedQuerySearchKey = templist.join('_;');
                }
            }

            this.getData();
            this.showAdvancedQueryDialog = false;
        },
        addSortField() {
            this.tableSortFields.push({
                name: '',
                order: 'asc',
            });
        },
        removeSortField(i) {
            if (this.tableSortFields.length > 0) {
                this.tableSortFields.splice(i, 1);
            }
        },
        tableSortQuery() {

            this.orderByStr = '';

            if (this.tableSortFields.length > 0) {

                var templist = [];

                this.tableSortFields.forEach(item => {
                    if (item.name && item.order) {
                        var tempItem = item.name + '_,' + item.order;
                        templist.push(tempItem);
                    }
                })

                if (templist.length > 0) {

                    this.orderByStr = templist.join('_;');
                }
            }

            this.getData();
            this.showTableSortDialog = false;
        },
        exportExcel() {
            window.location.href = this.$baseUrl + "/productComment/exportExcel?searchKey="
                + this.filter1 + "&advancedQuery=" + this.advancedQuerySearchKey + "&orderByStr=" + this.orderByStr;
        },
        searchData() {
            this.currentPage = 1;
            this.getData();
        },
        deleteRow(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/productComment/del',
                    data: { id: row.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.getData();
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
        DateTimeFormatter(row, column, cellValue) {
            if (cellValue) {
                return format(cellValue, 'YYYY/MM/DD HH:mm', { locale: zh })
            }

        },
        DateFormatter(row, column, cellValue) {
            if (cellValue) {
                return format(cellValue, 'YYYY/MM/DD', { locale: zh })
            }

        },
        showImg(img) {
            this.imgSrc = img;
            this.imageDialogVisible = true;
        },

    },
    components: {
        commentItem
    }
}
</script>
<style lang="less" scoped>
.container {
    min-height: 598px;
    background: rgba(255, 255, 255, 1);
    border-radius: 4px;
    border: 1px solid rgba(234, 234, 234, 1);
    padding: 0 40px 0 24px;
    .list {
        min-height: 500px;
        overflow: hidden;
    }

    .nothing {
        margin-top: 136px;
        img {
            width: 167px;
            height: 167px;
            display: block;
            margin: auto;
        }

        p {
            font-size: 14px;
            text-align: center;
            color: rgba(51, 51, 51, 1);
            line-height: 19px;
            margin-top: 24px;
        }
    }
}
</style>
