<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'}">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-anim layui-anim-fadein">
    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="test-table-reload-btn" style="margin-bottom: 10px;">
                        <div class="layui-inline">
                            <input type="text" id="keywords" class="layui-input search-item search-item-width-100" placeholder="输入关键字">
                        </div>
                        <button type="button" id="search" class="layui-btn layui-btn-sm">
                            <i class="fa fa-lg fa-search"></i> 搜索
                        </button>
                        <button type="button" id="confirm" class="layui-btn layui-btn-normal layui-btn-sm" style="float: right;">确定</button>
                    </div>
                    <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'table', 'laydate', 'common'], function() {
        var $ = layui.$;
        var $win = $(window);
        var table = layui.table;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var common = layui.common;
        var resizeTimeout = null;
        var queryParams = top.restAjax.params(window.location.href);
        var selectType = queryParams.selectType;
        selectType = selectType ? selectType : 'radio'
        var areaCode = queryParams.areaCode;
        var cplng = queryParams.cplng ? queryParams.cplng : 111.848937;
        var cplat = queryParams.cplat ? queryParams.cplat : 40.904095;

        var oldSelectedGridList = top.dialog.dialogData.oldSelectedGridList ? top.dialog.dialogData.oldSelectedGridList : [];
        // 清空上次选择
        top.dialog.dialogData.newSelectedGridList = [];
        var newSelectedGridList = [];
        var tableData = [];

        var tableUrl = 'api/grid/listpage/area-code/{areaCode}';

        // 初始化选择列表
        function initNewSelectedGridList() {
            for(var i = 0, item; item = oldSelectedGridList[i++];) {
                newSelectedGridList.push(item);
            }
        }
        initNewSelectedGridList();

        // 初始化表格
        function initTable() {
            $.extend(table, {config: {checkName: 'checked'}});
            table.render({
                elem: '#dataTable',
                id: 'dataTable',
                url: top.restAjax.path(tableUrl, [areaCode]),
                width: admin.screen() > 1 ? '100%' : '',
                height: $win.height() - 90,
                limit: 20,
                limits: [20, 40, 60, 80, 100, 200],
                toolbar: '#headerToolBar',
                request: {
                    pageName: 'page',
                    limitName: 'rows'
                },
                cols: [
                    [
                        {type: selectType, fixed: 'left'},
                        {field:'rowNum', width:80, title: '序号', fixed: 'left', align:'center', templet: '<span>{{d.LAY_INDEX}}</span>'},
                        {field: 'gridName', width: 120, title: '网格名称', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'gridDuty', width: 120, title: '职责', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'gridSummary', width: 180, title: '描述', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'gridCode', width: 180, title: '网格编码', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'areaName', width: 180, title: '地区名称', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'areaCode', width: 180, title: '地区编码', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'gridSquare', width: 180, title: '单元网格面积', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData + ' ㎡';
                            }
                        },
                        {field: 'gmtCreate', width: 180, title: '创建时间', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'gmtModified', width: 180, title: '修改时间', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'operation', width: 90, title: '操作', align:'center', fixed: 'right',
                            templet: function(row) {
                                return '<div class="layui-btn-group">' +
                                    '<button class="layui-btn layui-btn-xs" lay-event="gridEvent">查看网格</button>' +
                                    '</div>';
                            }
                        }
                    ]
                ],
                page: true,
                parseData: function(data) {
                    for(var i = 0, item; item = data.rows[i++];) {
                        if(!newSelectedGridList) {
                            item.checked = false;
                            continue;
                        }
                        var isSelected = false;
                        for(var j = 0, jItem; jItem = newSelectedGridList[j++];) {
                            if(item.gridId === jItem.gridId) {
                                isSelected = true;
                                break;
                            }
                        }
                        if(isSelected) {
                            item.checked = true;
                        } else {
                            item.checked = false;
                        }
                    }
                    tableData = data.rows;

                    return {
                        'code': 0,
                        'msg': '',
                        'count': data.total,
                        'data': data.rows
                    };
                }
            });
        }
        // 重载表格
        function reloadTable(currentPage) {
            table.reload('dataTable', {
                url: top.restAjax.path(tableUrl, [areaCode]),
                where: {
                    keywords: $('#keywords').val(),
                    startTime: $('#startTime').val(),
                    endTime: $('#endTime').val()
                },
                page: {
                    curr: currentPage
                },
                height: $win.height() - 90,
            });
        }
        initTable();
        // 事件 - 页面变化
        $win.on('resize', function() {
            clearTimeout(resizeTimeout);
            resizeTimeout = setTimeout(function() {
                reloadTable();
            }, 500);
        });
        // 事件 - 搜索
        $(document).on('click', '#search', function() {
            reloadTable(1);
        });

        function isGridSelected(gridId) {
            for(var i = 0, item; item = newSelectedGridList[i++];) {
                if(gridId == item.gridId) {
                    return true;
                }
            }
            return false;
        }

        function addGridSelected(gridItem) {
            if(isGridSelected(gridItem.gridId)) {
                return;
            }
            newSelectedGridList.push({
                gridId: gridItem.gridId,
                gridName: gridItem.gridName,
                gridCode: gridItem.gridCode
            })
        }

        function removeGridSelected(gridItem) {
            for(var i = 0, item; item = newSelectedGridList[i++];) {
                if(gridItem.gridId == item.gridId) {
                    newSelectedGridList.splice(--i, 1);
                    i--;
                    return;
                }
            }
        }

        table.on('checkbox(dataTable)', function(obj) {
            if(obj.type === 'all') {
                if(obj.checked) {
                    // 添加全部
                    for(var i = 0, item; item = tableData[i++];) {
                        addGridSelected(item);
                    }
                } else {
                    // 删除全部
                    for(var i = 0, item; item = tableData[i++];) {
                        removeGridSelected(item);
                    }
                }
            } else {
                if(obj.checked) {
                    addGridSelected(obj.data);
                } else {
                    removeGridSelected(obj.data);
                }
            }
        });

        table.on('radio(dataTable)', function(obj) {
            newSelectedGridList.splice(0, newSelectedGridList.length);
            addGridSelected(obj.data);
        });
        $(document).on('click', '#search', function() {
            reloadTable(1);
        });
        $(document).on('click', '#confirm', function() {
            top.dialog.dialogData.newSelectedGridList = newSelectedGridList;
            top.dialog.closeBox();
        });

        table.on('tool(dataTable)', function(obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'gridEvent') {
                top.dialog.open({
                    url: top.restAjax.path('route/grid/get?gridId={gridId}&cplng={cplng}&cplat={cplat}', [data.gridId, cplng, cplat]),
                    title: '【'+ data.gridName +'】网格',
                    width: '80%',
                    height: '80%',
                    onClose: function() {}
                });
            }
        })
    });
</script>
</body>
</html>