@page
@model Waste.Web.Entry.Pages.Device.IndexModel
@{
    ViewData["Title"] = "设备管理";
}
<div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <select id="BusinessId" lay-search>
                        <option value="">请选择商户</option>
                        @foreach (var item in Model.blist)
                        {
                            <option value="@item.Id">@item.Name</option>
                        }
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="Ecode" id="Ecode" placeholder="请输入机器码" />
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="FacEcode" id="FacEcode" placeholder="请输入设备编号" />
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="Name" id="Name" placeholder="请输入设备名称" />
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" data-time="ysdatetime" name="lasthearttime" id="lasthearttime" placeholder="请选择最近上报时间" />
                </div>
            </div>
            <div class="layui-inline">
                <button class="btn btn-primary btn-lg  js-search" type="button">查询</button>
                <button class="btn btn-primary btn-lg  js-add" type="button">添加</button>
            </div>
        </div>
    </div>
    <div class="layui-card-body">
        <table class="layui-table" id="list" lay-filter="list">
        </table>
    </div>
</div>
<script type="text/html" id="optpl">
    <a href="#" class="js-edit" title="编辑" data-id="{{d.id}}">编辑</a>&nbsp;
    @*<a href="#" class="js-detail" title="详情" data-id="{{d.id}}">详情</a>&nbsp;*@
    {{#if (d.status == 1){ }}
    <a href="#" class="js-stop" title="设备停用" data-id="{{d.id}}">停用</a>&nbsp;
    {{# } else if (d.status == 0){ }}
        <a href="#" class="js-start" title="设备启用" data-id="{{d.id}}">启用</a>&nbsp;
    {{#} }}
    <a href="#" class="js-config" title="配置" data-id="{{d.id}}">配置</a>
</script>
<script type="text/html" id="lefttoolbar">
    <button class="btn btn-primary btn-lg" lay-event="js-fenpei">分配</button>
    <button class="btn btn-primary btn-lg" lay-event="reback">回收</button>
</script>
<script type="text/html" id="netstatustpl">
    {{#if (d.netstatus == 0){ }}
    <span style="color:red;">离线</span>
    {{# } else { }}
        <span style="color: #19be6b;">在线</span>
    {{#} }}
</script>
<script type="text/html" id="signtpl">
    <ul class="signlist">
        <li class="{{d.sign >0 && d.netstatus ==1?"active":""}}"></li>
        <li class="{{d.sign >1 &&d.netstatus ==1?"active":""}}"></li>
        <li class="{{d.sign >2 &&d.netstatus ==1?"active":""}}"></li>
        <li class="{{d.sign >3 &&d.netstatus ==1?"active":""}}"></li>
        <li class="{{d.sign >4 &&d.netstatus ==1?"active":""}}"></li>
    </ul>
</script>
@section Scripts
{
<script type="text/javascript">
        layui.use(['common', 'table', 'layer'], function () {
            var common = layui.common, table = layui.table, layer = layui.layer;
        common.initTable({
            url: '/api/device/getlist',
            toolbar:"#lefttoolbar"
            , method: 'post',
            ontoolbarevent: function (obj) {
                if (obj.event == "js-fenpei" || obj.event == "reback") {
                    var checkStatus = table.checkStatus(obj.config.id);
                    if (checkStatus.data.length == 0) {
                        common.alert("请选择设备");
                        return;
                    }
                    var ids = [];
                    $.each(checkStatus.data, function () {
                        ids.push(this.id);
                    });
                    switch (obj.event) {
                        case "js-fenpei":
                            common.normaldialog({
                                title: '选择服务商',
                                area: ['70%', '70%'],
                                content: '/Business/List',
                                btn: ['确认', '取消'],
                                yes: function (index, layero) {
                                    var body = layer.getChildFrame('body', index);
                                    var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                                    var values = iframeWin.getselect(table);
                                    if (values.length == 0) {
                                        common.alert("请选择商户");
                                        return;
                                    }
                                    common.uajax({
                                        url: "/api/device/batchset",
                                        type: "post",
                                        data: {
                                            businessid: values[0].id,
                                            codes: ids,
                                            type: 1
                                        },
                                        success: function (res) {
                                            if (res.succeeded) {
                                                var newres = res.data;
                                                if (newres.code === 0) {
                                                    common.info(res.message, function () {
                                                    /*$(".js-search").trigger("click");*/
                                                        layer.close(index);
                                                    });
                                                } else {
                                                    common.alert(newres.message);
                                                }
                                            } else {
                                                var msg = "状态码:" + res.statuscode + ",时间戳:" + res.timestamp + ",描述:" + res.errors;
                                                that.alert(msg);
                                            }
                                        },
                                    });
                                }, btn2: function (index, layero) {

                                }
                            });
                            break;
                        case "reback":
                            common.confirm("确定回收?", function () {
                                common.uajax({
                                    url: "/api/device/batchset",
                                    type: "post",
                                    data: {
                                        businessid: '@Model.emptyBusinessid',
                                        codes: ids,
                                        type: 2
                                    }
                                });
                            });
                            break;
                        default:
                            break;
                    }
                }
            }
            , cols: [[
                { type: "checkbox" },
                {
                    field: 'businessname', title: '所属商户'
                },
                {
                    field: 'facecode', title: '编号'
                },
                {
                    field: 'ecode', title: '机器码'
                },
                {
                    field: 'name', title: '设备名称'
                }
                ,
                {
                    field: 'todaycount', title: '今日/累计称重', templet: function (d) {
                        return d.todaycount + "/" + d.totalcount;
                    }
                }
                ,
                {
                    field: 'todayweight', title: '今日/累计重量（KG)', templet: function (d) {
                        return d.todayweight + "/" + d.totalweight;
                    }
                }
                ,
                {
                    field: 'nettype', title: '联网方式', templet: function (d) {
                        return DeviceNetType[d.nettype];
                    }, hide: true
                }
                ,
                {
                    field: 'status', title: '设备状态', templet: function (d) {
                        return DeviceStatus[d.status];
                    }
                }
                ,
                {
                    field: 'netstatus', title: '网络状态', templet:"#netstatustpl"
                }
                ,
                {
                    field: 'sign', title: '信号强度', templet: "#signtpl", hide: true
                }
                ,
                {
                    field: 'iccid', title: 'ICCID', hide: true
                }
                ,
                {
                    field: 'lasthearttime', title: '最近上报时间',sort:true
                }
                ,
                ,
                {
                    field: 'lastbeattime', title: '心跳时间'
                }
                ,
                {
                    field: 'longitude', title: '经度',hide:true
                }
                ,
                {
                    field: 'latitude', title: '纬度', hide: true
                }
                ,
                {
                    field: 'address', title: '地址'
                }
                ,
                {
                    field: 'remark', title: '备注', hide: true
                }
                ,
                {
                    title: '操作', templet: "#optpl"
                }
            ]]
        });
        $(".js-search").on("click", function () {
            var queryParam = [{
                "Name": 'FacEcode',
                "Type": QueryCond.Like,
                "Value": $("#FacEcode").val()
            }, {
                "Name": 'Ecode',
                "Type": QueryCond.Like,
                "Value": $("#Ecode").val()
            }, {
                "Name": 'Name',
                "Type": QueryCond.Like,
                "Value": $("#Name").val()
            }, {
                "Name": 'BusinessId',
                "Type": QueryCond.Equal,
                "Value": $("#BusinessId").val()
            }];
            var starttime = $("#lasthearttime").val();
        if (starttime != "") {
            var times = starttime.split('~');
            queryParam.push({
                "Name": 'lasthearttime',
                "Type": QueryCond.GreaterThanOrEqual,
                "Value": times[0].trim()
            });
            queryParam.push({
                "Name": 'lasthearttime',
                "Type": QueryCond.LessThanOrEqual,
                "Value": times[1].trim()
            });
        }
            common.reloadtable("list", {
                where: {
                    queryParam: queryParam
                }
            });
        });
        $(".js-add").on("click", function () {
            common.dialog({
                title: '添加设备',
                content: '/Device/Edit'
            });
        });
        $("body").on("click", ".js-edit", function () {
            var id = $(this).data('id');
            common.dialog({
                title: '编辑设备',
                content: '/Device/Edit?id=' + id
            });
        });
         $("body").on("click", ".js-config", function () {
            var id = $(this).data('id');
            common.dialog({
                title: '推送配置',
                content: '/Device/Config?id=' + id
            });
        });
        $("body").on("click", ".js-delete", function () {
            var id = $(this).data('id');
            common.confirm("确定删除?", function () {
                common.ajax({
                    url: "Del?id=" + id
                });
            });
        });
            $("body").on("click", ".js-detail", function () {
                var id = $(this).data('id');
                common.dialog({
                    content:"/Device/Detail?id="+id
                });
            });
            $("body").on("click", ".js-stop", function () {
                var id = $(this).data('id');
                common.confirm("确定停用?", function () {
                    common.uajax({
                        url: "/api/device/setstatus?id=" + id+"&status=0"
                    });
                });
            });
            $("body").on("click", ".js-start", function () {
                var id = $(this).data('id');
                common.confirm("确定启用?", function () {
                    common.uajax({
                        url: "/api/device/setstatus?id=" + id + "&status=1"
                    });
                });
            });
    });
</script>
}
