<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 后台管理员</title>
    <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">
    <meta name="decorator" content="base" />
    <style>
        .layui-table-cell {
            height: inherit;
        }

    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <%-- <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="height:30px;">
             <div class="layui-form-item">
                 <a href="${ctx}/systemSetup" style="cursor:pointer;">
                     <i class="layui-icon layui-icon-return layuiadmin-button-btn"></i><span style="margin-left:2px;">返回</span>
                 </a>
             </div>
         </div>--%>
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="XTSZ_FWXT_FWXT_T" html='<a href="${ctx}/serviceMeasure/operationServiceMeasure"><li class="layui-this">服务措施设置</li></a>'/>
            </ul>
        </div>
        <div class="layui-card-body">
            <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <sfTags:pagePermission authFlag="XTSZ_FWCS_XZ_T" html='<button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>'/>
                    <sfTags:pagePermission authFlag="XTSZ_FWCS_XG_T" html='<button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</button>'/>
                    <sfTags:pagePermission authFlag="XTSZ_FWCS_SC_T" html='<button class="layui-btn layui-btn-sm" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</button>'/>
                </div>
            </script>
        </div>
    </div>
</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script>
    var reg = /^\d{1,6}$/;
    var reg1 = /^.{1,10}$/;
    layui.use(['table','layer','laypage'], function(){
        $ = layui.$;
        table = layui.table;
        layer = layui.layer;
        laypage = layui.laypage;

        table.render({
            elem: '#test',
            height: 'full-80'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            , url: '${ctx}/serviceMeasure/getServiceData'
            ,request: {
                pageName: 'pageNo' //页码的参数名称，默认：page
                ,limitName: 'PageSize' //每页数据量的参数名，默认：limit
            }
            , cols: [[
                {filed:'number', type: 'numbers',title:'序号',align: 'center'}
                , {field: 'id', type: 'checkbox',align: 'center'}
                , {field: 'service_type', title: '服务类型', sort: false, align: 'center'}
                ,{field: 'service_measures', title: '服务措施', sort: false, align: 'center'
                  ,  templet : function(d) {
                        var detail = d.detail;
                        var html = "";
                        for (var i = 0; i < detail.length; i++) {
                            var tp = detail[i].service_measures;
                            if (i == 0) {
                                html += tp;
                            } else {
                                html += '<hr class="layui-bg-gray" style="margin-left:-20px;margin-right:-20px;">' + tp
                            }
                        }
                        return html;
                    }
            }
            ]],
            jump: function (obj) {

            },
            done : function(res) {

            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 2
                        ,title: '新增'
                        ,content: '${ctx}/serviceMeasure/addMeasure'
                        ,area: ['480px', '370px']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'LAY-user-front-submit'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);

                            //监听提交
                            var status = "ok";
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                var jsonArry = [];
                                var arr = [];
                                layero.find('iframe').contents().find(".contentBody").each(function (index) {
                                    var service_type = $(this).find("select[name='service_type']").val();
                                    var service_measures = $(this).find("input[name='service_measures']").val();
                                    arr.push(service_measures );
                                    if(isRepeat(arr)){
                                        layer.msg("服务措施名称重复", {icon: 5, anim: 6});
                                        status="no";
                                    }
                                    if (!isBlank(service_type) && !isBlank(service_measures)) {
                                        var info = {
                                            service_type: service_type,
                                            service_measures: service_measures
                                        };
                                    }else{
                                        var info = {
                                            service_measures: service_measures
                                        };
                                    }
                                    jsonArry.push(info);
                                });
                                if ("ok" == status) {
                                    $.ajax({
                                        type: 'post',
                                        data:  {jsonData: JSON.stringify(jsonArry)},
                                        url: "${ctx}/serviceMeasure/saveMeasure",
                                        success: function (data) {
                                            if (data =='200') {
                                                layer.msg("添加成功！", {icon: 1});
                                                table.reload('test'); //数据刷新
                                                layer.close(index); //关闭弹层
                                            } else if (data =='205') {
                                                layer.msg("保存失败,服务措施已存在", {icon: 2});
                                            }else {
                                                layer.msg("保存失败！", {icon: 2});
                                            }
                                            saveMark = false;
                                            return;
                                        },
                                        error: function (e) {
                                            layer.msg("请刷新！", {icon: 2});
                                            saveMark = false;
                                            return;
                                        }
                                    })
                                }
                            });
                            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");
                        }
                    });
                    break;
                case 'edit':
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg("请选择您要编辑的数据！");
                    }else if(data.length > 1){
                        layer.msg("一次只能编辑一条数据！");
                    }else{
                        layer.open({
                            type: 2
                            ,title: '编辑'
                            , content: '${ctx}/serviceMeasure/editMeasure?service_type='+data[0].service_type
                            ,area: ['600px', '360px']
                            ,btn: ['确定', '取消']
                            ,yes: function(index, layero){
                                var iframeWindow = window['layui-layer-iframe'+ index]
                                    ,submitID = 'LAY-user-front-submit_edit'
                                    ,submit = layero.find('iframe').contents().find('#'+ submitID);

                                //监听提交
                                var status = "ok";
                                iframeWindow.layui.form.on('submit(' + submitID + ')', function(data) {
                                     var service_type= layero.find('iframe').contents().find("#service_type").val();
                                    var jsonArry = [];
                                    var arr = [];
                                    layero.find('iframe').contents().find("#detailSet").find("tr").each(function(){
                                        var service_measures = $(this).find("input[name='service_measures']").val();
                                       var id = $(this).find("input[name='id']").val();
                                        arr.push(service_measures );
                                        if(isRepeat(arr)){
                                            layer.msg("服务措施名称重复", {icon: 5, anim: 6});
                                            status="no";
                                        }
                                        if (!isBlank(service_measures) && !isBlank(id)) {
                                            var info = {
                                                id: id,
                                                service_measures: service_measures
                                            };
                                        }else{
                                            var info = {
                                                service_measures: service_measures
                                            };
                                        }
                                        jsonArry.push(info);
                                    });
                                    if ("ok" == status) {
                                    $.ajax({
                                        type : 'post',
                                        data :{jsonData: JSON.stringify(jsonArry),service_type:service_type},
                                        url : "${ctx}/serviceMeasure/saveEditMeasure",
                                        success : function(data) {
                                            if (data== '200') {
                                                layer.msg("编辑成功！", {
                                                    icon : 1
                                                });
                                                table.reload('test'); //数据刷新
                                                layer.close(index); //关闭弹层
                                            }else {
                                                layer.msg("编辑失败", {icon: 2});
                                            }
                                            saveMark = false;
                                            return;
                                        },
                                        error : function(e) {
                                            layer.msg("保存失败！", {
                                                icon : 2
                                            });
                                            saveMark = false;
                                            return;
                                        }
                                    })
                                }
                                });
                                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");
                            }
                        });
                    }
                    break;
                case 'del':
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg("请选择您要删除的数据！");
                    }else {
                        var service_measures="";
                        for (var i = 0; i < data.length; i++) {
                            var detail = data[i].detail;
                            for (var i = 0; i < detail.length; i++) {
                                service_measures += detail[i].service_measures + ",";
                            }
                        }
                        layer.confirm('确认要删除选中数据吗？', {
                            icon:3,
                            btn: ['确定', '取消']//按钮
                        }, function (index) {
                            delData(service_measures ,index);
                        });
                    }
                    break;
            };
        });
    });

    var noRepeat = false;
    function editMark(data, index,id) {
        if (noRepeat) {
            return;
        }
        noRepeat = true;
        $.ajax({
            url: '',
            type: 'post',
            data: {
                id: id,
                name: data.mallName,
                sort: data.sort
            },
            success: function (data) {
                if (data.code=="200") {
                    layer.msg("编辑成功！",{icon:1});
                    table.reload('test'); //数据刷新
                    layer.close(index); //关闭弹层
                } else if(data.code=="204"){
                    layer.msg("编辑失败！",{icon:2});
                }else if(data.code=="205"){
                    layer.msg("购买渠道已存在！",{icon:2});
                }else{
                    layer.msg("请刷新！");
                }
            },
            complete: function () {
                noRepeat = false;
            }
        });
    }

    function delData(service_measures,index){
        $.ajax({
            url: '${ctx}/serviceMeasure/deleMeasure',
            type: 'post',
            data: {
                service_measures: service_measures
            },
            success: function (data) {
                if (data=="200") {
                    layer.msg("删除成功！",{icon:1});
                    table.reload('test'); //数据刷新
                    layer.close(index); //关闭弹层
                } else if(data=="402"){
                    layer.msg("删除失败,有结算方案使用的服务措施！",{icon:2});
                }else{
                    layer.msg("删除失败！");
                }
            },
            complete: function () {
                noRepeat = false;
            }
        });
    }

    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }
    function isRepeat(arr){

        var hash = {};

        for(var i in arr) {

            if(hash[arr[i]]) //hash 哈希

                return true;

            hash[arr[i]] = true;

        }

        return false;

    }
</script>
</body>
</html>

