<?php $this->view("includes/Inc_pagehead");?>
<!-- Page Styles Goes Here-->

<?php $this->view("includes/Inc_navigation");?>
<div class="m-grid__item m-grid__item--fluid m-wrapper">
  <!-- BEGIN: Subheader -->
  <div class="m-subheader ">
    <div class="d-flex align-items-center">
      <div class="mr-auto">
        <h3 class="m-subheader__title m-subheader__title--separator">
          Daily Attendance Reports
        </h3>
        <ul class="m-subheader__breadcrumbs m-nav m-nav--inline">
          <li class="m-nav__item m-nav__item--home">
            <a href="#" class="m-nav__link m-nav__link--icon">
              <i class="m-nav__link-icon la la-home"></i>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'dashboard'?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Home
              </span>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'attendance' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Daily Attendance Reports
              </span>
            </a>
          </li>


        </ul>
      </div>
      <div>

      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <div class="m-portlet m-portlet--mobile m-portlet--info m-portlet--head-solid-bg m-portlet--bordered m-portlet--rounded">
      <div class="m-portlet__head">
        <div class="m-portlet__head-caption">
          <div class="m-portlet__head-title">
            <h3 class="m-portlet__head-text">
              Daily Attendance Reports
            </h3>
          </div>
        </div>
        <div class="m-portlet__head-tools">
          <?php if($this->session->userdata('user_role')=='admin' || $this->session->userdata('user_role')=='general_manager' || $this->config->item('User_allow_web_users_attendance_mark') == 1){ ?>
            <a href="#" data-toggle="modal" data-target="#attendance_add_modal" class="btn btn-focus m-btn m-btn--custom m-btn--icon m-btn--air m-btn--pill">
              <span>
                <i class="la la-plus"></i>
                <span>
                  Add Attendance Manually
                </span>
              </span>
            </a>
          <?php } ?>
        </div>
      </div>
      <div class="m-portlet__body">
        <div class="m-form m-form--label-align-right m--margin-bottom-10">
          <div class="row align-items-center">
            <div class="col-xl-12 order-2 order-xl-1">
              <div class="form-group m-form__group row align-items-center">

                <div class="col-md-5">
                  <div class="m-form__group ">
                    <label >
                      Date:
                    </label>
                    <input  id="table_filter_date_range"   type="text" class="form-control m-input m-input--air m-input--pill"  >
                  </div>
                </div>
                <div class="col-md-4">
                  <div class="m-form__group ">
                    <label>
                      User:
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select style="width: 100%" class="form-control m-select2 table_filter" id="user"  name="user">
                        <option value="all">All Users</option>
                        <?php
                        foreach($users as $row): ?>
                        <option value="<?php echo $row->user_id?>"><?php echo $row->user_alias?></option>
                        <?php
                      endforeach;
                      ?>
                    </select>
                  </div>
                </div>
              </div>
              <div class="col-md-3">
                <div class="m-form__group ">
                  <label>
                    Attendance Status:
                  </label>
                  <div class="m-select2 m-select2--air m-select2--pill">
                    <select style="width: 100%" class="form-control m-select2 table_filter" id="attendance_status"  name="attendance_status">
                      <option selected value="0">All</option>
                      <option value="1">Present</option>
                      <option value="2">Leave</option>
                      <option value="3">Not Marked</option>
                    </select>
                  </div>
                </div>
              </div>
            </div>
            <div class="m-form__seperator m-form__seperator--dashed"></div>
          </div>
        </div>
      </div>
      <table width="100%" class="table m-table nowrap table-header-fixed table-striped table-bordered table-hover table-header-fixed" id="daily_attendance_view_table">
        <thead>
          <tr class="">
            <th>Sl No</th>
            <th>Attendance ID</th>
            <th>Date</th>
            <th>Day</th>
            <th>User</th>
            <th>Status</th>
            <th>Attendance</th>
            <th>Sign in Time</th>
            <th>Sign Off Time</th>
            <th>Worked Hours</th>
            <th>Overtime</th>
            <th>Sign in From</th>
            <!-- <th>Sign in From (User Provided)</th> -->
            <th>Sign Off From</th>
            <th>Leave Marked Time</th>
            <th>Leave Marked From</th>
            <th>Action</th>
            <th>Leave Type</th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>


      <div class="modal fade" tabindex="-1" id="attendance_add_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog " role="document" style="max-width: 800px">
          <div class="modal-content">
            <div class="modal-header" style="background-color: #36a3f7;border-radius: 4px">
              <h5 class="modal-title" id="exampleModalLabel" style="color: #ffffff" >
                Add Attendance
              </h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">
                  &times;
                </span>
              </button>
            </div>
            <form autocomplete="off" id="form_attendance_add" data-modal-id="attendance_add_modal" method="post" action="<?php echo base_url().'attendance/create'?>" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right " >
              <div class="modal-body">
                <div class="m-form__content">
                  <div class="m-alert m-alert--icon alert alert-warning m--hide" role="alert" id="form_attendance_add_msg">
                    <div class="m-alert__icon">
                      <i class="la la-warning"></i>
                    </div>
                    <div class="m-alert__text">
                      Oh snap! Change a few things up and try submitting again.
                    </div>
                    <div class="m-alert__close">
                      <button type="button" class="close" data-close="alert" aria-label="Close"></button>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-lg-7">
                    <div class="form-group m-form__group" >
                      <label for="user_name">User</label>
                      <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_attendance_mark_by_self_only')==1){ ?>
                        <input readonly value="<?php echo $this->session->userdata('user_alias')?>" class="form-control m-input m-input--air m-input--pill" type="text" />
                        <input value="<?php echo $this->session->userdata('user_id')?>" class="form-control m-input m-input--air m-input--pill" type="hidden" name="user"/>
                      <?php }else{ ?>
                        <div class="m-select2 m-select2--air m-select2--pill">
                          <select style="width: 100%" class="form-control m-select2" id="user_add"  name="user">
                            <option selected disabled>Select User</option>
                            <?php foreach($users as $row): ?>
                              <option value="<?php echo $row->user_id?>"><?php echo $row->user_alias?></option>
                            <?php endforeach; ?>
                          </select>
                        </div>
                      <?php } ?>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-5">
                    <div class="form-group m-form__group" >
                      <label for="user_name">Type</label>
                      <div class="m-radio-inline" style="padding-top: 10px;">
                        <label class="m-radio m-radio--solid m-radio--state-brand">
                          <input required type="radio" class="form_fields_radio"  name="attendance_type" value="sign_in"> Sign In
                          <span></span>
                        </label>
                        <label class="m-radio m-radio--solid m-radio--state-info">
                          <input type="radio" class="form_fields_radio" name="attendance_type"  value="sign_off"> Sign Off
                          <span></span>
                        </label>
                        <label class="m-radio m-radio--solid m-radio--state-danger">
                          <input type="radio" class="form_fields_radio" name="attendance_type"  value="leave"> Leave
                          <span></span>
                        </label>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
                <div class="row" >
                  <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_attendance_mark_by_self_only')==1){ ?>
                  <?php }else{?>
                    <div class="col-lg-4">
                      <div class="form-group m-form__group" >
                        <label for="user_name">Date</label>
                        <input data-date-format="dd-mm-yyyy" value="<?php echo date("d-m-Y")?>"  data-date-end-date="+0d" class="form-control m-input m-input--air m-input--pill" name="date" id="attendance_date_add" placeholder="Select Date" type="text" />
                      </div>
                    </div>
                    <div class="col-lg-4 sign_in_fields" style="display: none">
                      <div class="form-group m-form__group" >
                        <label for="user_name">Sign in Time</label>
                        <input class="form-control m-input m-input--air m-input--pill" name="sign_in_time" id="sign_in_time_add" readonly placeholder="Select time" type="text" />
                      </div>
                    </div>
                    <div class="col-lg-4 sign_off_fields" style="display: none">
                      <div class="form-group m-form__group" >
                        <label for="user_name">Sign Off Time</label>
                        <input class="form-control m-input m-input--air m-input--pill" name="sign_off_time" id="sign_off_time_add" readonly placeholder="Select time" type="text" />
                      </div>
                    </div>
                  <?php } ?>
                  <div class="col-lg-4 leave_fields" style="display: none">
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Leave type
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select style="width: 100%" class="form-control m-select2" id="leave_type_add" name="leave_type"  >
                          <option selected disabled>Select Leave Type</option>
                          <?php foreach ($leave_types as $key => $value) { ?>
                            <option value="<?php echo $value->leave_type_id?>">
                              <?php echo $value->leave_type_name;
                              if($value->leave_paid==1){
                                echo " [Paid]";
                              }else{
                                echo " [UnPaid]";
                              }
                              ?></option>
                            <?php } ?>
                          </select>
                        </div>
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-4 landmark_fields " style="display: none">
                      <div class="form-group m-form__group" >
                        <label for="user_name">
                          Landmark
                        </label>
                        <div class="m-input-icon m-input-icon--right">
                          <input  type="text" name="landmark"  class="form-control m-input m-input--pill m-input--air form_attendance_add_fields" placeholder="Landmark" aria-describedby="basic-addon1">
                          <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-edit"></i></span></span>
                        </div>
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                  </div>
                  <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">
                    Close
                  </button>
                  <button type="submit" class="btn btn-primary form_submit_btn">
                    Submit
                  </button>
                </div>
              </form>
            </div>
          </div>
        </div>
        <div class="modal fade" tabindex="-1" id="attendance_edit_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog " role="document" style="max-width: 800px">
            <div class="modal-content">
              <div class="modal-header" style="background-color: #36a3f7;border-radius: 4px">
                <h5 class="modal-title" id="exampleModalLabel" style="color: #ffffff" >
                  Edit Attendance
                </h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">
                    &times;
                  </span>
                </button>
              </div>
              <form autocomplete="off" id="form_attendance_edit" data-modal-id="attendance_edit_modal" method="post" action="<?php echo base_url().'attendance/update'?>" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right " >
                <div class="modal-body">
                  <div class="m-form__content">
                    <div class="m-alert m-alert--icon alert alert-warning m--hide" role="alert" id="form_attendance_add_msg">
                      <div class="m-alert__icon">
                        <i class="la la-warning"></i>
                      </div>
                      <div class="m-alert__text">
                        Oh snap! Change a few things up and try submitting again.
                      </div>
                      <div class="m-alert__close">
                        <button type="button" class="close" data-close="alert" aria-label="Close"></button>
                      </div>
                    </div>
                  </div>
                  <div class="row" style="margin-top: 10px">
                    <div class="col-lg-4">
                      <div class="form-group m-form__group" >
                        <label for="user_name">Date</label>
                        <input readonly class="form-control m-input m-input--air m-input--pill" id="attendance_date_edit" placeholder="Select Date" type="text" />
                      </div>
                    </div>
                    <div class="col-lg-4" id="attendance_sign_div" style="display: none">
                      <div class="form-group m-form__group" >
                        <label for="user_name">Type</label>
                        <div class="m-radio-inline" style="padding-top: 10px;">
                          <label class="m-radio m-radio--solid m-radio--state-brand">
                            <input type="radio" class="form_fields_radio"  name="attendance_type_edit" value="sign_in"> Sign In
                            <span></span>
                          </label>
                          <label class="m-radio m-radio--solid m-radio--state-info">
                            <input type="radio" class="form_fields_radio" name="attendance_type_edit"  value="sign_off"> Sign Off
                            <span></span>
                          </label>
                        </div>
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-4" id="sign_in_edit_div" style="display: none">
                      <div class="form-group m-form__group" >
                        <label for="user_name">Sign in Time</label>
                        <input class="form-control m-input m-input--air m-input--pill" name="sign_in_time" id="sign_in_time_edit"  placeholder="Select time" type="text" />
                        <span id="sign_in_time_edit_notmarked" style="color: red;display: none">Not Marked !</span>
                      </div>
                    </div>
                    <div class="col-lg-4" id="sign_off_edit_div" style="display: none">
                      <div class="form-group m-form__group" >
                        <label for="user_name">Sign Off Time</label>
                        <input class="form-control m-input m-input--air m-input--pill" name="sign_off_time" id="sign_off_time_edit"  placeholder="Select time" type="text" />
                        <span id="sign_off_time_edit_notmarked" style="color: red">Not Marked !</span>
                      </div>
                    </div>
                    <div class="col-lg-4" id="leave_edit_div" style="display: none">
                      <div class="form-group m-form__group" >
                        <label for="user_name">
                          Leave type
                        </label>
                        <div class="m-select2 m-select2--air m-select2--pill">
                          <select style="width: 100%" class="form-control m-select2" id="leave_type_edit" name="leave_type"  >
                            <option selected disabled>Select Leave Type</option>
                            <?php foreach ($leave_types as $key => $value) { ?>
                              <option value="<?php echo $value->leave_type_id?>">
                                <?php echo $value->leave_type_name;
                                if($value->leave_paid==1){
                                  echo " [Paid]";
                                }else{
                                  echo " [UnPaid]";
                                }
                                ?></option>
                              <?php } ?>
                            </select>
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                      </div>
                      <input id="sign_in_time_edit_1" type="hidden" />
                      <input id="sign_off_time_edit_1" type="hidden" />
                    </div>
                    <input type="hidden"  name="attendance_id" id="attendance_id_edit">
                    <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">
                      Close
                    </button>
                    <button type="submit" class="btn btn-primary form_submit_btn">
                      Save
                    </button>
                  </div>
                </form>
              </div>
            </div>
          </div>
          <div class="modal fade"  tabindex="-1" id="daily_attendance_delete_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog " role="document" >
              <form autocomplete="off" method="post" data-modal-id="daily_attendance_delete_modal" id="form_daily_attendance_delete" action="<?php echo base_url().'attendance/delete'?>" class="form-horizontal">
                <div class="modal-content">
                  <div class="alert alert-danger" role="alert" style="margin-bottom: 0px">
                    <button type="button" class="close" data-dismiss="modal"></button>
                    <br/>
                    <h4 class="alert-heading">Warning! Please Confirm Your Action</h4>
                    <br/>
                    <p> Are You Sure to Delete this Data ?. All the Associated Data will Lost. </p>
                    <div class="alert m-alert--default" role="alert" style="margin-bottom: 10px">
                      <div class="form-group m-form__group" >
                        <label for="user_name" style="color: black">Type</label>
                        <div class="m-radio-inline" style="padding-top: 10px;">
                          <label class="m-radio m-radio--solid m-radio--state-brand">
                            <input required type="radio" class="form_fields_radio"  name="attendance_type_delete" value="1"> Sign In & Sign Off Entry
                            <span></span>
                          </label>
                          <label class="m-radio m-radio--solid m-radio--state-info">
                            <input type="radio" class="form_fields_radio" name="attendance_type_delete"  value="2"> Sign Off Entry only
                            <span></span>
                          </label>
                          <label class="m-radio m-radio--solid m-radio--state-danger">
                            <input type="radio" class="form_fields_radio" name="attendance_type_delete"  value="3"> Leave Entry
                            <span></span>
                          </label>
                        </div>
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <p>
                      <button class="btn btn-danger form_submit_btn" type="submit" name="submit">Confirm & Delete</button>
                      <button type="button" class="btn btn-primary modal-dismiss" data-dismiss="modal">Cancel</button>
                    </p>
                    <input type="hidden"  name="attendance_id" id="delete_attendance_id">
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
      <?php if($this->config->item('Attendance_projectwise_attendance')==1){ ?>
        <div class="m-portlet m-portlet--mobile m-portlet--focus m-portlet--head-solid-bg m-portlet--bordered m-portlet--rounded">
          <div class="m-portlet__head">
            <div class="m-portlet__head-caption">
              <div class="m-portlet__head-title">
                <h3 class="m-portlet__head-text">
                  Daily Project Wise Attendance Reports
                </h3>
              </div>
            </div>
            <div class="m-portlet__head-tools">

            </div>
          </div>
          <div class="m-portlet__body">
            <div class="m-form m-form--label-align-right m--margin-bottom-10">
              <div class="row align-items-center">
                <div class="col-xl-12 order-2 order-xl-1">
                  <div class="form-group m-form__group row align-items-center">
                    <div class="col-md-5">
                      <div class="m-form__group ">
                        <label >
                          Date:
                        </label>
                        <input  id="table_filter_date_range_2"   type="text" class="form-control m-input m-input--air m-input--pill"  >
                      </div>
                    </div>
                    <div class="col-md-4">
                      <div class="m-form__group ">
                        <label>
                          User:
                        </label>
                        <div class="m-select2 m-select2--air m-select2--pill">
                          <select style="width: 100%" class="form-control m-select2 table_filter_2" id="user_2"  name="user">
                            <option value="all">All Users</option>
                            <?php
                            foreach($users as $row): ?>
                            <option value="<?php echo $row->user_id?>"><?php echo $row->user_alias?></option>
                            <?php
                          endforeach;
                          ?>
                        </select>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="m-form__group ">
                      <label>
                        Project:
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select style="width: 100%" class="form-control m-select2 table_filter_2" id="project"  name="project">
                          <option value="all">All Projects</option>
                          <?php
                          foreach($work_contracts as $row): ?>
                          <option value="<?php echo $row->work_id?>"><?php echo $row->work_name?></option>
                          <?php
                        endforeach;
                        ?>
                      </select>
                    </div>
                  </div>
                </div>
              </div>
              <div class="m-form__seperator m-form__seperator--dashed"></div>
            </div>
          </div>
        </div>
        <table width="100%" class="table m-table nowrap table-header-fixed table-striped table-bordered table-hover table-header-fixed" id="daily_projectwise_attendance_view_table">
          <thead>
            <tr class="">
              <th>Sl No</th>
              <th>Attendance ID</th>
              <th>Date</th>
              <th>Day</th>
              <th>User</th>
              <th>Project</th>
              <th>Sign in Time</th>
              <th>Sign Off Time</th>
              <th>Sign in From</th>
              <th>Sign Off From</th>
            </tr>
          </thead>
          <tbody>
          </tbody>
        </table>

      </div>
    </div>
  <?php } ?>
</div>
</div>
</div>
<!-- end:: Body -->
<!-- begin::Footer -->

<?php $this->view("includes/Inc_footer");?>
<!-- end::Footer -->
</div>
<!-- end:: Page -->

<?php $this->view("includes/Inc_sidebar");?>
<?php $this->view("includes/Inc_scripts");?>


<!--begin::Page Snippets -->
<script>
$("#daily_attendance_menu").addClass('m-menu__item--active m-menu__item--open');
$("#daily_attendance_menu_1").addClass('m-menu__item--active');

$("#leave_type_add").select2({
  placeholder: "Select Leave type",
  dropdownParent: $("#form_attendance_add")
})
$("#leave_type_edit").select2({
  placeholder: "Select Employee",
  dropdownParent: $("#form_attendance_edit")
})

$('body').on('click', '#daily_attendance_delete_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#delete_attendance_id').val(no.eq(1).text());
  if(no.eq(7).text()=='' && no.eq(8).text()==''){
    $('input[type=radio][name=attendance_type_delete][value="1"]').attr('disabled',true);
    $('input[type=radio][name=attendance_type_delete][value="2"]').attr('disabled',true);
    $('input[type=radio][name=attendance_type_delete][value="3"]').removeAttr('disabled',true);
    $('input[type=radio][name=attendance_type_delete][value="3"]').attr('checked',true);
  }else{
    $('input[type=radio][name=attendance_type_delete][value="3"]').attr('disabled',true);
    $('input[type=radio][name=attendance_type_delete][value="1"]').removeAttr('disabled',true);
    $('input[type=radio][name=attendance_type_delete][value="2"]').removeAttr('disabled',true);
  }
})
$('body').on('change', 'input[type=radio][name=attendance_type],#user_add', function() {
  var user = $('#user_add :selected').val();
  var attendance_type = $("input[name='attendance_type']:checked").val();
  $("#leave_type_add").removeAttr('required',true);
  $(".leave_fields").hide();
  if(attendance_type=='sign_in'){
    $(".sign_in_fields").show();
    $(".sign_off_fields").hide();
    $(".landmark_fields").show();
  }else if(attendance_type=='sign_off'){
    $(".sign_in_fields").hide();
    $(".sign_off_fields").show();
    $(".landmark_fields").show();
  }else if(attendance_type=='leave'){
    $(".sign_in_fields").hide();
    $(".sign_off_fields").hide();
    $(".landmark_fields").hide();
    $(".leave_fields").show();
    $("#leave_type_add").attr('required',true);
  }else{
    $(".sign_in_fields").hide();
    $(".sign_off_fields").hide();
    $(".landmark_fields").hide();
  }
  if(user!='' && user!=null && user!='Select User'){
    $.post("<?php echo base_url() ?>user/select_details",{user_id:user},function(data) {
      var obj = $.parseJSON(data);
      var i=0;
      if(obj.count==1) {
        if(attendance_type=='sign_in'){
          $("#sign_in_time_add").timepicker({
            defaultTime: obj.duty_start_time_1,
            minuteStep: 1,
          })
        }else if(attendance_type=='sign_off'){
          $("#sign_off_time_add").timepicker({
            defaultTime: obj.duty_end_time_1,
            minuteStep: 1,
          })
        }
      }
    })
  }
})
</script>
<script>

$("#table_filter_time").timepicker( {
  // defaultTime: "<?php //echo date("H:i:s a",strtotime($this->config->item('User_attendance_start_due_time')))?>",
  minuteStep: 1,
})
$("#user_add").select2({
  placeholder: "Select User",
  dropdownParent: $("#form_attendance_add")
})
$("#user").select2({
  placeholder: "Select User"
})
$("#user_2").select2({
  placeholder: "Select User"
})
$("#project").select2({
  placeholder: "Select Project",
  templateResult: format_project_dropdown
})
$("#attendance_status").select2({
  placeholder: "Select Status"
})
$("#report_date,#report_date_update,#report_date_view,#attendance_date_add").datepicker( {
  todayHighlight:!0, orientation:"bottom left", templates: {
    leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
  }
})

$('body').on('change', 'input[type=radio][name=attendance_type_edit]', function() {
  var attendance_type = $("input[name='attendance_type_edit']:checked").val();
  var sign_in_time = $("#sign_in_time_edit_1").val();
  var sign_off_time = $("#sign_off_time_edit_1").val();

  if(attendance_type=='sign_in'){
    $("#sign_in_edit_div").show();
    $("#sign_off_edit_div").hide();
    $("#sign_off_time_edit").val('');

    if(sign_in_time!=''){
      $("#sign_in_time_edit").val(sign_in_time);
      $("#sign_in_time_edit").timepicker({
        defaultTime: sign_in_time,
        minuteStep: 1,
      });
    }else{
      $("#sign_in_time_edit_notmarked").show();
      $("#sign_in_time_edit").val(formatAMPM(new Date));
      $("#sign_in_time_edit").timepicker({
        defaultTime: formatAMPM(new Date),
        minuteStep: 1,
      });
    }

  }else if(attendance_type=='sign_off'){
    $("#sign_off_edit_div").show();
    $("#sign_in_edit_div").hide();
    $("#sign_in_time_edit").val('');

    if(sign_off_time!=''){
      $("#sign_off_time_edit").val(sign_off_time);
      $("#sign_off_time_edit").timepicker({
        defaultTime: sign_off_time,
        minuteStep: 1,
      });
    }else{
      $("#sign_off_time_edit_notmarked").show();
      $("#sign_off_time_edit").val(formatAMPM(new Date));
      $("#sign_off_time_edit").timepicker({
        defaultTime: formatAMPM(new Date),
        minuteStep: 1,
      });
    }
  }
})
$('body').on('click', '#attendance_edit_btn', function() {
  var attendance_status = $(this).data('attendance-status');
  $("input[name='attendance_type_edit']").prop('checked',false);
  $("input[name='attendance_type_edit']").removeAttr('required',true);
  $("#sign_off_edit_div").hide();
  $("#sign_in_edit_div").hide();
  $("#leave_edit_div").hide();
  $("#attendance_sign_div").hide();
  $("#leave_type_edit").removeAttr('required',true);

  $("#sign_in_time_edit_notmarked").hide();
  $("#sign_off_time_edit_notmarked").hide();
  $("#sign_in_time_edit").val('');
  $("#sign_off_time_edit").val('');

  var no = $(this).closest('tr').children('td');
  $('#attendance_id_edit').val(no.eq(1).text());
  $('#attendance_date_edit').val(no.eq(2).text());
  var sign_in_time = no.eq(7).text();
  var sign_off_time = no.eq(8).text();

  if(attendance_status==1){
    $("#attendance_sign_div").show();
    $("input[name='attendance_type_edit']").attr('required',true);
    $("#sign_in_time_edit_1").val(sign_in_time);
    $("#sign_off_time_edit_1").val(sign_off_time);
  }else{
    var leave_type = no.eq(16).text();
    $("#leave_edit_div").show();
    $("#leave_type_edit").attr('required',true);
    $("#leave_type_edit").val(leave_type).trigger('change');
  }
})
function formatAMPM(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var ampm = hours >= 12 ? 'pm' : 'am';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ' ' + ampm;
  return strTime;
}
//  client List Data Table
jQuery(document).ready(function() {

  function GetSearchDate(){
    return $('#table_filter_date_range').val();
  }
  function GetSearchuser(){
    return $('#user').val();
  }
  function GetSearchstatus(){
    return $('#attendance_status').val();
  }
  function GetSearchtime(){
    return $('#table_filter_time').val();
  }
  var table = $('#daily_attendance_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'attendance/select_attendance' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        //                         d.status = GetSearchStatus();
        d.date = GetSearchDate();
        d.user = GetSearchuser();
        d.time = GetSearchtime();
        d.attendance_status = GetSearchstatus();
      }
    },
    "bProcessing": true,
    "deferRender": true,
    "bDeferRender": true,
    "scrollX" : true,
    buttons: [
      { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Daily Attendance Report' },
      { extend: 'csv', className: 'btn purple btn-outline ' },
      { extend: 'colvis', className: 'btn dark btn-outline', text: 'Columns'}
    ],
    dom:"<'row'<'col-sm-6 text-left'B><'col-sm-6 text-right'f>>\n\t\t\t<'row'<'col-sm-12'tr>>\n\t\t\t<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 dataTables_pager'lp>>", // horizobtal scrollable datatable
    "lengthMenu": [
      [25, 50, 100, -1],
      [25, 50, 100, "All"] // change per page values here
    ],
    "columnDefs": [
      {
        "targets": [ 1,16 ],
        className: "hidden"
      },
      {
        render: function (data, type, full, meta) {
          return "<div class='text-wrap width-400'>" + data + "</div>";
        },
        targets: [ 11,12,14 ],
      },
    ],
    scrollCollapse: true,
    fixedColumns: true,
    fixedColumns:   {
      left: 5
    },
    'ordering': [0,'asc'],

  });
  $(".table_filter").on("change", function() {
    table.ajax.reload();
  });
  $(".hide_column").hide();
  // Date Range Picker
  var start = moment();
  var end = moment();

  function cb(start, end) {
    $('#table_filter_date_range').val(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
    table.ajax.reload();
  }

  $('#table_filter_date_range').daterangepicker({
    buttonClasses: "m-btn btn",
    applyClass: "btn-primary",
    cancelClass: "btn-secondary",
    startDate: start,
    endDate: end,
    locale: {
      format: 'MMMM D, YYYY'
    },
    ranges: {
      'Today': [moment(), moment()],
      'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
      'Last 7 Days': [moment().subtract(6, 'days'), moment()],
      'Last 30 Days': [moment().subtract(29, 'days'), moment()],
      'This Month': [moment().startOf('month'), moment().endOf('month')],
      'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
      'All time': [moment('1970-01-01'), moment().add(9999, 'days')],
    }
  }, cb);
  cb(start, end);

  <?php if($this->config->item('Attendance_projectwise_attendance')==1){ ?>
    function GetSearchDate2(){
      return $('#table_filter_date_range_2').val();
    }
    function GetSearchuser2(){
      return $('#user_2').val();
    }
    function GetSearchproject(){
      return $('#project').val();
    }

    var table2 = $('#daily_projectwise_attendance_view_table').DataTable({
      "ajax":{
        url :"<?php echo base_url().'attendance/select_attendance_projectwise' ?>", // json datasource
        type: "post", // type of method  , by default would be get
        data: function(d){
          d.date = GetSearchDate2();
          d.user = GetSearchuser2();
          d.project = GetSearchproject();
        }
      },
      "bProcessing": true,
      "deferRender": true,
      "bDeferRender": true,
      "scrollX" : true,
      buttons: [
        { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Daily Projet wise Attendance' },
        { extend: 'csv', className: 'btn purple btn-outline ' },
        { extend: 'colvis', className: 'btn dark btn-outline', text: 'Columns'}
      ],
      dom:"<'row'<'col-sm-6 text-left'B><'col-sm-6 text-right'f>>\n\t\t\t<'row'<'col-sm-12'tr>>\n\t\t\t<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 dataTables_pager'lp>>", // horizobtal scrollable datatable
      "lengthMenu": [
        [25, 50, 100, -1],
        [25, 50, 100, "All"] // change per page values here
      ],
      "columnDefs": [
        {
          "targets": [ 1 ],
          className: "hidden"
        },
        {
          render: function (data, type, full, meta) {
            return "<div class='text-wrap width-400'>" + data + "</div>";
          },
          targets: [ 8 ],
        },
      ],
      'ordering': [0,'asc']
    });
    $(".table_filter_2").on("change", function() {
      table2.ajax.reload();
    });
    // Date Range Picker
    var start2 = moment();
    var end2 = moment();

    function cb2(start2, end2) {
      $('#table_filter_date_range_2').val(start2.format('MMMM D, YYYY') + ' - ' + end2.format('MMMM D, YYYY'));
      table2.ajax.reload();
    }

    $('#table_filter_date_range_2').daterangepicker({
      buttonClasses: "m-btn btn",
      applyClass: "btn-primary",
      cancelClass: "btn-secondary",
      startDate: start2,
      endDate: end2,
      locale: {
        format: 'MMMM D, YYYY'
      },
      ranges: {
        'Today': [moment(), moment()],
        'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
        'Last 7 Days': [moment().subtract(6, 'days'), moment()],
        'Last 30 Days': [moment().subtract(29, 'days'), moment()],
        'This Month': [moment().startOf('month'), moment().endOf('month')],
        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
        'All time': [moment('1970-01-01'), moment().add(9999, 'days')],
      }
    }, cb2);
    cb2(start2, end2);
    <?php } ?>

    $(".form_submit_btn").click(function (e) {
      e.preventDefault();
      var a = $(this),
      l = $(this).closest("form"),
      action = l.attr('action');
      var form_id = l.attr('id');
      l.validate({
        rules: {
          work_id: {
            required: !0
          }
        }, invalidHandler: function (e, r) {
          var i = $("#" + form_id + "_msg");
          i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
        }, submitHandler: function (e) {
        }
      }), l.valid() && (a.addClass("m-loader m-loader--right m-loader--light").attr("disabled", !0),
      l.ajaxSubmit({
        url: action,
        success: function (e, t, r, s) {

          var modal_id = l.attr('data-modal-id');
          var form_type = l.attr('data-form-type');
          $("#" + form_id + "_msg").addClass("m--hide");
          // Block UI
          mApp.blockPage({
            overlayColor: "#000000",
            type: "loader",
            state: "success",
            message: "Please wait..."
          }), setTimeout(function () {
            mApp.unblockPage()
          }, 2e3);
          var obj = $.parseJSON(e);
          if (obj.status == 1) {
            a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
            $("#"+modal_id).modal('toggle');
            $("."+form_id+"_fields").val('');
            $("."+form_id+"_select").val('').trigger("change");
            table.ajax.reload();
            toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
          } else {
            a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
            toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
          }
          if (obj == null) {
            setTimeout(function () {
              a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
              toastr["warning"]("Your Attempt is Timed Out. Please Try Again.", "Timed Out");
            }, 2e4)
          }
        },error: function() {
          a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
          toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
        }
      })
    )
  });
})
</script>
<style>
.text-wrap{
  white-space:normal;
}
.width-200{
  width:400px;
}
.width-120{
  width:300px;
}
</style>
