<?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">
          Manage Tools Transfers
        </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().'tools/transferd' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Tools Transfers
              </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">
              List of Transfers
            </h3>
          </div>
        </div>
        <div class="m-portlet__head-tools">
          <?php if($this->session->userdata('user_role')!='auditor' || $this->session->userdata('user_data_permission')['adding']==1) { ?>
            <a href="#" data-toggle="modal" data-target="#tool_transfer_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 New Transfer
                </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="">
            <div class="col-xl-12 order-2 order-xl-1">
              <div class="form-group m-form__group row " style="padding-bottom: 0px">
                <div class="col-md-4">
                  <div class="form-group m-form__group">
                    <label >
                      Transfer Date:
                    </label>
                    <input style="border: none" id="date_range"  type="text" class="form-control  m-input--solid m-input "  >
                  </div>
                </div>
                <div class="col-md-4">
                  <div class="form-group m-form__group">
                    <label>
                      Tool Category:
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select style="width: 100%" class="form-control m-select2 table_filter" id="tools_category_filter"  name="" data-placeholder="Select Tool Category">

                      </select>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-md-4">
                  <div class="form-group m-form__group">
                    <label>
                      Tool:
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select style="width: 100%" class="form-control m-select2 table_filter" id="tools_filter"  name="" data-placeholder="Select Tool">

                      </select>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>
              <div class="form-group m-form__group row " style="padding-top: 0px">
                <div class="col-md-4">
                  <div class="form-group m-form__group">
                    <label>
                      Transfer From:
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select style="width: 100%" class="form-control m-select2 table_filter" id="transfer_from_filter"  name="" data-placeholder="Select Transfer From">

                      </select>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-md-4">
                  <div class="form-group m-form__group">
                    <label>
                      Transfer To:
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select style="width: 100%" class="form-control m-select2 table_filter" id="transfer_to_filter"  name="" data-placeholder="Select Transfer To">

                      </select>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!--begin: Datatable -->
        <?php $this->view("modules/include_tables/table_tool_transfer");?>
        <!--end: Datatable -->
      </div>
    </div>
  </div>

  <div class="modal fade" tabindex="-1" id="tool_transfer_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 New Transfer
          </h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">
              &times;
            </span>
          </button>
        </div>
        <!--                                                    ADD EXPENSE DATA start-->
        <form autocomplete="off" id="form_tool_transfer_add" data-modal-id="tool_transfer_add_modal" method="post" action="<?php echo base_url().'tools/create_transfer'?>" 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_tool_transfer_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">
                    Tool
                  </label>
                  <div class="m-select2 m-select2--air m-select2--pill">
                    <select required style="width: 100%" class="form-control m-select2 form_tool_transfer_add_select" id="tools_add"  name="tool" data-placeholder="Select Tool">
                      <option selected disabled>Select Tool</option>
                      <?php
                      foreach($tools as $row): ?>
                      <option value="<?php echo $row->tool_id?>"><?php echo $row->name." [".$row->cat_name."]"?></option>
                      <?php
                    endforeach;
                    ?>
                  </select>
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-6">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Transfer From
                </label>
                <div class="m-select2 m-select2--air m-select2--pill">
                  <select required style="width: 100%" class="form-control m-select2 form_tool_transfer_add_select" id="transfer_from_add"  name="transfer_from" data-placeholder="Select Transfer From">
                    <option selected disabled>Select Transfer From</option>
                  </select>
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-6">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Transfer To
                </label>
                <div class="m-select2 m-select2--air m-select2--pill">
                  <select required style="width: 100%" class="form-control m-select2 form_tool_transfer_add_select" id="transfer_to_add"  name="transfer_to" data-placeholder="Select Transfer To">
                    <option selected disabled>Select Transfer To</option>
                  </select>
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-4">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Available Qty
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <input readonly type="text" name="available_qty" id="available_qty"  class="form-control m-input form_tool_transfer_add_fields" value="0" aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-4">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Transferred Qty
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <input required type="text" name="transferred_qty" id="transferred_qty"  class="form-control m-input form_tool_transfer_add_fields" placeholder="Transferred Qty" aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-4">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Transferred Date
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <input required type="text" name="transferred_date" id="transferred_date"  class="form-control m-input form_tool_transfer_add_fields" placeholder="Transferred Date" data-date-format="dd-mm-yyyy" value="<?php echo date('d-m-Y')?>" aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-6">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Transferred By
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <input type="text" name="transferred_by"  class="form-control m-input form_tool_transfer_add_fields" placeholder="Transferred By" aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-6">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Remarks
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <input type="text" name="remarks"  class="form-control m-input form_tool_transfer_add_fields" placeholder="Remarks" aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
          </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="tool_transfer_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: #00c5dc;border-radius: 4px">
        <h5 class="modal-title" id="exampleModalLabel" style="color: #ffffff" >
          Edit Tool Details
        </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="tool_transfer_edit_modal" id="form_tool_transfer_edit" method="post" action="<?php echo base_url().'tools/update_transfer'?>" 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_tool_transfer_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">
                  Tool
                </label>
                <div class="m-select2 m-select2--air m-select2--pill">
                  <select disabled style="width: 100%" class="form-control m-select2 form_tool_transfer_edit_select" id="tools_edit"  name="tool" data-placeholder="Select Tool">
                    <option selected disabled>Select Tool</option>
                    <?php
                    foreach($tools as $row): ?>
                    <option value="<?php echo $row->tool_id?>"><?php echo $row->name." [".$row->cat_name."]"?></option>
                    <?php
                  endforeach;
                  ?>
                </select>
              </div>
              <span class="m-form__help"></span>
            </div>
          </div>
          <div class="col-lg-6">
            <div class="form-group m-form__group" >
              <label for="user_name">
                Transfer From
              </label>
              <div class="m-select2 m-select2--air m-select2--pill">
                <select disabled style="width: 100%" class="form-control m-select2 form_tool_transfer_edit_select" id="transfer_from_edit"  name="transfer_from" data-placeholder="Select Transfer From">
                  <option selected disabled>Select Transfer From</option>
                </select>
              </div>
              <span class="m-form__help"></span>
            </div>
          </div>
          <div class="col-lg-6">
            <div class="form-group m-form__group" >
              <label for="user_name">
                Transfer To
              </label>
              <div class="m-select2 m-select2--air m-select2--pill">
                <select disabled style="width: 100%" class="form-control m-select2 form_tool_transfer_edit_select" id="transfer_to_edit"  name="transfer_to" data-placeholder="Select Transfer To">
                  <option selected disabled>Select Transfer To</option>
                </select>
              </div>
              <span class="m-form__help"></span>
            </div>
          </div>
          <!-- <div class="col-lg-4">
          <div class="form-group m-form__group" >
          <label for="user_name">
          Available Qty
        </label>
        <div class="input-group m-input-group m-input-group--pill m-input-group--air">
        <input readonly type="text" name="available_qty" id="available_qty_edit"  class="form-control m-input form_tool_transfer_edit_fields" value="0" aria-describedby="basic-addon1">
      </div>
      <span class="m-form__help"></span>
    </div>
  </div> -->
  <div class="col-lg-6">
    <div class="form-group m-form__group" >
      <label for="user_name">
        Transferred Qty
      </label>
      <div class="input-group m-input-group m-input-group--pill m-input-group--air">
        <input readonly type="text" name="transferred_qty" id="transferred_qty_edit"  class="form-control m-input form_tool_transfer_edit_fields" placeholder="Transferred Qty" aria-describedby="basic-addon1">
      </div>
      <span class="m-form__help"></span>
    </div>
  </div>
  <div class="col-lg-6">
    <div class="form-group m-form__group" >
      <label for="user_name">
        Transferred Date
      </label>
      <div class="input-group m-input-group m-input-group--pill m-input-group--air">
        <input required type="text" name="transferred_date" id="transferred_date_edit"  class="form-control m-input form_tool_transfer_edit_fields" placeholder="Transferred Date" data-date-format="dd-mm-yyyy" value="<?php echo date('d-m-Y')?>" aria-describedby="basic-addon1">
      </div>
      <span class="m-form__help"></span>
    </div>
  </div>
  <div class="col-lg-6">
    <div class="form-group m-form__group" >
      <label for="user_name">
        Transferred By
      </label>
      <div class="input-group m-input-group m-input-group--pill m-input-group--air">
        <input type="text" name="transferred_by" id="transferred_by_edit"  class="form-control m-input form_tool_transfer_edit_fields" placeholder="Transferred By" aria-describedby="basic-addon1">
      </div>
      <span class="m-form__help"></span>
    </div>
  </div>
  <div class="col-lg-6">
    <div class="form-group m-form__group" >
      <label for="user_name">
        Remarks
      </label>
      <div class="input-group m-input-group m-input-group--pill m-input-group--air">
        <input type="text" name="remarks" id="remarks_edit"  class="form-control m-input form_tool_transfer_edit_fields" placeholder="Remarks" aria-describedby="basic-addon1">
      </div>
      <span class="m-form__help"></span>
    </div>
  </div>
  <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
  <input type="hidden" name="transfer_id" id="transfer_id_edit" >
</div>
</div>
<div class="modal-footer">
  <button type="button" class="btn  btn-secondary" data-dismiss="modal">
    Close
  </button>
  <button type="submit" class="btn form_submit_btn btn-primary">
    Submit
  </button>
</div>
</form>
</div>
</div>
</div>
<div class="modal fade" tabindex="-1" id="tool_transfer_delete_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog " role="document" >
    <form autocomplete="off" data-modal-id="tool_transfer_delete_modal" id="form_tool_transfer_delete" method="post" action="<?php echo base_url().'tools/delete_transfer'?>" 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" 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="transfer_id" id="transfer_id_delete">
          <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
        </div>
      </div>
    </form>
  </div>
</div>
<div class="modal fade" tabindex="-1" id="transfer_return_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: #00c5dc;border-radius: 4px">
        <h5 class="modal-title" id="exampleModalLabel" style="color: #ffffff" >
          Return Transferred Tools
        </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="transfer_return_modal" id="form_transfer_return" method="post" action="<?php echo base_url().'tools/update_transfer_return'?>" 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_transfer_return_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-4" >
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Balance to Return
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <input readonly required type="text" name="returnable_qty_status_change" id="returnable_qty_edit_status_change"  class="form-control m-input form_transfer_return_fields" placeholder="Qty to be returned" aria-describedby="basic-addon1">
                  <input required type="hidden" name="current_returned_qty_status_change" id="current_returned_qty_status_change">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-4" >
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Returned Quantity
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <input required type="text" min="1" name="quantity" id="returned_qty_edit_status_change"  class="form-control m-input form_transfer_return_fields" placeholder="Returned Qty" aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-4">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Returned Date
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <input required type="text" name="date" id="returned_date_edit_status_change"  class="form-control m-input form_transfer_return_fields" placeholder="Returned Date" data-date-format="dd-mm-yyyy" value="<?php echo date('d-m-Y')?>" aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
            <input type="hidden" name="transfer_id" id="transfer_id_return" >
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn  btn-secondary" data-dismiss="modal">
            Close
          </button>
          <button type="submit" class="btn form_submit_btn btn-primary">
            Submit
          </button>
        </div>
      </form>
    </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");?>

<?php $this->view("modules/include_scripts/tool_transfer_script");?>
<!--begin::Page Snippets -->
<script>
$("#tools_menu").addClass('m-menu__item--active m-menu__item--open');
$("#tools_menu_3").addClass('m-menu__item--active');

$("#transferred_date,#transferred_date_edit,#returned_date_edit_status_change").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 } ?>
  })
  $("#tools_category_filter,#tools_filter,#transfer_from_filter,#transfer_to_filter").select2();

  function select_tools_category(select_id,type){
    $('#'+select_id).html('');
    if(type=='filter'){
      $("#"+select_id).append("<option value='all' selected >All Category</option>");
    }else{
      $("#"+select_id).append("<option disabled selected >Select Tool Category</option>");
    }
    $.post("<?php echo base_url() ?>tools/select_category_list",{},function(data) {
      var obj = $.parseJSON(data);
      var i=0;
      if(obj.count>0) {
        while (i < obj.id.length) {
          $("#"+select_id).append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
          i++;
        }
      }
    })
  }
  select_tools_category("tools_category_filter",'filter');

  function select_tools(select_id){
    var category_id=$('#tools_category_filter :selected').val();
    $("#"+select_id).html('');
    $("#"+select_id).append("<option value='all' selected >All Tools</option>");
    $.post("<?php echo base_url() ?>tools/select_tools_list",{category_id:category_id},function(data) {
      var obj = $.parseJSON(data);
      var i=0;
      if(obj.count>0) {
        while (i < obj.id.length) {
          $("#"+select_id).append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
          i++;
        }
      }
    })
    $("#"+select_id).trigger('change');
  }
  select_tools("tools_filter");
  $('body').on('change', '#tools_category_filter', function() {
    select_tools("tools_filter");
  })

  function select_locations(select_id,type='',location_id=''){
    $("#"+select_id).html('');
    if(type=='filter'){
      $("#"+select_id).append("<option value='all' selected >All Locations</option>");
    }else{
      $("#"+select_id).append("<option disabled selected >Select Location</option>");
    }
    $.post("<?php echo base_url() ?>tools/select_locations",{location_id:location_id},function(data) {
      var obj = $.parseJSON(data);
      var i=0;
      if(obj.count>0) {
        while (i < obj.id.length) {
          $("#"+select_id).append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
          i++;
        }
      }
    })
    $("#"+select_id).trigger('change');
  }
  select_locations("transfer_from_filter",'filter');
  select_locations("transfer_to_filter",'filter');
  select_locations("transfer_from_edit",'','all');
  select_locations("transfer_to_edit");

  $("#tools_add").select2({
    placeholder: "Select Tool Category",
    dropdownParent: $("#form_tool_transfer_add")
  })
  $("#transfer_from_add").select2({
    dropdownParent: $("#form_tool_transfer_add"),
    templateResult: format_project_dropdown
  })
  $("#transfer_to_add").select2({
    dropdownParent: $("#form_tool_transfer_add"),
    templateResult: format_project_dropdown
  })
  $("#tools_edit").select2({
    placeholder: "Select Tool Category",
    dropdownParent: $("#form_tool_transfer_edit")
  })
  $("#transfer_from_edit").select2({
    dropdownParent: $("#form_tool_transfer_edit"),
    templateResult: format_project_dropdown
  })
  $("#transfer_to_edit").select2({
    dropdownParent: $("#form_tool_transfer_edit"),
    templateResult: format_project_dropdown
  })
  $('body').on('change', '#tools_add', function() {
    var tool_id=$('#tools_add :selected').val();
    $('#transfer_from_add').html('');
    $("#transfer_from_add").append("<option disabled selected >Select Transfer From</option>");
    $.post("<?php echo base_url() ?>tools/select_available_locations",{tool_id:tool_id},function(data) {
      var obj = $.parseJSON(data);
      var i=0;
      if(obj.count>0) {
        while (i < obj.locations.length) {
          $("#transfer_from_add").append("<option value=" + obj.locations[i]['id'] + ">" + obj.locations[i]['name'] + "</option>");
          i++;
        }
      }
    })
  })
  $('body').on('change', '#transfer_from_add', function() {
    var location_id=$('#transfer_from_add :selected').val();
    $('#transfer_to_add').html('');
    $("#transfer_to_add").append("<option disabled selected >Select Transfer To</option>");
    // Select Other Locations
    $.post("<?php echo base_url() ?>tools/select_locations",{location_id:location_id},function(data) {
      var obj = $.parseJSON(data);
      var i=0;
      if(obj.count>0) {
        while (i < obj.id.length) {
          $("#transfer_to_add").append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
          i++;
        }
      }
    })
    var tool_id=$('#tools_add :selected').val();
    // Select Max Available Qty
    $.post("<?php echo base_url() ?>tools/select_available_qty",{location_id:location_id,tool_id:tool_id},function(data) {
      var obj = $.parseJSON(data);
      var i=0;
      if(obj.count==1) {
        $("#available_qty").val(obj.available_qty);
        $("#transferred_qty").attr('max',obj.available_qty);
      }
    })
  })

  $('body').on('click', '#tool_transfer_edit_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#transfer_id_edit').val(no.eq(1).text());
    $('#tools_edit').val(no.eq(14).text()).trigger('change');
    $('#transfer_from_edit').val(no.eq(15).text()).trigger('change');
    $('#transfer_to_edit').val(no.eq(16).text()).trigger('change');
    $('#remarks_edit').val(no.eq(8).text());
    $('#transferred_by_edit').val(no.eq(7).text());
    $('#transferred_date_edit').val(no.eq(6).text());
    $('#transferred_qty_edit').val(no.eq(5).text());

  })
  $('body').on('click', '#tool_transfer_delete_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#transfer_id_delete').val(no.eq(1).text());
  })
  $('body').on('click', '#btn_return_transfer', function() {
    var no = $(this).closest('tr').children('td');
    $('#transfer_id_return').val(no.eq(1).text());
    var transferred_qty =  no.eq(5).text();
    var returned_qty =  no.eq(9).text();
    var balance_to_return =  no.eq(10).text();
    $('#returnable_qty_edit_status_change').val(balance_to_return);
    $('#current_returned_qty_status_change').val(returned_qty);
    $("#returned_qty_edit_status_change").attr('max',balance_to_return);
  })

  $(".hide_column").hide();
  // Date Range Picker
  var start = moment();
  var end = moment();

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

  $('#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: {
        transferred_qty: {
          number: !0,
          required: !0,
          min: 1
        }
      }, 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[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
          $("#"+modal_id).modal('toggle');
          $("."+form_id+"_fields").val('');
          $("."+form_id+"_select").val('').trigger("change");

          select_tools_category("tools_category_filter",'filter');
          select_tools_category("tools_category_add");
          select_tools_category("tools_category_edit");
          tool_transfer_view_table.ajax.reload();
        } 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>
.hidden{
  display: none;
}
</style>
