{% load i18n static common %}
<!--suppress ContinueStatementJS -->
<div style="padding: 10px;min-height: 350px;height: 480px;width: 1100px">
  <form id="attschedule_addattschedule" action="" class="layui-form action-form" lay-filter="shiftForm">
    <div class="">
      {% with form.form as f %}
        <div class="layui-row">
          <div class="layui-col-md5">
            <div class="layui-card">
              <div class="layui-card-header" style="padding: 0 0 0 3px;background-color: #f2f2f2;">
                <div class="layui-form">
                  <div class="layui-form-item">
                    <div class="layui-inline" style="margin-right: 0px;">
                      <div class="layui-input-inline" style="width: 115px">
                        <select id="employee_search" class="layui-input" lay-filter="empSearchSelect">
                          <option value="emp">{% trans 'schedule_empFrameOption_employee' %}</option>
                          <option value="dept">{% trans 'schedule_empFrameOption_department' %}</option>
                          <option value="area">{% trans 'schedule_empFrameOption_area' %}</option>
                            <option value="pos">{% trans 'schedule_empFrameOption_position' %}</option>
                        </select>
                      </div>
                    </div>
                    <div class="layui-inline layui-inline-emp" style="margin-right: 0px;">
                      <div class="layui-input-inline" style="width: 150px">
                        <input id="id_frameEmpSearch" type="text" style="min-width: 150px"
                               placeholder="{% trans 'schedule_empFrameSearch_placeholder_employee' %}"
                               name="frame-emp-search" autocomplete="off"
                               class="layui-input">
                      </div>
                      <div id="id_frameEmpContainer" class="layui-hide"></div>
                    </div>
                    <div class="layui-inline layui-inline-dept layui-hide" style="margin-right: 0px;">
                      <div class="layui-input-inline" style="width: 150px">
                        <input id="id_frameDeptSearch" type="text" style="min-width: 150px"
                               placeholder="{% trans 'schedule_empFrameSearch_placeholder_department' %}"
                               name="frame-dept-search" autocomplete="off"
                               class="layui-input" readonly>
                      </div>
                      <div id="id_frameDeptContainer" class="layui-hide"></div>
                    </div>
                      <div class="layui-inline layui-inline-area layui-hide" style="margin-right: 0px;">
                      <div class="layui-input-inline" style="width: 150px">
                        <input id="id_frameAreaSearch" type="text" style="min-width: 150px"
                               placeholder="{% trans 'schedule_empFrameSearch_placeholder_area' %}"
                               name="frame-area-search" autocomplete="off"
                               class="layui-input" readonly>
                      </div>
                      <div id="id_frameAreaContainer" class="layui-hide"></div>
                    </div>
                      <div class="layui-inline layui-inline-pos layui-hide" style="margin-right: 0px;">
                      <div class="layui-input-inline" style="width: 150px">
                          <input id="id_framePosSearch" type="text" style="min-width: 150px"
                               placeholder="{% trans 'schedule_empFrameSearch_placeholder_position' %}"
                               name="frame-pos-search" autocomplete="off"
                               class="layui-input" readonly>
                      </div>
                      <div id="id_framePosContainer" class="layui-hide"></div>
                    </div>
                    <div class="layui-inline layui-inline-dept layui-hide"
                         title="{% trans 'schedule_empFrameSearch_placeholder_allEmployee' %}" style="margin-right: 0px;">
                      <input id="id_frameAllEmp" type="checkbox" name="dept_emp" lay-skin="primary" lay-filter="allEmp">
                    </div>
                     <div class="layui-inline layui-inline-area layui-hide"
                         title="{% trans 'schedule_empFrameSearch_placeholder_allEmployee' %}" style="margin-right: 0px;">
                      <input id="id_frameAllEmp1" type="checkbox" name="area_emp" lay-skin="primary" lay-filter="allEmp">
                    </div>
                       <div class="layui-inline layui-inline-pos layui-hide"
                         title="{% trans 'schedule_empFrameSearch_placeholder_allEmployee' %}" style="margin-right: 0px;">
                      <input id="id_frameAllEmp2" type="checkbox" name="pos_emp" lay-skin="primary" lay-filter="allEmp">
                    </div>
                    <div class="layui-inline" style="margin-right: 0px;">
                      <div class="button-3d" lay-submit="" lay-filter="employeeSearch"
                           style="background-color: #f2f2f2;">
                        <i class="fa fa-fw fa-search"></i>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div id="id_empFrame" class="layui-card-body">
                <table id="schedule_employee" class="layui-hide" lay-filter="scheduleEmployee"></table>
              </div>
            </div>
          </div>
          <div class="layui-col-md7" style="padding-left: 5px;border-left: 1px solid rgba(0, 0, 0, 0)">
            <div class="layui-row">
              <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body schedule-add-form">
                        <input type="hidden" id="id_model_pk" name="pk" value="{{ object_id }}"/>
                        <input type="hidden" id="id_days" name="days" class="layui-hide">
                        {% csrf_token %}
                        <div class="layui-form-item">
                          {% layui_inline form.form.start_date %}
                          {% layui_inline form.form.end_date %}
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <input id="id_schedule_cover" type="checkbox" name="schedule_cover" lay-skin="primary"
                                   lay-filter="scheduleCover" title="{% trans 'schedule_action_scheduleCover' %}">
                            </div>
                        </div>
                    </div>
                </div>
              </div>
              <div class="layui-col-md12">
                <div class="layui-card">
                  <div class="layui-card-header" style="padding: 0;">
                    <div class="layui-form">
                      <div class="layui-form-item">
                        <div class="layui-inline">
                          <div class="layui-input-inline" style="width: 160px">
                            <input id="id_frameShiftSearch" type="text" placeholder="{% trans 'shiftList_search_shift' %}" name="frame-shift-search" autocomplete="off"
                                   class="layui-input">
                          </div>
                        </div>
                        <div class="layui-inline">
                          <div lay-submit="" lay-filter="ShiftSearch" style="padding: 0 0 0 15px;">
                            <i class="fa fa-fw fa-search"></i>
                          </div>
                        </div>
                        <!--TODO implement add shift
                        <div class="layui-inline">
                          <div lay-submit="" lay-filter="ShiftAdd">
                            <i class="fa fa-fw fa-plus"></i>
                          </div>
                        </div>
                        -->
                        <div class="layui-inline">
                          <div lay-submit="" lay-filter="ShiftRefresh">
                            <i class="fa fa-fw fa-refresh"></i>
                          </div>
                        </div>
                      </div>
                      <div class="layui-hide">{% layui_inline form.form.shift %}</div>
                    </div>
                  </div>
                  <div class="layui-card-body">
                    <table id="schedule_shift" class="layui-hide" lay-filter="scheduleShift"></table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      {% endwith %}
    </div>
  </form>
</div>
<!--suppress JSUnfilteredForInLoop -->
<script>
  $(function () {
      var gloable_pm_codes = new Array();
    var table_data = new Array();

    layui.use(['form', 'laydate', 'table', 'tablePlug'], function () {
      var table = layui.table, tablePlug = layui.tablePlug, form = layui.form, laydate = layui.laydate;
      tablePlug.smartReload.enable(true);
      var empOpts = {
        elem: "#schedule_employee"
        , height: 395
        , 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: 'emp_code', title: "{% trans 'emp_field_employeeCode' %}"}
          , {field: 'first_name', title: "{% trans 'emp_field_firstName' %}"}
        ]]
        , url: '/personnel/api/employee/'
        , done: function (res, curr, count) {
          //this.where = {};
            table_data = res.data;
            for(var i=0;i<res.data.length;i++){
                for(var j=0;j<gloable_pm_codes.length;j++){
                    if(res.data[i].id===gloable_pm_codes[j]){
                        res.data[i]["LAY_CHECKED"]='true';
                        var index= res.data[i]['LAY_TABLE_INDEX'];
                        $('#id_empFrame .layui-table tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
                        $('#id_empFrame .layui-table tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
                    }
                }
            }
            var checkStatus = table.checkStatus('scheduleEmployee');
            if(checkStatus.isAll){
                $('#id_empFrame .layui-table-header th[data-field="0"] input[type="checkbox"]').prop('checked', true);
                $('#id_empFrame .layui-table-header th[data-field="0"] input[type="checkbox"]').next().addClass('layui-form-checked');
            }
        }
      };
      var selected_employees = [];
      var empTable = table.render(empOpts);
      table.on('checkbox(scheduleEmployee)', function (obj) {
        $("#id_frameEmpContainer").empty();
        //var checkedData = table.checkStatus("schedule_employee");
        //if (checkedData.data.length > 0) {
        //  checkedData.data.forEach(function (item, index) {
        //    $("#id_frameEmpContainer").append('<input name="schd_employee" value="' + item.id + '"/>');
        //  });
        //}
        if(obj.checked===true){
           if(obj.type==='one'){
               if (!selected_employees.includes(obj.data.id))
                    selected_employees.push(obj.data.id);
          }else{
               for(var i=0;i<table_data.length;i++){
                   if (!selected_employees.includes(table_data[i].id))
                        selected_employees.push(table_data[i].id);
               }
           }
        }else{
           if(obj.type==='one'){
               for(var i=0;i<selected_employees.length;i++){
                  if(selected_employees[i]===obj.data.id){
                       selected_employees.remove(i);
                   }
              }
           }else{
               for(var j=0;j<table_data.length;j++){
                    for(var i=0;i<selected_employees.length;i++){
                        if(selected_employees[i]===table_data[j].id){
                            selected_employees.remove(i);
                            break;
                        }
                    }
                }
           }
       }
       gloable_pm_codes = selected_employees;
       gloable_pm_codes.forEach(function (item) {
           $("#id_frameEmpContainer").append('<input name="schd_employee" value="' + item + '"/>');
        });
      });

      Array.prototype.remove=function(dx)
      {
        if(isNaN(dx)||dx>this.length){return false;}
        for(var i=0,n=0;i<this.length;i++)
        {
            if(this[i]!=this[dx])
            {
                this[n++]=this[i]
            }
        }
        this.length-=1
      };
      var shiftOpts = {
        elem: "#schedule_shift"
        , height: 310
        , 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: 'radio'}
          , {field: 'alias', title: "{% trans 'attShift_field_alias' %}"}
          , {field: 'shift_timetable', title: "{% trans 'timeInterval_field_alias' %}"}
          , {field: 'cycle_unit', title: "{% trans 'attShift_field_cycleUnit' %}"}
          , {field: 'shift_cycle', title: "{% trans 'attShift_field_cycle' %}"}
          , {field: 'auto_shift', title: "{% trans 'attShift_field_autoShift' %}"}
        ]]
        , url: '/att/api/attshifts/'
      };
      var shiftTable = table.render(shiftOpts);
      form.render();
      var cur = moment(new Date());
      var startDate = laydate.render({
        elem: '#id_start_date'
        , type: 'date'
        , value: cur.startOf('month').format('YYYY-MM-DD')
      });
      var endDate = laydate.render({
        elem: '#id_end_date'
        , type: 'date'
        , value: cur.endOf('month').format('YYYY-MM-DD')
      });
      form.on('select(empSearchSelect)', function (data) {
        switch (data.value) {
          case 'emp':
            $(".layui-inline-emp").removeClass('layui-hide');
            $(".layui-inline-dept").addClass('layui-hide');
            $(".layui-inline-area").addClass('layui-hide');
            $(".layui-inline-pos").addClass('layui-hide');
            $("#id_frameDeptSearch").val("");
            $("#id_frameAreaSearch").val("");
            $("#id_framePosSearch").val("");
            $("#id_frameDeptContainer").empty();
            $("#id_frameAreaContainer").empty();
            $("#id_framePosContainer").empty();
            table.reload(empTable.config.id, {where: {departments: ""}});
            table.reload(empTable.config.id, {where: {areas: ""}});
            table.reload(empTable.config.id, {where: {positions: ""}});
            gloable_pm_codes = [];
            break;
          case 'dept':
            $(".layui-inline-dept").removeClass('layui-hide');
            $(".layui-inline-emp").addClass('layui-hide');
            $(".layui-inline-pos").addClass('layui-hide');
            $(".layui-inline-area").addClass('layui-hide');
            $("#id_frameEmpContainer").empty();
            table.reload(empTable.config.id, {where: {emp_code_icontains: ""}});
            gloable_pm_codes = [];
            break;
          case 'area':
            $(".layui-inline-area").removeClass('layui-hide');
            $(".layui-inline-emp").addClass('layui-hide');
            $(".layui-inline-dept").addClass('layui-hide');
            $(".layui-inline-pos").addClass('layui-hide');
            $("#id_frameEmpContainer").empty();
            table.reload(empTable.config.id, {where: {emp_code_icontains: ""}});
            gloable_pm_codes = [];
            break;
          case 'pos':
            $(".layui-inline-pos").removeClass('layui-hide');
            $(".layui-inline-emp").addClass('layui-hide');
            $(".layui-inline-area").addClass('layui-hide');
            $(".layui-inline-dept").addClass('layui-hide');
            $("#id_frameEmpContainer").empty();
            table.reload(empTable.config.id, {where: {emp_code_icontains: ""}});
            gloable_pm_codes = [];
            break;
        }
      });
      table.on('radio(scheduleShift)', function (obj) {
        $("#id_shift").val(obj.data.id);
      });
      form.on('submit(employeeSearch)', function () {
        var v = $("#employee_search").val();
        switch (v) {
          case 'emp':
            var emp = $("#id_frameEmpSearch").val();
            table.reload(empTable.config.id, {where: {search: emp}, page: {curr: 1}});
            gloable_pm_codes = [];
            break;
          case 'dept':
            break;
          case 'area':
            break;
          case 'pos':
            break;
          default :
            break;
        }
      });
      $("#id_frameEmpSearch").keypress(function (event) {
        if (event.keyCode == "13") {
          var emp = $(this).val();
          table.reload(empTable.config.id, {where: {search: emp}, page: {curr: 1}});
          gloable_pm_codes = [];
        }
      });
      form.on('submit(ShiftSearch)', function () {
        var alias = $("#id_frameShiftSearch").val();
        table.reload(shiftTable.config.id, {where: {alias__icontains: alias}});
      });
      form.on('submit(ShiftRefresh)', function () {
        $("#id_frameShiftSearch").val("");
        table.reload(shiftTable.config.id, {where: {alias__icontains: ""}});
      });
      $("#id_frameShiftSearch").keypress(function (event) {
        if (event.keyCode == "13") {
          var alias = $(this).val();
          table.reload(shiftTable.config.id, {where: {alias__icontains: alias}});
        }
      });
      $("#id_frameDeptSearch").click(function () {
        $.ajax({
          url: "/personnel/getDepartmentList/"
          , type: "GET"
          , dataType: "html"
          , success: function (response) {
            layer.open({
              type: 1
              , title: ' '
              , closeBtn: 1
              , area: ['450px', '400px']
              , maxmin: false
              , shadeClose: false
              , content: response
              , btn: [gettext('btn_confirm'), gettext('btn_cancel')]
              , yes: function (index, layero) {
                var selectedDepts = $(layero).find("#id_selectedDepartment").val();
                $("#id_frameDeptContainer").empty();
                if (selectedDepts) {
                  var depts = JSON.parse(selectedDepts);
                  var alias = [];
                  var ids = [];
                  depts.forEach(function (item, index) {
                    alias.push(item.dept_name);
                    ids.push(item.id);
                    $("#id_frameDeptContainer").append('<input name="department" value="' + item.id + '"/>');
                  });
                  $("#id_frameDeptSearch").val(alias.join(','));
                  table.reload(empTable.config.id, {where: {departments: ids.join(',')}});
                  gloable_pm_codes = [];
                } else {
                  $("#id_frameDeptSearch").val("");
                }
                layer.close(index);
              }
            });
          }
        });
       form.on('checkbox(allEmp)', function (data) {
         if (data.elem.checked) {
                var selected_drop = $("#employee_search").val();
             if (selected_drop == "dept"){
                var depts = $("#id_frameDeptContainer").find("input[name='department']");
                var text = 'Department Not Found'
             }
             if (selected_drop == "area"){
                var depts = $("#id_frameAreaContainer").find("input[name='area']");
                var text = 'Area Not Found'
             }
             if (selected_drop == "pos"){
                var depts = $("#id_framePosContainer").find("input[name='position']");
                var text = 'Position Not Found'
             }
           if (depts.length == 0) {
             form.val('shiftForm', {is_department: false});
             layer.alert(gettext(text),
                {title: gettext('layer_title_error'), icon: 2, time: 3000, btn: []});
           } else {
             $("#id_empFrame").addClass('layui-admin-shade ');
           }
         } else {
           $("#id_empFrame").removeClass('layui-admin-shade ');
         }
       });
      });
      $("#id_frameAreaSearch").click(function () {
        $.ajax({
          url: "/personnel/getAreaList/"
          , type: "GET"
          , dataType: "html"
          , success: function (response) {
            layer.open({
              type: 1
              , title: ' '
              , closeBtn: 1
              , area: ['450px', '400px']
              , maxmin: false
              , shadeClose: false
              , content: response
              , btn: [gettext('btn_confirm'), gettext('btn_cancel')]
              , yes: function (index, layero) {
                var selectedAreas = $(layero).find("#id_selectedArea").val();
                $("#id_frameAreaContainer").empty();
                if (selectedAreas) {
                  var Areas = JSON.parse(selectedAreas);
                  var alias = [];
                  var ids = [];
                  Areas.forEach(function (item, index) {
                    alias.push(item.area_name);
                    ids.push(item.id);
                    $("#id_frameAreaContainer").append('<input name="area" value="' + item.id + '"/>');
                  });
                  $("#id_frameAreaSearch").val(alias.join(','));
                  table.reload(empTable.config.id, {where: {areas: ids.join(',')}});
                  gloable_pm_codes = [];
                } else {
                  $("#id_frameAreaSearch").val("");
                }
                layer.close(index);
              }
            });
          }
        });
        form.on('checkbox(allEmp)', function (data) {
            if (data.elem.checked) {
                var selected_drop = $("#employee_search").val();
             if (selected_drop == "area"){
                var areas = $("#id_frameAreaContainer").find("input[name='area']");
                var text = 'Area Not Found'
             }
             if (selected_drop == "dept"){
                var areas = $("#id_frameDeptContainer").find("input[name='department']");
                var text = 'Department Not Found'
             }
             if (selected_drop == "pos"){
                var areas = $("#id_framePosContainer").find("input[name='position']");
                var text = 'Position Not Found'
             }
              if (areas.length == 0) {
                form.val('shiftForm', {is_area: false});
                layer.alert(gettext(text),
                    {title: gettext('layer_title_error'), icon: 2, time: 3000, btn: []});
              } else {
                $("#id_empFrame").addClass('layui-admin-shade ');
              }
            } else {
              $("#id_empFrame").removeClass('layui-admin-shade ');
            }
        });
      });
      $("#id_framePosSearch").click(function () {
        $.ajax({
          url: "/personnel/getPositionList/"
          , type: "GET"
          , dataType: "html"
          , success: function (response) {
            layer.open({
              type: 1
              , title: ' '
              , closeBtn: 1
              , area: ['450px', '400px']
              , maxmin: false
              , shadeClose: false
              , content: response
              , btn: [gettext('btn_confirm'), gettext('btn_cancel')]
              , yes: function (index, layero) {
                var selectedPositions = $(layero).find("#id_selectedPosition").val();
                $("#id_framePosContainer").empty();
                if (selectedPositions) {
                  var Positions = JSON.parse(selectedPositions);
                  var alias = [];
                  var ids = [];
                  Positions.forEach(function (item, index) {
                    alias.push(item.position_name);
                    ids.push(item.id);
                    $("#id_framePosContainer").append('<input name="position" value="' + item.id + '"/>');
                  });
                  $("#id_framePosSearch").val(alias.join(','));
                  table.reload(empTable.config.id, {where: {positions: ids.join(',')}});
                  gloable_pm_codes = [];
                } else {
                  $("#id_framePosSearch").val("");
                }
                layer.close(index);
              }
            });
          }
        });
        form.on('checkbox(allEmp)', function (data) {
            if (data.elem.checked) {
                   var selected_drop = $("#employee_search").val();
             if (selected_drop == "pos"){
                var positions = $("#id_framePosContainer").find("input[name='position']");
                var text = 'Position Not Found'
             }
             if (selected_drop == "dept"){
                var positions = $("#id_frameDeptContainer").find("input[name='department']");
                var text = 'Department Not Found'
             }
             if (selected_drop == "area"){
                var positions = $("#id_frameAreaContainer").find("input[name='area']");
                var text = 'Area Not Found'
             }
              if (positions.length === 0) {
                form.val('shiftForm', {is_position: false});
                layer.alert(gettext(text),
                    {title: gettext('layer_title_error'), icon: 2, time: 3000, btn: []});
              } else {
                $("#id_empFrame").addClass('layui-admin-shade ');
              }
            } else {
              $("#id_empFrame").removeClass('layui-admin-shade ');
            }
        });
      });
    });
  });
</script>
