<%@ page contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="renderer" content="webkit">
    <!-- 移动设备 viewport -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <jsp:include page="../../common.jsp"/>
</head>
<body>
<div class="row animated fadeInRight" style="margin: 0;">
    <div class="wrapper wrapper-content mt5 right-content ml5 mr5">
        <div class="toolbar-panel bdd">
            <div class="toolbar-box">
                <div class="toolbar-head clearfix lgraybg pt5 pb10 pl10 bdbd">
                    <!-- 顶部按钮 -->
                    <div class="fl">
                        查询条件
                    </div>
                    <div class="tools fr">
                        <a href="javascript:void(0);" class="collapse dblock f14 dgray mr5">
                            <i class="fa-2x fa fa-angle-double-up"></i>
                        </a>
                    </div>
                </div>
            </div>
            <!--搜索条件 -->
            <div class="toolbar-body">
                <form class="layui-form" action="" style="margin-top: 10px">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">音箱编号</label>
                            <div class="layui-input-block">
                                <input type="text" name="boxNo" id="boxNo" placeholder="请输入音箱编号" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">购买商家</label>
                            <div class="layui-input-block">
                                <input type="text" name="buyMercName" id="buyMercName" placeholder="请输入购买商家名称"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">激活商家</label>
                            <div class="layui-input-block">
                                <input type="text" name="actMercName" id="actMercName" placeholder="请输入激活商家名称"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-block">
                                <select class="layui-select" name="status">
                                    <option value="">请选择</option>
                                    <option value="1">未出售</option>
                                    <option value="2">已出售</option>
                                    <option value="3">已激活</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="mb50 mt10">
        <div class="whitebg pl10 pr10">
            <table class="layui-hide" id="deviceVoiceBoxTab" lay-filter="deviceVoiceBoxTab"></table>
        </div>
    </div>
</div>

<script type="text/template" id="add">
    <form class="layui-form" id="form2" style="margin-top: 2rem;">
        <input name="id" id="id" hidden>
        <div class="layui-form-item" style="width: 300px;">
            <label class="layui-form-label">音箱名称</label>
            <div class="layui-input-block">
                <input type="text" name="boxName" id="boxName" placeholder="请输入音箱名称" autocomplete="off" value="QR686"
                       class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item" style="width: 300px;">
            <label class="layui-form-label">音箱编号</label>
            <div class="layui-input-block">
                <input type="text" name="boxNo" id="addBoxNo" placeholder="请输入编号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
    </form>
</script>

<script type="text/template" id="addBatch">
    <form class="layui-form" id="form3" style="margin-top: 2rem;">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">编号范围</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <input type="text" name="startBoxNo" id="startBoxNo" placeholder="开始编号" autocomplete="off"
                           class="layui-input">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width: 200px;">
                    <input type="text" name="endBoxNo" id="endBoxNo" placeholder="结束编号" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
        </div>
    </form>
</script>

<script type="text/template" id="bind">
    <form class="layui-form" id="form4" style="margin-top: 2rem;">
        <input name="id" id="bindId" hidden>
        <input name="status" id="status" value="3" hidden>
        <div class="layui-form-item" style="width: 250px;">
            <label class="layui-form-label">商户ID</label>
            <div class="layui-input-block">
                <input type="text" name="actMercId" id="actMercId" placeholder="请输入商户ID" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
    </form>
</script>

<script type="text/html" id="leftBar">
    <button type="button" class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon">&#xe608;</i>新增</button>
    <button type="button" class="layui-btn layui-btn-sm" lay-event="addBatch"><i class="layui-icon">&#xe608;</i>批量新增
    </button>
    <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="search"><i class="layui-icon">&#xe615;</i>查询
    </button>
</script>

<script type="text/html" id="toolBar">
    {{#  if(d.status == 3){ }}
    <button type="button" class="layui-btn layui-btn-sm layui-btn-warm" lay-event="unbind"><i class="layui-icon">&#xe64d;</i>解绑
    </button>
    {{# } }}
    {{#  if(d.status == 1 || d.status == 2){ }}
    <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="bind"><i class="layui-icon">&#xe64c;</i>绑定
    </button>
    {{# } }}
    <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del"><i
            class="layui-icon">&#xe640;</i>删除
    </button>
</script>

<script>
    layui.use(['table', 'layer', 'form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var tableIns = table.render({
            elem: '#deviceVoiceBoxTab'
            , url: '${ctx}/deviceVoiceBox/page'
            , id: 'deviceVoiceBox'
            , response: {
                statusName: 'code' //数据状态的字段名称，默认：code
                , countName: 'count' //数据总数的字段名称，默认：count
                , dataName: 'data' //默数据列表的字段名称，认：data
                , statusCode: 200
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            , toolbar: '#leftBar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter']
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '智能音箱信息表'
            , cols: [[
                {type: 'checkbox', fixed: 'left', hidden: true}
                , {field: 'numbers', title: '序号', fixed: 'left', unresize: true, width: 80, type: 'numbers'}
                , {field: 'boxName', title: '音箱名称'}
                , {
                    field: 'boxNo', title: '音箱编号', templet: function (d) {
                        return "NO." + d.boxNo;
                    }
                }
                , {field: 'orderNo', title: '购买单号'}
                , {field: 'buyMercName', title: '购买商家名称'}
                , {field: 'buyMercId', title: '购买商家ID'}
                , {field: 'actMercName', title: '激活商家名称'}
                , {field: 'actMercId', title: '激活商家ID'}
                , {
                    field: 'status', title: '状态', templet: function (d) {
                        if (d.status == '1') {
                            return "<span>未出售</span>"
                        } else if (d.status == '2') {
                            return "<span style='color: blue'>已出售</span>"
                        } else if (d.status == '3') {
                            return "<span style='color: green'>已激活</span>"
                        }
                    }
                }
                , {
                    field: 'createTime',
                    title: '创建时间',
                    width: 160,
                    templet: "<div>{{layui.util.toDateString(d.createTime,'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {fixed: 'right', title: '操作', width: 250, toolbar: '#toolBar'}
            ]]
            , request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , page: true
        });
        table.on('tool(deviceVoiceBoxTab)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'del') { //删除
                layer.confirm('真的删除么?', {icon: 3}, function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    del(data);
                    //向服务端发送删除指令
                });
            } else if (layEvent === 'bind') {
                toBind(obj);
            } else if (layEvent === 'unbind') {
                layer.confirm('确定要解绑吗?', {icon: 3}, function (index) {
                    layer.close(index);
                    var buyId = obj.data.buyMercId;
                    var status = 2;
                    if (buyId == null) {
                        status = 1;
                    }
                    toUnBindSave(status, obj.data.id);
                    //向服务端发送删除指令
                });
            }
        });

        //监听头工具栏事件
        table.on('toolbar(deviceVoiceBoxTab)', function (obj) {
            switch (obj.event) {
                case 'add':
                    // 添加
                    toAdd();
                    break;
                case 'addBatch':
                    // 添加
                    toAddBatch();
                    break;
                case 'search':
                    var params = serialize($('#form1'));
                    table.reload('deviceVoiceBox', {
                        where: params
                        , page: {
                            size: 10
                            , current: 1
                        }
                    });
                    break;
            }
        });

        function toBind(obj) {
            var index = layer.open({
                type: 1
                , shade: 0.5
                , title: '绑定'
                , maxmin: true
                , content: $("#bind").html()
                , area: ['350px', '300px']
                , btn: ['保存', '取消',]
                , shadeClose: true
                , btn1: function (index, layero) {
                    var actMercId = $("#actMercId").val();
                    if (!actMercId) {
                        layer.msg("商户ID不能为空!");
                        return;
                    }
                    toAddSave(index, $("#form4"));
                }
                , btn2: function (index, layero) {
                    layer.close(index);
                }
                , success: function () {
                    $("#bindId").val(obj.data.id);
                }
            });
        }

        function toAddBatch() {
            var index = layer.open({
                type: 1
                , shade: 0.5
                , title: '批量新增'
                , maxmin: true
                , content: $("#addBatch").html()
                , area: ['650px', '200px']
                , btn: ['保存', '取消',]
                , shadeClose: true
                , btn1: function (index, layero) {
                    var startBoxNo = $("#startBoxNo").val();
                    if (!startBoxNo) {
                        layer.msg("开始编号不能为空!");
                        return;
                    }
                    var endBoxNo = $("#endBoxNo").val();
                    if (!endBoxNo) {
                        layer.msg("结束编号不能为空!");
                        return;
                    }
                    var reg = /^[0-9]+$/;
                    if (!reg.test(startBoxNo)) {
                        layer.msg("开始编号格式不正确!");
                        return;
                    }
                    if (!reg.test(endBoxNo)) {
                        layer.msg("结束编号格式不正确!");
                        return;
                    }
                    if (parseInt(endBoxNo) - parseInt(startBoxNo) < 0) {
                        layer.msg("编号范围输入有误!");
                        return;
                    }
                    toAddBatchSave(index, startBoxNo, endBoxNo);
                }
                , btn2: function (index, layero) {
                    layer.close(index);
                }
            });
        }

        function toAdd(obj) {
            var index = layer.open({
                type: 1
                , shade: 0.5
                , title: '新增'
                , maxmin: true
                , content: $("#add").html()
                , area: ['400px', '400px']
                , btn: ['保存', '取消',]
                , shadeClose: true
                , btn1: function (index, layero) {
                    if (!$("#addBoxNo").val()) {
                        layer.msg("设备编号不能为空!");
                        return;
                    }
                    toAddSave(index, $("#form2"));
                }
                , btn2: function (index, layero) {
                    layer.close(index);
                }
            });
        }

        function toAddBatchSave(index, start, end) {
            var dialog = layer.msg('正在保存...', {icon: 16, time: 0, shade: [0.3, '#000', false]});
            var data = {};
            data.startBoxNo = start;
            data.endBoxNo = end;
            $.ajax({
                url: '${ctx}/deviceVoiceBox/batchSaveNo',
                method: 'post',
                data: JSON.stringify(data),
                dataType: 'JSON',
                contentType: "application/json",
                success: function (res) {
                    layer.close(dialog);
                    if (res.code == 200) {
                        layer.close(index);
                        layer.msg(res.message, {
                            icon: 1
                        });
                        tableIns.reload();
                    } else {
                        layer.msg(res.message, {
                            icon: 2
                        });
                    }
                }
            })
        }

        function toAddSave(index, dom) {
            var data = dom.serializeObject();
            $.ajax({
                url: '${ctx}/deviceVoiceBox/saveOrUpdate',
                method: 'post',
                data: JSON.stringify(data),
                dataType: 'JSON',
                contentType: "application/json",
                success: function (res) {
                    if (res.code == 200) {
                        layer.close(index);
                        layer.msg(res.message, {
                            icon: 1
                        });
                        tableIns.reload();
                    } else {
                        layer.msg(res.message, {
                            icon: 2
                        });
                    }
                }
            })
        }

        function toUnBindSave(status, id) {
            var data = {};
            data.status = status;
            data.id = id;
            $.ajax({
                url: '${ctx}/deviceVoiceBox/unBindSave',
                method: 'post',
                data: JSON.stringify(data),
                dataType: 'JSON',
                contentType: "application/json",
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.message, {
                            icon: 1
                        });
                        tableIns.reload();
                    } else {
                        layer.msg(res.message, {
                            icon: 2
                        });
                    }
                }
            })
        }

        function del(obj) {
            $.ajax({
                url: '${ctx}/deviceVoiceBox/del/' + obj.id,
                method: 'DELETE',
                dataType: 'JSON',
                contentType: "application/json",
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.data, {
                            icon: 1
                        });
                    } else {
                        layer.msg(res.data, {
                            icon: 2
                        });
                    }
                }
            })
        }
    });
    jQuery.prototype.serializeObject = function () {
        var obj = new Object();
        $.each(this.serializeArray(), function (index, param) {
            if (!(param.name in obj)) {
                obj[param.name] = param.value;
            }
        });
        return obj;
    };
</script>
</body>
</html>
