{% load i18n %}
<style>
    #id_framePositionSearch{
     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_framePositionSearch" type="text" placeholder="{% trans 'positionList_search_position' %}" name="frame-position-search" autocomplete="off"
                       class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <div class="button-3d" lay-submit="" lay-filter="positionSearch">
                <i class="fa fa-fw fa-search"></i>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="layui-card-body">
        <table id="id_attPositionList" class="layui-hide" lay-filter="attPositionList"></table>
      </div>
    </div>
  </div>
  <input id="id_selectedPosition" 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_attPositionList"
      ,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: 'position_code', title: "{% trans 'position_field_code' %}", width: 90}
          ,{field: 'position_name', title: "{% trans 'position_field_name' %}", width: 140}
          ,{field: 'parent_position_name', title: "{% trans 'position_field_parentPosition' %}", width: 140}
      ]]
      ,url: '/personnel/api/positions/'
    };
    var posTable = table.render(opts);
    table.on('checkbox(attPositionList)', function(obj){
      var checkedData = table.checkStatus("id_attPositionList");
      if(checkedData.data.length > 0){
        $("#id_selectedPosition").val(JSON.stringify(checkedData.data));
      }else{
        $("#id_selectedPosition").val("");
      }
    });
    form.on('submit(positionSearch)', function(){
      var alias = $("#id_framePositionSearch").val();
      table.reload(posTable.config.id, {where: {search: alias}})
    });
    $("#id_framePositionSearch").keypress(function(event){
      if (event.keyCode == "13") {
        var alias = $(this).val();
        table.reload(posTable.config.id, {where: {search: alias}});
      };
    });
  });
</script>
