<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.shopName" placeholder="请输入店铺名称"></el-input>
                        </el-col>
                        <el-col :span="4">
                            <em>负责人:</em>
                            <el-input v-model="panel.contacts" 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">
                            <el-button size="" type="primary" @click="query" plain>查询</el-button>
                        </el-col>
                    </el-row>
                </div>
            </div>
        </div>
        <div class="change">
            <el-button @click="addAdmin" type="primary">新增店面</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" @current-change="clickChange"
            >

                >
                <el-table-column label="选择" width="55">
    <template slot-scope="scope">
        <el-radio  v-model="tableRadio" :label="scope.row"><i></i></el-radio>
    </template>
</el-table-column>
                <el-table-column
                        type="index"
                        label="序号"
                        align="center"

                        width="50">
                </el-table-column>
                <el-table-column
                        prop="ShopName"
                        label="店面名称"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="Addr"
                        label="位置"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="Contacts"
                        label="负责人"
                        sortable
                >
                </el-table-column>
                <el-table-column
                        prop="Phone"
                        label="手机号"
                >
                </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.shopName"></el-input>
                    </el-form-item>
                    <el-form-item label="位置">
                        <el-input v-model="form.addr"></el-input>
                    </el-form-item>
                    <el-form-item label="负责人">
                        <el-input v-model="form.contacts"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号">
                        <el-input v-model="form.phone"></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="confirmEdite" 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 {
        ShopAdd,
        ShopDetailQuery,
        ShopEdit,
        ShopListQuery,
        ShopStatusEdit,
        testTable,
        testSelect
    } from "../api/getApiRes";

    let qs = require('qs');
    export default {
        data() {
            return {
                dialogVisible: false,
                dialogTitle: '新增店面',
                // panel 配置项目
                panel: {
                    shopName: '',
                    contacts: '',
                    phone: '',
                    userCode: '',
                    tel: '',
                    shopList: 0,
                    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: {
                    shopName: '',
                    addr: '',
                    name: '',
                    contacts: '',
                    phone: '',
                    userCode: '',
                    shopId: '',
                    btnState: 0,
                },
                shops: [],
                tableData: [],
                tableRadio: [],
            }
        },
        mounted() {
            this.getTableQuery();
        },
        methods: {
            clickChange (item) {
                this.tableRadio = item
            },
            // 新增 确认提交
            confirmAdmin() {
                let that = this;
                // checkNum
                if (!that.form.shopName) {
                    this.$message.error('错了哦，店面名称不能为空');
                    return false
                }
                if (that.form.shopName.length > 18) {
                    this.$message.error('错了哦，姓名字数超过18个字');
                    return false
                }
                if (!that.form.addr) {
                    this.$message.error('错了哦，位置不能为空');
                    return false
                }
                if (that.form.addr.length > 38) {
                    this.$message.error('错了哦，位置字数超过38个字');
                    return false
                }
                if (!that.form.contacts) {
                    this.$message.error('错了哦，负责人不能为空');
                    return false
                }
                if (that.form.contacts.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
                }
                let param = {
                    token: localStorage.token,
                    shopName: that.form.shopName,
                    addr: that.form.addr,
                    contacts: that.form.contacts,
                    phone: that.form.phone,
                    shopid: that.form.shopid,
                };
                let postdata = qs.stringify(param);
                ShopAdd(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        // 关闭弹窗
                        that.dialogVisible = false;
                        // 重载列表
                        that.getTableQuery();
                        that.$message({
                            showClose: true,
                            message: '店面添加成功！',
                            type: 'success'
                        });
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 修改
            confirmEdite() {
                let that = this;
                // checkNum
                if (!that.form.shopName) {
                    this.$message.error('错了哦，店面名称不能为空');
                    return false
                }
                if (that.form.shopName.length > 18) {
                    this.$message.error('错了哦，姓名字数超过18个字');
                    return false
                }
                if (!that.form.addr) {
                    this.$message.error('错了哦，位置不能为空');
                    return false
                }
                if (that.form.addr.length > 38) {
                    this.$message.error('错了哦，位置字数超过38个字');
                    return false
                }
                if (!that.form.contacts) {
                    this.$message.error('错了哦，负责人不能为空');
                    return false
                }
                if (that.form.contacts.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
                }
                let param = {
                    token: localStorage.token,
                    shopName: that.form.shopName,
                    addr: that.form.addr,
                    contacts: that.form.contacts,
                    phone: that.form.phone,
                    shopId: that.form.shopId,
                };
                let postdata = qs.stringify(param);
                ShopEdit(postdata).then(res => {
                    let json = res;
                    if (json.Code == 0) {
                        // 关闭弹窗
                        that.dialogVisible = false;

                        // 重载列表
                        that.getTableQuery();
                        that.$message({
                            showClose: true,
                            message: '店面编辑成功！',
                            type: 'success'
                        });
                    } else {
                        that.$message.error(json.Memo);
                    }
                })
            },
            // 删除
            delList() {
                let that = this;
                if(this.tableRadio.length == 0){
                    this.$message.error("请先选中一条记录");
                    return false
                }
                let ShopID = this.tableRadio.ShopID;

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

                this.$confirm('此操作将永久删除该店面, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    ShopStatusEdit(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: '已取消删除'
                    });
                });
            },
            clearForm() {
                // clear
                this.form.shopName = '';
                this.form.addr = '';
                this.form.name = '';
                this.form.userCode = '';
                this.form.shopId = '';
                this.form.phone = '';
                this.form.contacts = '';
            },
            // 新增店面
            addAdmin() {
                this.clearForm();
                this.dialogVisible = true
                this.dialogTitle = '新增店面'
                this.form.btnState = 0; //新增
            },
            // 编辑店面
            editList() {
                let that = this;
                // checkNum
                this.clearForm();
                this.form.btnState = 1; //编辑
                if(this.tableRadio.length == 0){
                    this.$message.error("请先选中一条记录");
                    return false
                }
                let row = this.tableRadio;
                this.form.shopId = row.ShopID;
                this.form.shopName = row.ShopName;
                this.form.addr = row.Addr;
                this.form.contacts = row.Contacts;
                this.form.phone = row.Phone;

                this.dialogVisible = true;
                this.dialogTitle = '编辑店面'
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 查询按钮
            query() {
                this.getTableQuery();
                this.$message.success('查询完毕');
            },
            // 页面数据查询
            getTableQuery() {
                let that = this;
                that.loading = true;
                // 查询检测设备。上级区域id，区域id必传。regionid传0，查询supregionid对应所有子区域的检测设备。 如果supregionid，regionid都传0，默认查询企业ID下所有检测设备
                let param = {
                    token: localStorage.token,
                    shopName: this.panel.shopName,//
                    contacts: this.panel.contacts,//
                    phone: this.panel.phone,//
                };
                let postdata = qs.stringify(param);
                ShopListQuery(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;
    }
</style>
