<template>
    <div class="context">
        <div class="panel">
            <h5>管理员管理</h5>
            <div class="panel-body">
                <div class="panel_control">
                    <el-row :gutter="20">
                        <el-col :span="4">
                            <em>姓名:</em>
                            <el-input v-model="panel.name" placeholder="请输入用户名"></el-input>
                        </el-col>
                        <el-col :span="4">
                            <em>手机号:</em>
                            <el-input v-model="panel.phone" placeholder="请输入手机号"></el-input>
                        </el-col>
                        <el-col :span="4">
                            <em>店面:</em>
                            <el-select v-model="panel.shopId">
                                <el-option
                                        v-for="item in panel.options"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-col>
                        <el-col :span="4">
                            <el-button size="" type="primary" @click="query" plain>查询</el-button>
                        </el-col>
                    </el-row>
                </div>
            </div>
        </div>
        <div class="change">
            <el-button @click="addAdmin">新增管理员</el-button>
            <el-button @click="editList">编辑</el-button>
            <el-button @click="delList">删除</el-button>
        </div>
        <div class="table">
            <el-table
                    :data="tableData"
                    border
                    is-horizontal-resize
                    :default-sort="{prop: 'date', order: 'descending'}"
                    element-loading-background="rgba(0, 0, 0, 0.8)"
                    class=""
                    @selection-change="handleSelectionChange"
            >
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        type="index"
                        label="序号"
                        width="50">
                </el-table-column>
                <el-table-column
                        prop="Usercode"
                        label="用户名"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="Name"
                        label="姓名"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="Phone"
                        label="手机号"
                >
                </el-table-column>
                <el-table-column
                        prop="ShopName"
                        label="所属门店"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="LastLoginTime"
                        label="上次登陆时间"
                        :formatter="filterFmtDate"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="Memo"
                        label="备注"
                        sortable
                >
                    <template slot-scope="scope">
                        <el-popover
                                placement="top"
                                title="备注"
                                width="200"
                                trigger="hover"
                                :content="scope.row.Memo">
                            <span slot="reference"
                                  v-if="scope.row.Memo.length > 10">{{scope.row.Memo.substr(0,10)}} ....</span>
                        </el-popover>
                        <span v-if="scope.row.Memo.length <= 10">{{scope.row.Memo}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="Status"
                        label="操作"
                >
                    <template slot-scope="scope">
                        <el-button type="danger" size="mini" v-if="scope.row.Status == 1"
                                   @click="runAndPause(scope.row,8)">禁用
                        </el-button>
                        <el-button type="success" size="mini" v-if="scope.row.Status == 8"
                                   @click="runAndPause(scope.row,1)">启用
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <br>
            <el-pagination
                    background
                    :total="pageination.total"
                    :page-size="pageination.pageItem"
                    @current-change="pageChange"
            ></el-pagination>
        </div>

        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="650px">
            <div>
                <el-form ref="form" :model="form" label-width="80px">
                    <el-form-item label="登陆账号">
                        <el-input v-model="form.userCode"></el-input>
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input v-model="form.password" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="姓名">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号">
                        <el-input v-model="form.phone"></el-input>
                    </el-form-item>
                    <el-form-item label="所属门店">
                        <el-select v-model="form.shopId" placeholder="请选择所属门店">
                            <el-option
                                    v-for="item in shops"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="备注">
                        <el-input v-model="form.memo"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <div class="dialogFooter">
                <el-button type="primary" size="small" @click="confirmAdmin" v-if="form.btnState == 0">确定</el-button>
                <el-button type="primary" size="small" @click="confirmEdit" v-if="form.btnState == 1">确定</el-button>
                <el-button @click="dialogVisible = false" size="small">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import Global from '../Global.js'
    import {
        ShopManagerListQuery,
        ShopListQuery,
        ShopManagerAdd,
        ShopManagerEdit,
        ShopManagerStatusEdit,
        testTable,
        testSelect
    } from "../api/getApiRes";

    let qs = require('qs');
    export default {
        data() {
            return {
                dialogVisible: false,
                dialogTitle: '新增管理员',
                // panel 配置项目
                panel: {
                    userCode: '',
                    name: '',
                    phone: '',
                    shopId: '',
                    options: [],
                    draw: 1,
                    start: 0,
                    recordsTotal: 0,
                    tableData: [],
                    allTableData: [],
                    limit: '10',
                    multipleSort: false,
                    loading: false,
                    fileList: [],
                    multipleSelection: [],
                    detectedmac: '',
                    time1: globalBt(),
                },
                multipleSelection: [],
                pageination: {
                    pageItem: 100,
                    pageoptions: pageOptions(),
                    total: 100,
                    pageIndex: 1,
                },
                form: {
                    shopId: '',
                    userId: '',
                    userCode: '',
                    password: '',
                    name: '',
                    phone: '',
                    memo: '',
                    btnState: 0,
                },
                shops: [],
                tableData: []
            }
        },
        mounted() {
            this.panelSelect();
            this.getTableQuery();
        },
        methods: {
            // 新增 确认提交
            confirmAdmin() {
                let that = this;
                // checkNum
                if (!that.form.userCode) {
                    this.$message.error('错了哦，登陆账号不能为空');
                    return false
                }
                if (that.form.name.length > 20) {
                    this.$message.error('错了哦，登陆账号字数超过20个字');
                    return false
                }
                if (!that.form.password) {
                    this.$message.error('错了哦，密码不能为空');
                    return false
                }
                if (that.form.password.length < 6) {
                    this.$message.error('错了哦，密码字数小于6个字');
                    return false
                }
                if (that.form.password.length > 8) {
                    this.$message.error('错了哦，密码字数超过8个字');
                    return false
                }
                if (!that.form.name) {
                    this.$message.error('错了哦，姓名不能为空');
                    return false
                }
                if (that.form.name.length > 8) {
                    this.$message.error('错了哦，姓名字数超过8个字');
                    return false
                }
                if (!that.form.phone) {
                    this.$message.error('错了哦，手机号码不能为空');
                    return false
                }
                if (!globalCheckPhone(that.form.phone)) {
                    this.$message.error('错了哦，手机号格式不正确');
                    return false
                }
                if (that.form.memo) {
                    if (that.form.name.memo > 200) {
                        this.$message.error('错了哦，备注字数超过200个字');
                        return false
                    }
                }

                let param = {
                    token: localStorage.token,
                    shopId: that.form.shopId,
                    usercode: that.form.userCode,
                    password: that.form.password,
                    name: that.form.name,
                    phone: that.form.phone,
                    memo: that.form.memo,
                };
                let postdata = qs.stringify(param);
                ShopManagerAdd(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        // 关闭弹窗
                        that.dialogVisible = false;
                        that.$message({
                            showClose: true,
                            message: '管理员添加成功！',
                            type: 'success'
                        });

                        // 重载列表
                        that.getTableQuery();
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            confirmEdit() {
                // ShopManagerEdit
                let that = this;
                // checkNum
                if (!that.form.userCode) {
                    this.$message.error('错了哦，登陆账号不能为空');
                    return false
                }
                if (that.form.name.length > 20) {
                    this.$message.error('错了哦，登陆账号字数超过20个字');
                    return false
                }

                // 密码有输入才检测，没输入默认传空不修改
                if (that.form.password) {
                    if (that.form.password.length < 6) {
                        this.$message.error('错了哦，密码字数小于6个字');
                        return false
                    }
                    if (that.form.password.length > 8) {
                        this.$message.error('错了哦，密码字数超过8个字');
                        return false
                    }
                }
                if (!that.form.name) {
                    this.$message.error('错了哦，姓名不能为空');
                    return false
                }
                if (that.form.name.length > 8) {
                    this.$message.error('错了哦，姓名字数超过8个字');
                    return false
                }
                if (!that.form.phone) {
                    this.$message.error('错了哦，手机号码不能为空');
                    return false
                }
                if (!globalCheckPhone(that.form.phone)) {
                    this.$message.error('错了哦，手机号格式不正确');
                    return false
                }
                if (that.form.memo) {
                    if (that.form.name.memo > 200) {
                        this.$message.error('错了哦，备注字数超过200个字');
                        return false
                    }
                }

                let param = {
                    token: localStorage.token,
                    userId: that.form.userId,
                    shopId: that.form.shopId,
                    usercode: that.form.userCode,
                    password: that.form.password,
                    name: that.form.name,
                    phone: that.form.phone,
                    memo: that.form.memo,
                };
                let postdata = qs.stringify(param);
                ShopManagerEdit(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        // 关闭弹窗
                        that.dialogVisible = false;
                        that.$message({
                            showClose: true,
                            message: '管理员信息修改成功！',
                            type: 'success'
                        });

                        // 重载列表
                        that.getTableQuery();
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 加载选项
            panelSelect() {
                let that = this;
                let param = {
                    token: localStorage.token,
                };
                let postdata = qs.stringify(param);
                ShopListQuery(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        that.panel.options = turnShopResToOption(json.Rs);
                        that.shops = turnShopResToOption(json.Rs);
                        that.form.shopId = json.Rs[0].ShopID;
                        that.panel.options.unshift({value: '', label: "全部"});
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 删除
            delList() {
                let that = this;
                // checkNum
                if (!this.multipleSelection.length) {
                    that.$message({
                        showClose: true,
                        message: '错了哦，需要先选中至少一条记录',
                        type: 'error'
                    });
                    return false
                }
                if (this.multipleSelection.length != 1) {
                    that.$message({
                        showClose: true,
                        message: '错了哦，只能选中一条记录',
                        type: 'error'
                    });
                    return false
                }

                let userId  = that.multipleSelection[0].Id;

                let param = {
                    token: localStorage.token,
                    userId : userId ,
                    status: 9,//0禁用1启用9删除
                };
                let postdata = qs.stringify(param);

                this.$confirm('此操作将永久删除该管理员, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    ShopManagerStatusEdit(postdata).then(res => {
                        let json = res;
                        if (json.Code == 0) {
                            that.$message({
                                showClose: true,
                                message: '选中的管理员已删除！',
                                type: 'success'
                            });
                            // 重载列表
                            that.getTableQuery();
                        } else {
                            that.$message.error(json.Memo);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            runAndPause(row, status) {
                let that = this;
                let param = {
                    token: localStorage.token,
                    userId: row.Id,
                    status: status,
                };
                let postdata = qs.stringify(param);
                ShopManagerStatusEdit(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        that.$message({
                            showClose: true,
                            message: '管理员状态已修改成功！',
                            type: 'success'
                        });
                        // 重载列表
                        that.getTableQuery();
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            clearForm() {
                // clear
                this.form.shopId = this.shops[0].value;
                this.form.name = '';
                this.form.userCode = '';
                this.form.password = '';
                this.form.phone = '';
                this.form.memo = '';
            },
            // 新增管理员
            addAdmin() {
                this.clearForm();
                // 重载店面列表
                this.panelSelect();
                this.dialogVisible = true
                this.dialogTitle = '新增管理员'
                this.form.btnState = 0;
            },
            // 编辑管理员
            editList() {
                let that = this;
                this.clearForm();
                // 重载门店列表
                this.panelSelect();
                // checkNum
                if (!this.multipleSelection.length) {
                    that.$message({
                        showClose: true,
                        message: '错了哦，需要先选中至少一条记录',
                        type: 'error'
                    });
                    return false
                }
                if (this.multipleSelection.length != 1) {
                    that.$message({
                        showClose: true,
                        message: '错了哦，只能选中一条记录',
                        type: 'error'
                    });
                    return false
                }
                let row = that.multipleSelection[0];
                this.form.shopid = row.ShopId;
                this.form.userId = row.Id;
                this.form.name = row.Name;
                this.form.userCode = row.Usercode;
                this.form.password = '';
                this.form.phone = row.Phone;
                this.form.memo = row.Memo;
                this.dialogVisible = true
                this.dialogTitle = '编辑管理员'
                this.form.btnState = 1;
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 查询按钮
            query() {
                this.getTableQuery();
                this.$message.success('查询完毕');
            },
            // 页面数据查询
            getTableQuery() {
                let that = this;
                that.loading = true;
                let param = {
                    token: localStorage.token,
                    shopId: this.panel.shopId,
                    name: this.panel.name,
                    phone: this.panel.phone,
                    start: 1,//
                    tableMax: 9999,//
                };
                let postdata = qs.stringify(param);
                ShopManagerListQuery(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        that.loading = false;
                        if (json.Rs) {
                            that.allTableData = json.Rs;
                            that.recordsTotal = json.Rs.length;
                        } else {
                            that.allTableData = [];
                            that.recordsTotal = 0;
                        }
                        // 设置分页数据
                        that.setPaginations();
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 设置分页数据
            setPaginations() {
                // 分页属性
                let that = this;
                that.pageination.total = that.recordsTotal;
                // 默认分页
                that.tableData = that.allTableData.filter((item, index) => {
                    return index < that.pageination.pageItem;
                });
            },
            // 每页显示数量
            handleSizeChange() {
                let that = this;
                that.tableData = that.allTableData.filter((item, index) => {
                    return index < that.pageination.pageItem;
                });
                that.draw = that.pageination.pageItem;
                that.getTableQuery();
            },
            // 翻页
            pageChange(pageIndex) {
                let that = this;
                // 获取当前页
                let index = that.pageination.pageItem * (pageIndex - 1);
                // 数据总数
                let nums = that.pageination.pageItem * pageIndex;
                // 容器
                let tables = [];
                for (var i = index; i < nums; i++) {
                    if (that.allTableData[i]) {
                        tables.push(that.allTableData[i])
                    }
                    this.tableData = tables;
                }
                that.start = index * that.draw;
                that.getTableQuery();
            },
            // 自动排序
            sortChange(params) {
                console.log(params)
            },
            // 过滤时间
            filterFmtDate(value, row, column) {
                let that = this;
                return nonTfmtDate(column, 11);
            },
            // 过滤金额
            filterMoney(value, row, column) {
                let that = this;
                return parseFloat(column).toFixed(2);
            },
        },
    }
</script>

<style scoped>
    @import "../assets/css/panel.css";

    .context {   height: 770px;
         overflow-y: scroll;

        display: block;
        margin: 0 auto;
        background-color: #fff !important;
        padding: 30px;
        padding-bottom: 60px;
    }

    .panel-body {
        padding: 20px;
        background: #F0F2F5;
    }

    .change {
        width: 100%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        padding-top: 10px;
        padding-bottom: 10px;
    }

    .change button {
        float: left;
    }

    .change button.pull-right {
        float: right;
    }

    .dialogTitle {
        width: 100%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        color: #000000;
        font-size: 18px;
        text-align: center;
    }

    .dialogTitle em {
        float: none;
        font-style: normal;
        color: #3799FF;
        margin: 0;
    }

    /deep/ .el-transfer-panel__item .el-checkbox__input {
        left: 40px;
    }

    .dialogFooter {
        width: 90%;
        overflow: hidden;
        display: block;
        margin: 0 auto;
        margin-top: 10px;
    }

    .dialogFooter button {
        float: right;
        margin-left: 10px;
    }
    /deep/ .el-dialog .el-select .el-input__inner {
        width: 240px;
    }
    /deep/ .el-dialog .el-select {
        width: 530px;
    }
    /deep/ .el-dialog .el-select .el-input__inner {
        width: 530px;
    }
</style>
