<!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">
  <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" pad15>
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">

            <form class="layui-form" action="" lay-filter="component-form-element">
              <div class="layui-row layui-col-space10 layui-form-item">

                <div class="layui-col-md12">
                  <label class="layui-form-label"><font color='red' size="4">*</font>名称：</label>
                  <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required" placeholder="请输入权限组名称" autocomplete="off" class="layui-input">
                  </div>
                </div>

                <div class="layui-col-md12">
                  <label class="layui-form-label">权限：</label>
                  <div class="layui-input-block">
                    <div id="perms">

                    </div>
                  </div>
                </div>

                <button id="submit_btn" class="layui-btn" lay-submit lay-filter="component-form-element" style="display: none">保存</button>

              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>


  <script src="../../layuiadmin/layui/layui.js"></script>
  <script>
  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
    var $ = layui.$
    ,admin = layui.admin
    ,element = layui.element
    ,form = layui.form;
    var id = layui.view.getParameterByName('id');

    admin.req({
      url: '/account/permission/all/',
      beforeSend: function(){
        layer.load();
      },
      done: function(res){
        var curPermissions = [];
        var editdata = null;
        if(id){
          editdata = JSON.parse(JSON.stringify(parent.layui.table.editdata));
          curPermissions = editdata.permissions;
        }

        var data = res.data;
        var html = '';
        for (var key1 in data){
          html += "<div class='layui-colla-item'>\n" +
                "<input type='checkbox' title='"+key1+"' lay-filter='clickkey1' lay-skin='primary'>\n" +
                "          <div class='layui-colla-content layui-show' style='padding-top: 3px'>\n";
          for(var key2 in data[key1]){
              html += "<input type='checkbox' title='"+key2+"' lay-filter='clickkey2' lay-skin='primary'>\n" +
                  "          <div class='layui-colla-content layui-show' style='padding-top: 3px'>\n<div>\n";
              var item = data[key1][key2];
              for(var m in item){
                  var p = item[m];
                  var checked = '';
                  if(curPermissions.indexOf(p.id) > -1){
                    checked = 'checked'
                  }
                  html += "<input type='checkbox' name='permission' value='"+p.id+"' title='"+p.name+"' "+checked+" lay-skin='primary'>";
              }
              html += "</div></div>"
          }
          html += "</div></div>"
        }

        $('#perms').after(html);
        if (editdata) {
            form.val("component-form-element", editdata);
        }
        form.render(null, 'component-form-element');
        layer.closeAll('loading');
      }
    });
    form.on('checkbox(clickkey1)', function (data) {
         $(data.elem).parent().children().find(':checkbox').prop('checked', data.elem.checked);
         form.render();
    });
    form.on('checkbox(clickkey2)', function (data) {
        $(data.elem).next().next().find(':checkbox').prop('checked', data.elem.checked);
        form.render();
    });

    form.on('submit(component-form-element)', function(data){
      //layer.msg(JSON.stringify(data.field));
      var submitData = {name:data.field.name};
      submitData.permissions = [];
      $("input:checkbox[name='permission']:checked").each(function() {
          submitData.permissions.push(parseInt($(this).val()));
      });
      admin.req({
        url: '/account/group/save/?id='+id
        ,data: JSON.stringify(submitData)
        ,type: 'post'
        ,done: function(res){
            parent.layui.onSubmitChild(res.data);
        }
      });

      return false;
    });
    parent.layui.submitChild = function () {
      $("#submit_btn").click();
    };
  });
  </script>
</body>
</html>