{% load i18n %}
<style>
    #id_frameDepartmentSearch{
     min-width: 178px !important;
    }
</style>
<div class="layui-row">
  <div class="layui-col-md12">
    <div class="layui-card">
      <div class="layui-card-header">
        <div class="layui-form">
          <div class="layui-form-item">
            <div class="layui-inline">
              <div class="layui-input-inline" style="width: 160px">
                <input id="id_frameDepartmentSearch" type="text" placeholder="{% trans 'departmentList_search_department' %}" name="frame-department-search" autocomplete="off"
                       class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <div class="button-3d" lay-submit="" lay-filter="departmentSearch">
                <i class="fa fa-fw fa-search"></i>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="layui-card-body">
        <table id="id_attDepartmentList" class="layui-hide" lay-filter="attDepartmentList"></table>
      </div>
    </div>
  </div>
  <input id="id_selectedDepartment" type="text" class="layui-hide"/>
</div>
<script>
  layui.use(['form', 'table', 'tablePlug'], function (){
    var table = layui.table, tablePlug = layui.tablePlug, form=layui.form;
    tablePlug.smartReload.enable(true);
    var opts = {
      elem: "#id_attDepartmentList"
      ,height: 250
      ,text: {none: gettext('none_data')}
      ,request: {
        pageName: 'page',
        limitName: 'page_size'
      }
      ,page: {
        layout: ['count', 'prev', 'page', 'next', 'limit']
        ,limit: 50
        ,limits: [20, 50, 80, 100, 200]
        ,groups: 1
      }
      ,cols: [[
          {field: 'id', type: 'checkbox'}
          ,{field: 'dept_code', title: "{% trans 'department_field_code' %}", width: 90}
          ,{field: 'dept_name', title: "{% trans 'department_filed_name' %}", width: 140}
          ,{field: 'parent_dept_name', title: "{% trans 'department_field_parentDepartment' %}", width: 140}
      ]]
      ,url: '/personnel/api/departments/'
    };
    var deptTable = table.render(opts);
    table.on('checkbox(attDepartmentList)', function(obj){
      var checkedData = table.checkStatus("id_attDepartmentList");
      if(checkedData.data.length > 0){
        $("#id_selectedDepartment").val(JSON.stringify(checkedData.data));
      }else{
        $("#id_selectedDepartment").val("");
      }
    });
    form.on('submit(departmentSearch)', function(){
      var alias = $("#id_frameDepartmentSearch").val();
      table.reload(deptTable.config.id, {where: {search: alias}})
    });
    $("#id_frameDepartmentSearch").keypress(function(event){
      if (event.keyCode == "13") {
        var alias = $(this).val();
        table.reload(deptTable.config.id, {where: {search: alias}});
      };
    });
  });
</script>
