<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
    <meta name="decorator" content="base" />
    <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">
    <title>通知公告设置</title>
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
    <style>
    </style>
</head>
<body>
<div class="layui-fluid">
    <form class="layui-form layui-card" action="" id="searchForm" lay-filter="searchForm">

        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="TZGG_TZGG_THGG_T" html='
				<li class="layui-this">
					<a href="${ctx}/notification/userNotication">用户公告</a>
				</li>'/>
            </ul>
        </div>

        <div class="layui-card-body">
        </div>
    </form>

    <div class="layui-card">
        <div class="layui-card-body">
            <table id="dataList" class="scroll-list" lay-filter="dataList"></table>
        </div>
    </div>

</div>

<!--自定义toolbar-->
<div id="tableToolbar" class="layui-form-item layui-hide">
    <sfTags:pagePermission authFlag="YHGG_XZ_B" html='
		<button class="layui-btn layui-btn-sm layuiadmin-btn-useradmin " lay-event="add" data-type="add">
			<i class="layui-icon layui-icon-add-1"></i>新增</button>'/>
    <%--<sfTags:pagePermission authFlag="YHGG_XG_B" html='
		<button class="layui-btn layui-btn-sm layuiadmin-btn-useradmin " lay-event="mod" data-type="mod">
			<i class="layui-icon layui-icon-edit"></i>编辑</button>'/>--%>
    <sfTags:pagePermission authFlag="YHGG_SC_B" html='
		<button class="layui-btn layui-btn-sm layuiadmin-btn-useradmin " lay-event="del" data-type="del">
			<i class="layui-icon layui-icon-delete"></i>删除</button>'/>
</div>
<script type="text/html" id="rowToolbar">
    <span style="color:#D9B589;cursor:pointer;" lay-event="showDetail">{{d.title}}</span>
</script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script type="text/javascript">
    var table;
    layui.use(['table', 'form','layer'], function() {
        var $ = layui.$, form = layui.form;
        table = layui.table;

        //初始化table表格，事件处理在初始化之后
        table.render({
            elem : '#dataList',
            height : 'full-200',
            url : '${ctx}/notification/listData?objType=1', //数据接口
            page : true, //开启分页
            toolbar : "#tableToolbar",
            defaultToolbar : [],
            limits : [ 20, 50, 100 ],
            limit : 20,
            cols : [[
                {type: 'numbers', title: '序号', rowspan: 2}
                ,{field: 'id', type: 'checkbox', rowspan: 2}
                ,{field: 'title', title: '用户公告',templet: '#rowToolbar', align : 'center', rowspan: 2}
                ,{field: 'province', title: '通知群体', align : 'center', colspan: 4}
                ,{field: 'create_time', title: '创建时间', align : 'center', rowspan: 2}
            ], [
                {field: 'province', title: '地址', width: 180,templet:function(d){
                        var is_many = d.is_many;
                       if("2"==is_many){
                           return d.province+":"+d.city;
                       }else  if("1"==is_many){
                           return d.province;
                       }else{
                           return "";
                       }

                    }}
                ,{field: 'customer_name', title: '用户姓名', width: 180}
                ,{field: 'login_num_min', title: '登陆次数', width: 180,templet:function(d){
                        var min = d.login_num_min;
                        var max = d.login_num_max;
                      if(isBlank(min) && isBlank(max)){
                        return "";
                      }
                        if(isBlank(min)){return max+" 次以下"}
                        if(isBlank(max)){return min+" 次以上"}
                //    return isBlank(min) ?"":min +" ~ "+ isBlank(max)?"":max;
                        return min +" ~ "+ max;
                    }}
                ,{field: 'station_building_start', title: '建站时间', width: 180,templet:function(d){
                        var start = d.station_building_start;
                        var end = d.station_building_end;
                        if(isBlank(start) && isBlank(end)){
                            return "";
                        }
                        if(isBlank(start)){return end+" 之前"}
                        if(isBlank(end)){return start+" 之后"}
                        return start +" ~ "+ end;
                    }}
            ]]
        });
        table.on("tool(dataList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            //查看公告内容
            if (layEvent === 'showDetail') {
                layer.open({
                    type: 2,
                    title: '公告详情',
                    content: '${ctx}/msg/center/announceShowDetail?id=' + data.id,
                    area: ['51%', '75%'],
                /*    btn: ['关闭'],
                    yes: function (index, layero) {
                        layer.close(index);
                        if (!data.bid) {
                            table.reload('dataList', {
                                page: {
                                    curr: layui.$('.layui-laypage-skip').find('input').val()
                                }
                            });
                        }
                    },
                    cancel: function (index, layero) {
                        layer.close(index);
                        if (!data.bid) {
                            table.reload('dataList', {
                                page: {
                                    curr: layui.$('.layui-laypage-skip').find('input').val()
                                }
                            });
                        }
                    }*/
                });
            }
        });
        function _addHandler(win) {
            var adding = false;
            return function() {
                if (adding) {
                    return;
                }
                var fd = win.formData();
                console.log(fd)
                return
                fd.objType=1;
                adding = true;
                $.ajax({
                    type: 'post',
                    data: fd,
                    url: "${ctx}/notification/create",
                    success: function (data) {
                        if (data.code === "200") {
                            layer.msg("保存成功！", {icon: 1});
                            layer.close(addLayer);
                            table.reload('dataList');
                        } else if (data.code === '422') {
                            layer.msg("保存失败！", {icon: 2});
                        } else {
                            layer.msg("保存失败！", {icon: 2});
                        }
                    },
                    complete: function () {
                        adding = false;
                    }
                });
                return false;
            }
        }

        function _modHandler(win) {
            var moding = false;
            return function() {
                if (moding) {
                    return;
                }
                var fd = win.formData();
                moding = true;
                $.ajax({
                    type: 'post',
                    data: fd,
                    url: "${ctx}/notification/update",
                    success: function (data) {
                        if (data.code === "200") {
                            layer.msg("编辑成功！", {icon: 1});
                            layer.close(modLayer);
                            table.reload('dataList');
                        } else if (data.code === '422') {
                            layer.msg("保存失败！", {icon: 2});
                        } else {
                            layer.msg("保存失败！", {icon: 2});
                        }
                    },
                    complete: function () {
                        moding = false;
                    }
                });
                return false;
            }
        }

        var delHandler = (function _delHandler() {
            var deleting = false;
            return function(ids) {
                if (deleting) {
                    return;
                }
                $.ajax({
                    type: 'post',
                    data: {id: ids},
                    url: "${ctx}/notification/delete",
                    success: function (data) {
                        if (data.code === "200") {
                            layer.msg("删除成功！", {icon: 1});
                            table.reload('dataList');
                        } else if (data.code === '422') {
                            layer.msg("删除失败！", {icon: 2});
                        } else {
                            layer.msg("删除失败！", {icon: 2});
                        }
                    },
                    complete: function () {
                        deleting = false;
                    }
                });
                return false;
            }
        })();

        //监听行工具事件
        table.on('toolbar(dataList)', function(obj) {
            if(obj.event === 'add') {
                var addHandler;
                addLayer = layer.open({
                    type : 2,
                    title : '新增',
                    content: '${ctx}/notification/new?type=1',
                    maxmin : true,
                    area : [ '900px', '680px' ],
                 /*   btn : [ '确定', '取消' ],
                    yes : function(index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index],
                            submit = layero.find('iframe').contents().find('#btnSubmit');
                        //监听提交

                        if (!addHandler) {
                            addHandler = _addHandler(iframeWindow);
                        }
                        iframeWindow.layui.form.on('submit(btnSubmit)', addHandler);
                        submit.trigger('click');
                    },*/
                    success : function(layero) {
                        layero.find('.layui-layer-btn').css('text-align', 'center');
                        layero.find('.layui-layer-btn').find(".layui-layer-btn0").css("background-color","#009688");
                    }
                });
            } else if(obj.event === 'mod') {
                var modHandler;
                var data = table.checkStatus("dataList");
                if (data.data.length < 1) {
                    layer.msg("请勾选一条择待处理数据！", {icon: 2});
                    return;
                }
                if (data.data.length > 1) {
                    layer.msg("只能选择一条数据", {icon: 2});
                    return;
                }
                var item = data.data[0];
                modLayer = layer.open({
                    type: 2,
                    title: '编辑',
                    content: '${ctx}/notification/edit?id=' + item.id,
                    maxmin: true,
                    area: ['800px', '540px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index],
                            submit = layero.find('iframe').contents().find('#btnSubmit');
                        //监听提交
                        if (!modHandler) {
                            modHandler = _modHandler(iframeWindow);
                        }
                        iframeWindow.layui.form.on('submit(btnSubmit)', modHandler);
                        submit.trigger('click');
                    },
                    success : function(layero) {
                        layero.find('.layui-layer-btn').css('text-align', 'center');
                        layero.find('.layui-layer-btn').find(".layui-layer-btn0").css("background-color","#009688");
                    }
                });
            } else if(obj.event === 'del') {
                var delData = table.checkStatus("dataList");
                var ids = [];
                if (delData.data.length < 1) {
                    layer.msg("请勾选一条择待处理数据！", {icon: 2});
                    return;
                }
                var datas = delData.data;
                $.each(datas, function(i, d) {
                    ids.push(d.id);
                });
                layer.confirm('确定要删除所选择的公告吗？', {
                    icon:3,
                    btn: ['确定','取消'] //按钮
                }, function(){
                    delHandler(ids.join(","))
                }, function(){
                });
            }
        });
    });
    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }
</script>
</body>
</html>


