<!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">
  <link rel="stylesheet" type="text/css" href="../../layuiadmin/style/formSelects-v4.css"/>
  <style type="text/css">
        #department_employee_selecter dl{max-height: 250px;}
  </style>
</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">部门/人员：</label>
                <div class="layui-input-block" id="department_employee_selecter">
                  <select name="managers" xm-select="manager_range">
                  </select>
                </div>
              </div>
              <button class="layui-btn" id="submit_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' //主入口模块
    ,formSelects: 'formSelects-v4'
  }).use(['index', 'form','formSelects'], function(){
    var $ = layui.$
    ,admin = layui.admin
    ,element = layui.element
    ,form = layui.form
    ,formSelects = layui.formSelects;

    formSelects.render('manager_range',{
      template:function(id, item) {
        if (item.value.split('_')[0] == 'employee') {
          return "<span style='color:rgb(0,150,136);'>"+item.name+"</span>";
        }
        return item.name;
      }
    });

    var id = layui.view.getParameterByName('id');
    var editdata = parent.layui.table.editdata;
    var manages = [];
    for (var n in editdata.sub_department_ids) {
        manages.push('department_'+editdata.sub_department_ids[n]);
    }
    for (var n in editdata.sub_employee_ids) {
        manages.push('employee_'+editdata.sub_employee_ids[n]);
    }

    formSelects.value('manager_range', []);
    admin.req({
        url: '/account/employee/tree/'
        ,done: function(res){
            formSelects.data('manager_range', 'local', {
            arr: res.data,
            tree: {
                //在点击节点的时候, 如果没有子级数据, 会触发此事件
                nextClick: function(id, item, callback){
                    return false;
                    },
                }
            });
            formSelects.value('manager_range', manages);
        }
    });

    form.on('submit(component-form-element)', function(data){
        data.field.managers = formSelects.value('manager_range', 'val');
        admin.req({
        url: '/account/employee/manager/save/?id='+id
        ,data: JSON.stringify(data.field)
        ,type: 'post'
        ,done: function(res){
            parent.layui.onSubmitChild();
        }
      });
        return false;
    });

    parent.layui.submitChild = function () {
      $("#submit_btn").click();
    };

  });
  </script>
</body>
</html>