{% load i18n static %}
{% load common %}
{% autoescape off %}
  <div id="{{ model_name }}_edit_form" style="padding: 10px;height: 380px;width: 850px;">
    <form class="layui-form data-form time-interval-form" action="" method="post" lay-filter="normalTimetableEditForm">
      <input type="hidden" id="id_model_pk" data-name="pk" value="{{ object_id }}"/>
      {% csrf_token %}
      {% with adminform.form as f %}
        <div class="base-setting base-setting-large1">
          <div class="layui-form-item">
            {% layui_inline f.alias %}
          </div>
          <div class="layui-tab">
            <ul class="layui-tab-title">
              <li class="layui-this">{% trans 'timeInterval_tab_basicSetting' %}</li>
              <li>{% trans 'timeInterval_tab_breakTimeSetting' %}</li>
              <li>{% trans 'timeInterval_tab_unscheduledTimeSetting' %}</li>
              <li>{% trans 'timeInterval_tab_overtimeLevelSetting' %}</li>
              <li>{% trans 'timeInterval_tab_ruleSetting' %}</li>
              <li>{% trans 'timeInterval_tab_halfDatSetting' %}</li>
            </ul>
            <div class="layui-tab-content">
              <div class="layui-tab-item cross-setup layui-show">
                <div class="layui-form-item"  style="padding-top: 20px">
                  <div class="layui-inline">
                    <label class="layui-form-label" for="{{ f.in_time.id_for_label }}">
                      {{ f.in_time.label }}{% if f.in_time.field.required %}<span class="required">*</span>{% endif %}
                    </label>
                    <div class="time-table-input-time layui-input-inline">
                        <span aria-required="true">{{ f.in_time }}</span>
                    </div>
                    <div class="layui-form-mid"></div>
                    <div class="time-table-input-cross-day layui-input-inline" style="margin-right: 5px"></div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label" for="{{ f.out_time.id_for_label }}">
                      {{ f.out_time.label }}{% if f.out_time.field.required %}<span class="required">*</span>{% endif %}
                    </label>
                    <div class="time-table-input-time layui-input-inline">
                      {{ f.out_time }}
                    </div>
                    <div class="layui-form-mid">{% trans 'timeInterval_field_crossDay' %}</div>
                    <div class="time-table-input-cross-day layui-input-inline" style="margin-right: 5px">
                      {{ f.out_time_cross_days }}
                    </div>
                  </div>
                </div>
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label" for="{{ f.begin_in.id_for_label }}">
                      {{ f.begin_in.label }}{% if f.begin_in.field.required %}<span class="required">*</span>{% endif %}
                    </label>
                    <div class="time-table-input-time layui-input-inline">
                      {{ f.begin_in }}
                    </div>
                    <div class="layui-form-mid">{% trans 'timeInterval_field_crossDay' %}</div>
                    <div class="time-table-input-cross-day layui-input-inline" style="margin-right: 5px">
                      {{ f.begin_in_cross_days }}
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label" for="{{ f.begin_out.id_for_label }}">
                      {{ f.begin_out.label }}{% if f.begin_out.field.required %}<span class="required">*</span>{% endif %}
                    </label>
                    <div class="time-table-input-time layui-input-inline">
                      {{ f.begin_out }}
                    </div>
                    <div class="layui-form-mid">{% trans 'timeInterval_field_crossDay' %}</div>
                    <div class="time-table-input-cross-day layui-input-inline" style="margin-right: 5px">
                      {{ f.begin_out_cross_days }}
                    </div>
                  </div>
                </div>
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label" for="{{ f.end_in.id_for_label }}">
                      {{ f.end_in.label }}{% if f.end_in.field.required %}<span class="required">*</span>{% endif %}
                    </label>
                    <div class="time-table-input-time layui-input-inline">
                      {{ f.end_in }}
                    </div>
                    <div class="layui-form-mid">{% trans 'timeInterval_field_crossDay' %}</div>
                    <div class="time-table-input-cross-day layui-input-inline" style="margin-right: 5px">
                      {{ f.end_in_cross_days }}
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label" for="{{ f.end_out.id_for_label }}">
                      {{ f.end_out.label }}{% if f.end_out.field.required %}<span class="required">*</span>{% endif %}
                    </label>
                    <div class="time-table-input-time layui-input-inline">
                      {{ f.end_out }}
                    </div>
                    <div class="layui-form-mid">{% trans 'timeInterval_field_crossDay' %}</div>
                    <div class="time-table-input-cross-day layui-input-inline" style="margin-right: 5px">
                      {{ f.end_out_cross_days }}
                    </div>
                  </div>
                </div>
                <div class="layui-form-item">
                  {% layui_mid f.work_day %}
                </div>
                <div class="layui-form-item layui-hide">
                  {% layui_inline f.in_ahead_margin %}
                  {% layui_inline f.in_above_margin %}
                  {% layui_inline f.out_ahead_margin %}
                  {% layui_inline f.out_above_margin %}
                  {% layui_mid f.duration %}
                  {% layui_mid f.work_time_duration %}
                </div>
                <div class="layui-card"><div class="layui-card-body">
                  <div><span style="color: red">*</span><span>{% trans 'form_helpText_title' %}</span></div>
                  <div style="padding: 0 20px;">
                    <p>{% trans 'timeTable_helpText_tip1' %}</p>
                  </div>
                </div></div>
              </div>
              <div class="layui-tab-item non-cross-setup">
                <div id="id_break_div" style="height: 200px;">
                  <table id="idBreakTable" class="layui-table" lay-filter="breakTable" width="100%">
                  </table>
                </div>
              </div>
              <div class="layui-tab-item non-cross-setup">
                <div class="layui-form-item" style="padding-top: 20px">
                  {% layui_mid f.early_in %}
                  {% layui_mid f.min_early_in %}
                </div>
                <div class="layui-form-item">
                  {% layui_mid f.late_out %}
                  {% layui_mid f.min_late_out %}
                </div>
              </div>
              <div class="layui-tab-item non-cross-setup">
                <div class="layui-form-item" style="padding-top: 20px">
                  <span onblur="myFunction()">{% layui_mid f.overtime_lv %}</span>
                  {% layui_mid f.overtime_lv1 %}
                </div>
                <div class="layui-form-item">
                  {% layui_mid f.overtime_lv2 %}
                  {% layui_mid f.overtime_lv3 %}
                </div>
              </div>
              <div class="layui-tab-item non-cross-setup">
                <div class="layui-form-item" style="padding-top: 20px">
                  {% layui_inline f.in_required %}
                  {% layui_inline f.out_required %}
                </div>
                <div class="layui-form-item">
                  {% layui_mid f.allow_late %}
                  {% layui_mid f.allow_leave_early %}
                </div>
                <div class="layui-form-item">
                  {% layui_mid f.available_interval_type %}
                  {% layui_mid f.available_interval %}
                </div>
                <div class="layui-form-item">
                  {% layui_inline f.func_key %}
                  {% layui_inline f.day_change %}
                </div>
              </div>
              <div class="layui-tab-item non-cross-setup">
                <div class="layui-form-item" style="padding-top: 20px">
                    <div class="layui-inline field-first_half_in_time" title="">
                         <label class="layui-form-label" style="width: 122px; min-width: 185px; max-width: 122px;">
                             {% trans 'timeInterval_field_firstHalfInTime' %}
                             <span class="required">*</span>
                         </label>
                    <div class="layui-input-inline ">
                        <input type="text" name="first_half_in_time"  class="layui-input layui-time" autocomplete="off"
                               required="" lay-key="59" disabled="disabled"id="id_first_half_in_time">
                    </div>
                    </div>
                  {% layui_inline f.second_half_in_time %}
                </div>
                <div class="layui-form-item">
                  {% layui_inline f.first_half_out_time %}
                    <div class="layui-inline field-second_half_out_time" title="">
                         <label class="layui-form-label" style="width: 122px; min-width: 185px; max-width: 122px;">
                             {% trans 'timeInterval_field_secondHalfOutTime' %}
                             <span class="required">*</span>
                         </label>
                    <div class="layui-input-inline ">
                        <input type="text" name="second_half_out_time" class="layui-input layui-time" autocomplete="off"
                               required="" lay-key="59" disabled="disabled" id="id_second_half_out_time">
                    </div>
                    </div>
                </div>
              </div>
            </div>
          </div>
          <div id="idOffPeriod" class="layui-hidden">{{ f.off_period }}</div>
        </div>
      {% endwith %}
    </form>
  </div>
<script>
    $(".layui-layer-btn0").on('click', function(event){
    if ($("#id_overtime_lv").val() === '0') {
            $("#id_overtime_lv1").attr('readonly', true);
            $("#id_overtime_lv2").attr('readonly', true);
            $("#id_overtime_lv3").attr('readonly', true);
        }
    });

    function myFunction()
    {
        var ot_value = document.getElementById("id_overtime_lv").value;
        if (ot_value !== '0') {
          document.getElementById("id_overtime_lv1").readOnly  = false;
          document.getElementById("id_overtime_lv2").readOnly  = false;
          document.getElementById("id_overtime_lv3").readOnly  = false;
        }
        else {
          document.getElementById("id_overtime_lv1").readOnly  = true;
          document.getElementById("id_overtime_lv2").readOnly  = true;
          document.getElementById("id_overtime_lv3").readOnly  = true;
        }
    }
</script>
<script>
    layui.use(['element', 'form', 'laydate', 'table', 'tablePlug'], function () {
        var form = layui.form, element=layui.element, laydate = layui.laydate, table=layui.table, tablePlug = layui.tablePlug;
        tablePlug.smartReload.enable(true);

        $("#id_in_ahead_margin").attr("min", "0");
        $("#id_in_above_margin").attr("min", "0");
        $("#id_out_ahead_margin").attr("min", "0");
        $("#id_out_above_margin").attr("min", "0");
        $("#id_work_day").attr("min", "0.0");
        $("#id_work_time_duration").attr("min", "0");
        $("#id_min_early_in").attr("min", "0");
        $("#id_min_late_out").attr("min", "0");
        $("#id_overtime_lv1").attr("min", "0");
        $("#id_overtime_lv2").attr("min", "0");
        $("#id_overtime_lv3").attr("min", "0");
        $("#id_allow_late").attr("min", "0");
        $("#id_allow_leave_early").attr("min", "0");
        $("#id_available_interval").attr("min", "0");

        function set_in_out_time() {
            var in_time_value = $('#id_in_time').val();
            var out_time_value = $('#id_out_time').val();
            $('#id_first_half_in_time').val(in_time_value);
            $('#id_second_half_out_time').val(out_time_value);
        }
        set_in_out_time();

        form.render();
        element.render();
        $("form.layui-form input.layui-time:not(#id_in_time,#id_out_time)").each(function () {
          laydate.render({
              elem: this,
              type: 'time',
              format: 'HH:mm:ss'
          });
        });
        laydate.render({
          elem: '#id_in_time',
          type: 'time',
          format: 'HH:mm:ss',
          done: function (value, date) {
              console.log(value);
              $('#id_first_half_in_time').val(value);
          }
        });
        laydate.render({
          elem: '#id_out_time',
          type: 'time',
          format: 'HH:mm:ss',
          done: function (value, date) {
              console.log(value);
              $('#id_second_half_out_time').val(value);
          }
        });
        var objs = [];
        table.on('checkbox(breakTable)', function (obj) {
            if (obj.checked) {
                objs.push(obj.data.id)
            }
            else {
                index = objs.indexOf(obj.data.id);
                objs.splice(index, 1)
            }
            $("#id_off_period").val(objs.join(","));
        });
        function init_break_table() {
          var url = '/att/api/breaktimes/?page_size=100';
          $.ajax({
            url: url,
            type: 'GET',
            dataType: 'json',
            success: function (callback) {
              //var table = layui.table;
              var data = callback['data'];
              var btStr = $("#id_off_period").val();
              var newData = [];
              data.forEach(function (item/*, index*/) {
                if (btStr) {
                  var bts = btStr.split(',');
                  for (var i = 0; i < bts.length; i++) {
                    if (bts[i] === item.id.toString()) {
                      item['LAY_CHECKED'] = true;
                      objs.push(item.id)
                    }
                  }
                }
                newData.push(item);
              });
              table.render({
                elem: "#idBreakTable"
                , height: 240
                , data: data
                , page: true
                , text: {none: gettext('none_data')}
                , cols: [[
                  {type: 'checkbox'},
                  {field: 'id', title: "{% trans 'breakTime_number' %}", hide: true},
                  {field: 'alias', title: "{% trans 'breakTime_field_alias' %}"},
                  {field: 'period_start', title: "{% trans 'breakTime_field_startTime'%}"},
                  {field: 'period_end', title: "{% trans 'breakTime_field_endTime' %}", templet: function (data) {
                    if(data.cross_day > 0){
                        return data.period_end + '<sup>+'+data.cross_day + '</sup>';
                    }else{
                        return data.period_end;
                    }}},
                  {field: 'duration', title: "{% trans 'breakTime_field_duration' %}"},
                  {field: 'calc_type', title: "{% trans 'breakTime_field_calculateType' %}"}
                ]]
              });
            }
          });
        }
        function init_input(){
            if ($("#id_available_interval_type").val() === '0') {
                $("#id_available_interval").attr("disabled", "disabled");
            }
            if ($("#id_early_in").val() === '0') {
                $("#id_min_early_in").attr("disabled", "disabled");
            }
            if ($("#id_late_out").val() === '0') {
                $("#id_min_late_out").attr("disabled", "disabled");
            }
            if ($("#id_overtime_lv").val() === '0') {
                $("#id_overtime_lv1").attr("disabled", "disabled");
                $("#id_overtime_lv2").attr("disabled", "disabled");
                $("#id_overtime_lv3").attr("disabled", "disabled");
            }
        }
        init_break_table();
        init_input();

        form.on('select', function (data) {
            var elem = data.elem;
            var selectValue = data.value;
            switch (elem.id) {
                case 'id_available_interval_type':
                    if (selectValue === '0') {
                      $("#id_available_interval").attr("disabled", "disabled");
                    }
                    else{
                      $("#id_available_interval").removeAttr("disabled");
                    }
                    break;
                case 'id_early_in':
                    if (selectValue === '0') {
                      $("#id_min_early_in").attr("disabled", "disabled");
                    }
                    else{
                      $("#id_min_early_in").removeAttr("disabled");
                    }
                    break;
                case 'id_late_out':
                    if (selectValue === '0') {
                      $("#id_min_late_out").attr("disabled", "disabled");
                    }
                    else{
                      $("#id_min_late_out").removeAttr("disabled");
                    }
                    break;
                case 'id_overtime_lv':
                    if (selectValue === '0') {
                        $("#id_overtime_lv1").attr("disabled", "disabled");
                        $("#id_overtime_lv2").attr("disabled", "disabled");
                        $("#id_overtime_lv3").attr("disabled", "disabled");
                    }
                    else{
                        $("#id_overtime_lv1").removeAttr("disabled");
                        $("#id_overtime_lv2").removeAttr("disabled");
                        $("#id_overtime_lv3").removeAttr("disabled");
                    }
                    break;
                default:
                    break;
            }
        });
    })
    $(".layui-layer-btn0").click(function () {
      if (!$("input[name='in_time']").val()){
        $("#id_in_time").css({'border':'solid', 'border-color': 'red', 'border-width': '1px', 'border-radius': '2px'})
      }
    });
  </script>
{% endautoescape %}