<?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 Work 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().'daily_report' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Daily Work 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 Work Reports Details
            </h3>
          </div>
        </div>
        <div class="m-portlet__head-tools">
          <a href="#daily_report_add_modal" data-toggle="modal" data-target="#daily_report_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 Daily Work Reports
              </span>
            </span>
          </a>
        </div>
        <!-- Add Daily Report modal starts from here -->

        <!-- Add Daily Report Modal ends here -->
      </div>
      <div class="m-portlet__body">

        <!--begin: Datatable -->
        <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-6">
                  <div class="form-group m-form__group">
                    <label>
                      Work:
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select style="width: 100%" class="form-control m-select2 table_filter" id="work"  name="work" data-placeholder="Select Work">
                        <option value="all">All Works</option>
                        <?php
                        foreach($work_contracts as $row): ?>
                        <option data-work-status="<?php echo $row->work_status?>" value="<?php echo $row->work_id?>"><?php echo $row->work_name?></option>
                        <?php
                      endforeach;
                      ?>
                    </select>
                  </div>
                  <span class="m-form__help"></span>
                </div>
              </div>
              <div class="col-md-6">
                <div class="m-form__group m-form__group--inline">
                  <div class="m-form__label">
                    <label class="m-label m-label--single">
                      Date:
                    </label>
                  </div>
                  <div class="m-form__control">
                    <input  id="table_filter_date_range"   type="text" class="form-control m-input m-input--air m-input--pill"  >
                  </div>
                </div>
              </div>
            </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_report_view_table">
        <thead>
          <tr class="">
            <th>Sl No</th>
            <th>Report ID</th>
            <th style="display:none">Work ID</th>
            <th>Work Name</th>
            <th>Report Date</th>
            <th>Description</th>
            <th>Created By</th>
            <th>Created Date</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>

      <div class="modal fade" tabindex="-1" id="daily_report_add_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog " role="document" >
          <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 Daily Work Reports
              </h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">
                  &times;
                </span>
              </button>
            </div>
            <form autocomplete="off" data-modal-id="daily_report_add_modal" id="form_daily_report_add" method="post" action="<?php echo base_url().'daily_reports/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_daily_report_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-12">
                    <div class="form-group m-form__group">
                      <label for="user_name">
                        Work Name
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select required style="width: 100%" class="form-control m-select2 form_daily_report_add_select" id="work_1"  name="work_id" data-placeholder="Select Work">
                          <option selected disabled>Select Work</option>
                          <?php
                          foreach($work_contracts as $row): ?>
                          <option data-work-status="<?php echo $row->work_status?>" value="<?php echo $row->work_id?>"><?php echo $row->work_name?></option>
                          <?php
                        endforeach;
                        ?>
                      </select>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
              </div>
              <div class="row">
                <div class="col-lg-12">
                  <div class="form-group m-form__group">
                    <label for="user_name">
                      Date
                    </label>
                    <div class="input-group m-input-group--air date">
                      <div class="input-group-prepend">
                        <span class="input-group-text">
                          <i class="la la-calendar"></i>
                        </span>
                      </div>
                      <input required data-date-format="dd-mm-yyyy" id="report_date" type="text" name="report_date" class="form-control m-input form_daily_report_add_fields" placeholder="Work Date" >
                    </div>
                  </div>
                  <span class="m-form__help"></span>
                </div>
              </div>
              <div class="row">
                <div class="col-lg-12">
                  <div class="form-group m-form__group">
                    <label for="user_name">
                      Description
                    </label>
                    <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                      <textarea required name="description" id="description"  class="form-control m-input form_daily_report_add_fields"  aria-describedby="basic-addon1" rows="7"></textarea>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>
            </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="daily_report_edit_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog " role="document" >
        <div class="modal-content" >
          <div class="modal-header" style="background-color: #36a3f7;border-radius: 4px">
            <h5 class="modal-title" id="exampleModalLabel" style="color: #ffffff" >
              Update Daily Work Reports
            </h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">
                &times;
              </span>
            </button>
          </div>
          <form autocomplete="off" data-modal-id="daily_report_edit_modal" id="form_daily_report_edit" method="post" action="<?php echo base_url().'daily_reports/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_daily_report_edit_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-12">
                  <div class="form-group m-form__group">
                    <label for="user_name">
                      Work Name
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select required style="width: 100%" class="form-control m-select2 form_daily_report_edit_select" id="work_update"  name="work_id" data-placeholder="Select Work">
                        <<option selected disabled>Select Work</option>
                        <?php
                        foreach($work_contracts as $row): ?>
                        <option data-work-status="<?php echo $row->work_status?>" value="<?php echo $row->work_id?>"><?php echo $row->work_name?></option>
                        <?php
                      endforeach;
                      ?>
                    </select>
                  </div>
                  <span class="m-form__help"></span>
                </div>
              </div>
              <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
            </div>
            <div class="row">
              <div class="col-lg-12">
                <div class="form-group m-form__group">
                  <label for="user_name">
                    Date
                  </label>
                  <div class="input-group m-input-group--air date">
                    <div class="input-group-prepend">
                      <span class="input-group-text">
                        <i class="la la-calendar"></i>
                      </span>
                    </div>
                    <input required data-date-format="dd-mm-yyyy" id="report_date_update" type="text" name="report_date" class="form_daily_report_edit_fields form-control m-input " placeholder="Work Date" >
                  </div>
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="row">
              <div class="col-lg-12">
                <div class="form-group m-form__group">
                  <label for="user_name">
                    Description
                  </label>
                  <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                    <textarea required id="description_update" name="description"  class="form-control m-input form_daily_report_edit_fields"  aria-describedby="basic-addon1" rows="7"></textarea>
                  </div>
                  <span class="m-form__help"></span>
                </div>
                <input type="hidden" id="report_id" name="report_id">
              </div>
            </div>
          </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">
              Update
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
  <div class="modal fade" tabindex="-1" id="daily_report_view_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog " role="document" >
      <div class="modal-content" >
        <div class="modal-header" style="background-color: #36a3f7;border-radius: 4px">
          <h5 class="modal-title" id="exampleModalLabel" style="color: #ffffff" >
            View Daily Work Reports
          </h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">
              &times;
            </span>
          </button>
        </div>
        <form autocomplete="off" id="update_daily_report_form" method="post" action="" 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_material_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-12">
                <div class="form-group m-form__group">
                  <label for="user_name">
                    Work Name
                  </label>
                  <div class="m-select2 m-select2--air m-select2--pill">
                    <select disabled style="width: 100%" class="form-control m-select2 m-select2--solid m-input--solid m-input  table_filter" id="work_view" readonly name="work_1" data-placeholder="Select Work">
                      <option selected disabled>Select Work</option>
                      <?php
                      foreach($work_contracts as $row): ?>
                      <option data-work-status="<?php echo $row->work_status?>" value="<?php echo $row->work_id?>"><?php echo $row->work_name?></option>
                      <?php
                    endforeach;
                    ?>
                  </select>
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
          </div>
          <div class="row">
            <div class="col-lg-12">
              <div class="form-group m-form__group">
                <label for="user_name">
                  Date
                </label>
                <div class="input-group m-input-group--air date">
                  <div class="input-group-prepend">
                    <span class="input-group-text">
                      <i class="la la-calendar"></i>
                    </span>
                  </div>
                  <input  data-date-format="dd-mm-yyyy" readonly id="report_date_view" type="text" name="report_date" class="form-control m-input--solid m-input  m-input " placeholder="Work Date" >
                </div>
              </div>
              <span class="m-form__help"></span>
            </div>
          </div>
          <div class="row">
            <div class="col-lg-12">
              <div class="form-group m-form__group">
                <label for="user_name">
                  Description
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <textarea id="description_view" readonly name="description"  class="form-control m-input--solid m-input  m-input"  aria-describedby="basic-addon1" rows="7"></textarea>
                </div>
                <span class="m-form__help"></span>
              </div>
              <input type="hidden" id="report_id" name="report_id">
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">
            Close
          </button>
        </div>
      </form>
    </div>
  </div>
</div>
<div class="modal fade"  tabindex="-1" id="daily_report_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_report_delete_modal" id="form_daily_report_delete" action="<?php echo base_url().'daily_reports/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>
          <p>
            <button class="btn btn-danger form_submit_btn" id="delete_purchase_item" 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="report_id" id="delete_report_id">
        </div>
      </div>
    </form>
  </div>
</div>
</div>
</div>
</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>
$('body').on('click', '#daily_report_edit_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#work_update').val(no.eq(2).text()).trigger('change');
  $('#description_update').val(no.eq(5).text());
  $('#report_date_update').val(no.eq(4).text());
  $('#report_id').val(no.eq(1).text());

})
$('body').on('click', '#daily_report_view_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#work_view').val(no.eq(2).text()).trigger('change');
  $('#description_view').val(no.eq(5).text());
  $('#report_date_view').val(no.eq(4).text());

})
$('body').on('click', '#daily_report_delete_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#delete_report_id').val(no.eq(1).text());

})
</script>
<script>
$("#daily_reports_menu").addClass('m-menu__item--active');

$("#work,#work_1,#work_update,#work_view").select2({
  placeholder: "Select  Work",
  templateResult: format_project_dropdown
})
$("#report_date,#report_date_update,#report_date_view").datepicker( {
  todayHighlight:!0, orientation:"bottom left", templates: {
    leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
  },
  endDate: '+0d',
  <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
  startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
  <?php } ?>
})
//  client List Data Table
jQuery(document).ready(function() {
  function toaster(msg_type,message,title){
    toastr.options = {
      "closeButton": false,
      "debug": false,
      "newestOnTop": false,
      "progressBar": true,
      "positionClass": "toast-top-center",
      "preventDuplicates": false,
      "onclick": null,
      "showDuration": "300",
      "hideDuration": "1000",
      "timeOut": "5000",
      "extendedTimeOut": "1000",
      "showEasing": "swing",
      "hideEasing": "linear",
      "showMethod": "fadeIn",
      "hideMethod": "fadeOut"
    };
    if(msg_type=="success"){
      toastr.success(message,title);
    }else{
      toastr.error(message,title);
    }

  }
  function GetSearchDate(){
    return $('#table_filter_date_range').val();
  }
  function GetSearchwork(){
    return $('#work').val();
  }
  var table = $('#daily_report_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'daily_reports/select_daily_report' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        //                         d.status = GetSearchStatus();
        d.date = GetSearchDate();
        d.work = GetSearchwork();
      }
    },
    "bProcessing": true,
    "deferRender": true,
    "bDeferRender": true,
    "scrollX" : true,
    scrollX:!0, scrollCollapse:!0,
    buttons: [
      { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Daily Reports' },
      { 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,2 ],
        className: "hidden"
      }
    ],
    'order': false
  });
  $(".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);

  $(".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);
          toastr["success"](obj.message, "Success");
          $("#"+modal_id).modal('toggle');
          $("."+form_id+"_fields").val('');
          $("."+form_id+"_select").val('').trigger("change");

          table.ajax.reload();
        } else {
          a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
          if (obj.status == 2) {
            toastr["warning"](obj.message, "Duplication");
          }else{
            toastr["error"](obj.message, "Error");
          }
        }
        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>
