<%@ page contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<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="wrapper wrapper-content mt5 right-content ml5 mr5">
    <div class="toolbar-panel bdd">
        <!--搜索条件 -->
        <form action="${ctx}/device/page" method="get" id="condition" class="layui-form">
            <div class="toolbar-body">
                <div class="row pt10 pb10 whitebg ml0 mr0">
                    <span class="dlblock w100 textr">设备名称:</span>
                    <span class="bdc pt6 pb6 pl6 pr5 w180 dlblock">
							<input type="text" placeholder="请输入设备名称" id="name" name="name" value="">
						</span>
                    <span class="dlblock w100 textr">厂家名称:</span>
                    <span class="bdc pt6 pb6 pl6 pr5 w180 dlblock">
							<input type="text" placeholder="请输入厂家名称" id="venderName" name="venderName" value="">
						</span>
                    <span class="dlblock w100 textr">厂家型号:</span>
                    <span class="bdc pt6 pb6 pl6 pr5 w180 dlblock">
							<input type="text" placeholder="请输入厂家型号" id="venderModel" name="venderModel" value="">
						</span>
                    <div class="layui-inline">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <select id="status">
                                <option value="">请选择</option>
                                <option value="0">不显示</option>
                                <option value="1">显示</option>
                            </select>
                        </div>
                    </div>
                    <br>
                    <br>
                    <span class="dlblock w100 textr">开始日期:</span>
                    <span class="bdc pt6 pb6 pl6 pr5 w180 dlblock">
							 <input type="text" placeholder="" id="date1" name="startTime"
                                    class="w150"/>
							 <i class="fa fa-calendar"></i>
						</span>
                    <span class="dlblock w100 textr">结束日期:</span>
                    <span class="bdc pt6 pb6 pl6 pr5 w180 dlblock">
						<input type="text" placeholder="" id="date2" name="endTime"
                               class="w150"/>
							<i class="fa fa-calendar"></i>
						</span>
                </div>
            </div>
        </form>
    </div>
</div>

<table class="layui-hide" id="deviceTab" lay-filter="deviceTab"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="search">查询</button>
    </div>
</script>

<script id="barDemo" type="text/html">
    <button class="layui-btn layui-btn-xs"
            lay-mDropdown="{template: '#toolbarMenu'}">
        <span>操作</span>
        <i class="layui-icon layui-icon-triangle-d"></i>
    </button>
</script>

<script type="text/html" id="toolbarMenu">
    <a href="javascript:void(0);" lay-event="edit"
       class="layui-btn layui-btn-radius layui-btn-xs fa fa-edit ops_more f12">编&emsp;&emsp;辑</a>
    </br> <a href="javascript:void(0);" lay-event="particulars"
             class="layui-btn layui-btn-xs layui-btn-radius fa fa-edit ops_more f12">设备详情</a>
    </br><a href="javascript:void(0);" lay-event="del"
            class="layui-btn layui-btn-radius layui-btn-xs layui-btn-danger fa fa-remove ops_more f12">删&emsp;&emsp;除</a>
</script>

<script type="text/html" id="deviceLogo">
    <img src="{{d.deviceLogo}}">
</script>

<script type="text/html" id="statu">
    {{#  if(d.status == 0 ){ }}
    <span class="layui-badge ">不显示</span>
    {{#} else { }}
    <span class="layui-badge layui-bg-green">显示</span>
    {{#} }}
</script>

<script>
    layui.use(['table', 'layer','laydate','mDropdown'], function () {
        var table = layui.table;
        var layer = layui.layer;
		laydate = layui.laydate;
        dropdown = layui.mDropdown;
		laydate.render({
			elem: '#date1', //指定元素
            format:'yyyy-MM-dd 00:00:00'
		});
		laydate.render({
			elem: '#date2', //指定元素
            format:'yyyy-MM-dd 23:59:59'
		});

        var tableIns = table.render({
            elem: '#deviceTab'
            , url: '${ctx}/device/page'
            , initSort: {
                field: 'createTime'
                , type: 'desc'
            }
            , 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.count, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            }
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , 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, sort: true, type: 'numbers'}
                , {field: 'name', title: '设备名称'}
                , {field: 'deviceLogo', title: '设备图片', templet: "#deviceLogo"}
                , {field: 'venderName', title: '厂家名称'}
                , {field: 'venderModel', title: '厂家型号'}
                , {field: 'status', title: '状态', templet: "#statu"}
                , {field: 'unitPrice', title: '单价'}
                , {field: 'seq', title: '排序'}
                , {
                    field: 'createDt', title: '创建时间', sort: true,
                    templet: "<div>{{layui.util.toDateString(d.createDt, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {fixed: 'right', title: '操作',width:80, toolbar: '#barDemo'}
            ]]
            , request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            }
            , page: true
            , done: function (res) {
                dropdown.suite();
            }
        });

        //头工具栏事件
        table.on('toolbar(deviceTab)', function (obj) {
            switch (obj.event) {
                case 'add':
                    // 添加
                    toAdd();
                    break;
                case 'search':
                    // 查询
                    tableIns.reload({
                        where: { //设定异步数据接口的额外参数，任意设
                            name: $("#name").val(),
                            venderName: $("#venderName").val(),
                            venderModel: $("#venderModel").val(),
                            status: $("#status").val(),
                            startTime: $("#date1").val(),
                            endTime: $("#date2").val()
                        }
                        , page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(deviceTab)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么?', function (index) {
                    obj.del(data);
                    layer.close(index);
                    del(obj);
                });
            } else if (obj.event === 'edit') {
                toAdd(obj.data);
            } else if (obj.event === 'particulars'){
                particulars(obj.data);
            }
        });

        function particulars(obj) {
            if (obj != undefined) {
                url = "${ctx}/device/particulars/" + obj.id
            }
            var index = layer.open({
                type: 2
                , shade: 0.5
                , maxmin: true
                , content: url
                , area: ['550px', '300px']
            });
            layer.full(index);
        }


        function toAdd(obj) {
            var url = '${ctx}/device/detail/';
            if (obj != undefined) {
                url = "${ctx}/device/detail/" + obj.id
            }
            var index = layer.open({
                type: 2
                , shade: 0.5
                , maxmin: true
                , content: url
                , area: ['550px', '300px']
            });
            layer.full(index);
        }

        function del(obj) {
            $.ajax({
                url: '${ctx}/device/delete',
                method: 'post',
                data: JSON.stringify(obj.data),
                dataType: 'JSON',
                contentType: "application/json",
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.data, {
                            icon: 1
                        });
                    } else {
                        layer.msg(res.data, {
                            icon: 5
                        });
                    }
                }
            })
        }
    });
</script>
</body>
</html>