<template>
    <div class="container">

        <div class="pageTitle">
            客服管理
            <el-button type="primary" class="add" size="mini" @click="goNext">添加客服</el-button>
        </div>

        <div class="content" style="min-height:450px">
            <el-table class="table" :data="tableData" row-key="id" ref="table">
                <el-table-column type="index" align="center">
                </el-table-column>
                <el-table-column prop="icon" label="头像" align="center">
                    <template slot-scope="{row}">
                        <img :src="row.icon+'?x-oss-process=image/resize,m_mfit,h_50,w_50'" style="border-radius:100%;width:50px;height:50px" />
                    </template>

                </el-table-column>

                <el-table-column prop="serviceName" label="客服名" align="center">
                </el-table-column>

                <!-- <el-table-column v-if="isColumnShow('password')" prop="password" label="密码" min-width="100">
            </el-table-column> -->

                <el-table-column prop="username" label="登录名" align="center">
                </el-table-column>

                <el-table-column prop="useFlag" label="可用标识" align="center">
                </el-table-column>

                <el-table-column prop="statusFlag" label="状态" align="center">
                </el-table-column>

                <el-table-column prop="loginTime" :formatter="DateTimeFormatter" min-width="140" label="登录时间" align="center">
                </el-table-column>
                <el-table-column label="操作" align="center" fixed="right" min-width="150">
                    <template slot-scope="scope">
                        <el-button @click="editRow(scope.row)" type="primary" size="mini" plain>编辑</el-button>
                        <el-button @click="deleteRow(scope.row)" type="danger" size="mini" plain>删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="pagination-wrapper" v-if="totalNumber>10">

            <el-pagination background @current-change="currentPageChange" :current-page="currentPage" :page-size="pageSize" 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'

export default {
    created() {
        this.getData();
    },
    data() {
        return {
            totalNumber: 0,
            totalPage: 10,
            currentPage: 1,
            pageSize: 10,
            tableData: [],
            filter1: '',
            filter2: '',
            tableColumns: [
                {
                    label: '店铺',
                    value: 'storeId',
                    show: true
                },
                {
                    label: '客服名',
                    value: 'serviceName',
                    show: true
                },
                {
                    label: '密码',
                    value: 'password',
                    show: true
                },
                {
                    label: '登录名',
                    value: 'username',
                    show: true
                },
                {
                    label: '头像',
                    value: 'icon',
                    show: true
                },
                {
                    label: '可用标识',
                    value: 'useFlag',
                    show: true
                },
                {
                    label: '状态',
                    value: 'statusFlag',
                    show: true
                },
                {
                    label: '登录时间',
                    value: 'loginTime',
                    show: true
                },
            ],
            multipleMode: false,
            showAdvancedQueryDialog: false,
            advancedQueryFields: [],
            showTableSortDialog: false,
            tableSortFields: [],
            searchMethods: ['=', '!=', '>', '>=', '<', '<=', 'like'],
            advancedQueryColumns: [
                {
                    label: '店铺',
                    value: 'store_id'
                },
                {
                    label: '客服名',
                    value: 'service_name'
                },
                {
                    label: '密码',
                    value: 'password'
                },
                {
                    label: '登录名',
                    value: 'username'
                },
                {
                    label: '头像',
                    value: 'icon'
                },
                {
                    label: '可用标识',
                    value: 'use_flag'
                },
                {
                    label: '状态',
                    value: 'status_flag'
                },
                {
                    label: '登录时间',
                    value: 'login_time'
                },
            ],
            advancedQuerySearchKey: '',
            orderByStr: '',
            imgSrc: '',
            imageDialogVisible: false,
        }
    },
    computed: {
        ...mapState(['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,
                storeId:this.myStoreInfo.id
            }

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

            this.$http.get({
                url: '/customerService/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();
            }
        },
        goNext() {
            this.$router.push({
                path: '/customerService',
            })
        },
        editRow(row) {
            this.$router.push({
                path: '/customerService',
                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 + "/customerService/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: '/customerService/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;
        },

    }
}
</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: 24px;

    .table {
        border-top: 1px solid #ebebeb;
        border-right: 1px solid #ebebeb;
        border-left: 1px solid #ebebeb;
        margin: 0 20px;
        width: 95%;
    }

    .pageTitle {
        font-size: 14px;
        font-family: MicrosoftYaHei-Bold;
        font-weight: bold;
        color: rgba(51, 51, 51, 1);
        line-height: 19px;
        padding-bottom: 9px;
        margin-bottom: 30px;
        border-bottom: 1px solid #ebebeb;
        position: relative;

        .add {
            position: absolute;
            right: 20px;
            top: -10px;
        }
    }
}
</style>
