<!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>
<style type="text/css">
    .LAY-btns .layui-nav {padding-left:0;padding-right:10px;top:-4px;border: 0;background-color: #009688;}
    .LAY-btns .layui-nav .layui-nav-item{line-height: 30px;}
    .LAY-btns .layui-nav .layui-nav-child{top:34px;}
    .LAY-btns .layui-nav .layui-nav-bar{display: none;}
    .LAY-btns .layui-nav .layui-nav-child dd.layui-this a{color:#333;background-color:#fff;}
    .LAY-btns .layui-nav .layui-nav-child dd.layui-this a:hover {background-color: #f2f2f2;color: #000;}
  </style>
<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">

            <div class="LAY-btns" style="margin-bottom: 10px;">
              <button class="layui-nav" data-permission="account.add_department">
                <div class="layui-nav-item">
                  <a href="javascript:;" style="color:#fff;" >添加</a>
                  <dl class="layui-nav-child"> <!-- 二级菜单 -->
                    <dd><a href="#" id="btn_add">公司</a></dd>
                    <dd><a href="#" id="btn_add_child">部门</a></dd>
                  </dl>
                </div>
              </button>
            </div>

            <table  id="datagrid" lay-filter="datagrid-operate"></table>
            <script type="text/html" id="datagrid-operate-bar">
              <div class="layui-btn-group">
                <a class="layui-btn layui-btn-xs" lay-event="edit" data-permission="account.add_department">修改</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" data-permission="account.delete_department">删除</a>
              </div>
            </script>
          </div>
        </div>
        </div>
    </div>
  </div>

  <script src="../../layuiadmin/layui/layui.js?t=1"></script>
  <script>
  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table', 'treetable', 'utils'], function(){
    var $ = layui.$;
    var layer  = layui.layer ;
    var table  = layui.table ;
    var utils  = layui.utils ;
    var treetable   = layui.treetable  ;

    var renderTable = function () {
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: "0",
            treeIdName: 'id',
            treePidName: 'parent_id',
            treeDefaultClose: false,
            treeLinkage: false,
            elem: '#datagrid',
            url: '/account/department/data/',
            page: false,
            cols: [[
                {type:'checkbox',width:50},
                {field: 'name', title: '名称',width:170},
                {field: 'product_range_text', title: '产品范围',width:170},
                {field: 'notes', title: '备注', minWidth:150},
                {width:100,align:'left', fixed: 'right',templet: '#datagrid-operate-bar'}
            ]],
            done: function () {
                layui.index.removeNoPermButtons();
                layer.closeAll('loading');
            }
        });
    };

    renderTable();

    //监听工具条
    table.on('tool(datagrid-operate)', function(obj){
      var data = obj.data;
        if(obj.event === 'edit'){
          table.editdata = data;

          layer.open({
            type: 2,
            title: '修改',
            shadeClose: false,
            area: ['40%', '60%'],
            btn: ['保存', '取消'],
            yes: function(index, dom){
               layui.onSubmitChild = function (data) {
              layer.close(index);
              renderTable();
            };
            layui.submitChild();
            },
            btn2: function(index, layero){
              layer.close(index);//关闭当前按钮
            },
            content: 'department_edit.html?id='+data.id
          });
        }else if(obj.event === 'del'){
          layer.confirm('确定要删除吗？', function(index){
              layer.close(index);
            layui.admin.req({
              url: '/account/department/delete/?id='+data.id
              ,done: function(res){
                renderTable();
              }
            });
          });
        }
    });

    $('#btn_add').on('click', function(){
        layer.open({
          type: 2,
          title: '添加',
          shadeClose: false,
          area: ['40%', '60%'],
          btn: ['保存', '取消'],
          yes: function(index, dom){
             layui.onSubmitChild = function (data) {
              layer.close(index);
              renderTable();
            };
            layui.submitChild();
          },
          btn2: function(index, layero){
            layer.close(index);//关闭当前按钮
          },
          content: 'department_edit.html'
        });
    });

    $('#btn_add_child').on('click', function(){
        var data = utils.getSelectedRow('datagrid');
         if(data == false){
             return;
         }
         var parent_id = data.id;

        layer.open({
          type: 2,
          title: '添加',
          shadeClose: false,
          area: ['40%', '60%'],
          btn: ['保存', '取消'],
          yes: function(index, dom){
              layui.onSubmitChild = function (data) {
              layer.close(index);
              renderTable();
            };
            layui.submitChild();
          },
          btn2: function(index, layero){
            layer.close(index);//关闭当前按钮
          },
          content: 'department_edit.html?parent_id='+parent_id
        });
    });
  });
  </script>
</body>
</html>

