<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>方案类型设置</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" />
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/lib/font/iconfont.css" />
    <style>
        @font-face {
            font-family: 'iconfont';
            src: url('${ctxPlugin}/lib/font/iconfont.eot');
            src: url('${ctxPlugin}/lib/font/iconfont.eot?#iefix') format('embedded-opentype'),
            url('${ctxPlugin}/lib/font/iconfont.woff2') format('woff2'),
            url('${ctxPlugin}/lib/font/iconfont.woff') format('woff'),
            url('${ctxPlugin}/lib/font/iconfont.ttf') format('truetype'),
            url('${ctxPlugin}/lib/font/iconfont.svg#iconfont') format('svg');
        }
        .iconfont {
            font-family: "iconfont" !important;
            font-size: 16px;
            font-style: normal;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        }
    </style>
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
      <div class="layui-tab layui-tab-brief">
          <ul class="layui-tab-title">
              <sfTags:pagePermission authFlag="XTSZ_XMAZFS_T" html='<a href="${ctx}/sysSettings/projectInstall/index"><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_XMAZFS_XMAZFS_ADD_B" 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_XMAZFS_XMAZFS_EDIT_B" html='<button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</button>'/>
            <sfTags:pagePermission authFlag="XTSZ_XMAZFS_XMAZFS_DELETE_B" 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 src="${ctxPlugin}/lib/font/iconfont.js"></script>
<script>
    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}/sysSettings/projectInstall/ajaxForList'
            , cols: [[
                {type: 'numbers',title:'序号'}
                , {field: 'id', type: 'checkbox'}
                // , {field: 'code', title: '产品编号', sort: false, align: 'center'}
                , {field: 'name', title: '类型名称', align: 'center'}
                , {field: 'status', title: '状态', sort: false, align: 'center', templet: function(d){
                    if(d.status == '0'){
                        return '正常';
                    }else if(d.status == '1'){
                        return '已删除';
                    }
                    }}
                , {field: 'create_time', title: '创建时间', sort: false, align: 'center'}
            ]]
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            //var count = obj.config.page.count;  开启分页后又用
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 2
                        ,title: '新增'
                        ,content: '${ctx}/sysSettings/projectInstall/toAdd'
                        ,area: ['90%', '90%']
                        ,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("请选择您要编辑的数据！",{icon:5,anim:6});
                    }else if(data.length > 1){
                        layer.msg("每次只能编辑一条数据！",{icon:5,anim:6});
                    }else if(data[0].status === '1') {
                        layer.msg("已删除的数据不能编辑！",{icon:5,anim:6});
                    }else {
                        layer.open({
                            type: 2
                            ,title: '编辑'
                            , content: '${ctx}/sysSettings/projectInstall/toAdd?id=' + data[0].id
                            ,area: ['90%', '90%']
                            ,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 == '0') {
                        layer.msg("请选择您要删除的数据！",{icon:5,anim:6});
                        return;
                    }
                    var ids = "";
                    for(var i=0;i<data.length;i++){
                        ids += data[i].id + ",";
                    }
                    layer.confirm('确认要删除选中数据吗？',{
                        btn: ['确定', '取消'],
                        success: function (layero, index) {
                            this.enterEsc = function (event) {
                                if (event.keyCode == 13 ) {
                                    //确认  执行的代码
                                    delData(ids);
                                    //关闭弹窗
                                    layer.close(index);
                                    console.log("回车确认时事件")
                                    //取消连续回车键，导致不断弹出询问框
                                    $(':focus').blur();
                                }
                            };
                            //监听键盘事件
                            $(document).on('keydown', this.enterEsc);
                            //esc键触发取消
                            this.escQuit = (ev) => {
                                let code_val = ev.which;
                                if(code_val == 0x1B){
                                    $('.layui-layer-btn1').click();
                                    return false;
                                }
                            };
                            $(document).on('keydown', this.escQuit);
                        },
                        end: function () {
                            //取消键盘监听事件
                            $(document).off('keydown', this.enterEsc);
                            $(document).off('keydown', this.escQuit);
                        },
                        yes: function () {
                            //确认执行的代码
                            delData(ids);
                        },
                        no: function () {

                        }
                    });
                    break;
            };
        });
    });

    function delData(ids){
        $.ajax({
            url: '${ctx}/sysSettings/projectInstall/delete',
            type: 'post',
            data: {
                ids: ids
            },
            success: function (data) {
                if (data.code == "200") {
                    layer.msg('操作成功', {time: 2000}, function () {
                        table.reload('test'); //数据刷新
                    });
                } else {
                    layer.msg("删除失败！"+data.errMsg);
                }
            },
            complete: function () {
                noRepeat = false;
            }
        });
    }

    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }
</script>
</body>
</html>

