<?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">
          Loans Management
        </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().'loans' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Loans Management
              </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 Loans Payable
            </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="#loan_add_modal" data-toggle="modal" data-target="#loan_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>
                  Create New Payable Loan
                </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">
            <div class="col-xl-12 order-2 order-xl-1">
              <div class="form-group m-form__group row ">
                <div class="col-md-6">
                  <div class="form-group m-form__group">
                    <label>
                      Loan Provider:
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select style="width: 100%" class="form-control m-select2 table_filter" id="table_filter_loan_provider"   data-placeholder="Select Provider">

                      </select>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-md-6">
                  <div class="form-group m-form__group">
                    <label>
                      Loan Pupose:
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select style="width: 100%" class="form-control m-select2 table_filter" id="table_filter_loan_purpose"   data-placeholder="Select Purpose">
                        <option selected value="all">All Loans</option>
                        <option value="0">For Whole Business</option>
                        <?php
                        foreach($work_contracts as $row): ?>
                        <option data-work-status="<?php echo $row->work_status?>" value="<?php echo $row->work_id?>">For Project : <?php echo $row->work_name?></option>
                        <?php
                      endforeach;
                      ?>
                    </select>
                  </div>
                  <span class="m-form__help"></span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <?php $this->view("modules/include_tables/table_loans");?>
      <!--end: Datatable -->
    </div>
  </div>
  <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 Payments Towards Loan
          </h3>
        </div>
      </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 ">
              <div class="col-md-4">
                <div class="m-form__group ">
                  <label>
                    Date:
                  </label>
                  <input style="border: none" id="date_range"  type="text" class="form-control  m-input--solid m-input "  >
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!--begin: Datatable -->
      <?php $this->view("modules/include_tables/table_loan_transaction");?>
      <!--end: Datatable -->
    </div>
  </div>
  <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">
            List of Loan Providers
          </h3>
        </div>
      </div>
    </div>
    <div class="m-portlet__body">
      <table width="100%" class="table m-table nowrap table-header-fixed table-striped table-bordered table-hover table-header-fixed" id="loan_providers_view_table">
        <thead>
          <tr class="">
            <th>ID</th>
            <th>Provider Name</th>
            <th>Total Loan Amount</th>
            <th>Op Payable Balance</th>
            <th>Total Paid</th>
            <th>Balance Payable</th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>

    </div>
  </div>
</div>

<div class="modal fade" tabindex="-1" id="loan_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 Loan
        </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_loan_add" data-modal-id="loan_add_modal" method="post" action="<?php echo base_url().'loans/create_loan'?>" 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_loan_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-5">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Title
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">
                      <i class="la la-tag"></i>
                    </span>
                  </div>
                  <input required type="text" name="title" placeholder="Title.." class="form-control form_loan_add_fields m-input"  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">
                  Loan Amount
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">
                      <?php echo $this->config->item('Currency_currency_format'); ?>
                    </span>
                  </div>
                  <input required type="text" name="loan_amount"  class="form-control form_loan_add_fields m-input" placeholder="Loan Amount" aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-3">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Loan Date
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <input required type="text" name="loan_date" id="loan_date_add" data-date-format="dd-mm-yyyy" placeholder="Loan Date.." class="form-control form_loan_add_fields m-input"  aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <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--air m-input-group--pill">
                  <textarea name="description" class="form-control form_loan_add_fields m-input " placeholder="Description about Loan" ></textarea>
                </div>
              </div>
              <span class="m-form__help"></span>
            </div>

            <div class="col-lg-6">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Received To Account (Credit Acc)
                </label>
                <div class="m-select2 m-select2--air m-select2--pill">
                  <select required id="loan_received_acc_add" name="credit_acc" style="width: 100%" class="form-control m-input form_loan_add_select"  aria-describedby="basic-addon1">
                    <?php
                    foreach($this->config->item('account_heads') as $row):
                      $selected="";
                      if($row->acc_head_id==1){
                        $selected="selected";
                      }
                      ?>
                      <option <?php echo $selected?> value="<?php echo $row->acc_head_id?>"><?php echo $row->acc_head_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">
            Current Payable Balance
          </label>
          <div class="input-group m-input-group--air m-input-group--pill">
          <div class="input-group-prepend">
          <span class="input-group-text">
          <?php echo $this->config->item('Currency_currency_format'); ?>
        </span>
      </div>
      <input  required type="text" name="payable_balance" class="form-control form_loan_add_fields m-input " placeholder="Payable Balance" >
    </div>
  </div>
  <span class="m-form__help"></span>
</div> -->

<div class="col-lg-6">
  <div class="form-group m-form__group" >
    <label for="user_name">
      Provider Name
    </label>
    <div class="input-group m-input-group m-input-group--pill">
      <div class="input-group-prepend" id='suppler_add_btn'>
        <a href="#supplier_add_modal"   data-toggle="modal" data-target="#provider_add_modal" class="btn btn-focus">
          <i class="fa fa-plus-circle"></i>
        </a>
      </div>
      <select style="width: 80%" class="form-control m-select2 m-select2-general" required id="provider_add" name="provider" data-placeholder="Select Provider">

      </select>
    </div>
    <span class="m-form__help"></span>
  </div>
</div>
<div class="col-lg-6">
  <div class="form-group m-form__group" style="  !important">
    <label for="user_name">
      Loan Purpose
    </label>
    <div class="m-select2 m-select2--air m-select2--pill">
      <select  style="width: 100%" class="form-control m-select2 form_loan_add_select" id="work_id_loan_add"  name="work_id" data-placeholder="Select Purpose">
        <option selected value="0">For Whole Business</option>
        <?php
        foreach($work_contracts as $row): ?>
        <option data-work-status="<?php echo $row->work_status?>" value="<?php echo $row->work_id?>">For Project : <?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>
<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="loan_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 Loan Details
        </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_loan_edit" data-modal-id="loan_edit_modal" method="post" action="<?php echo base_url().'loans/update_loan'?>" 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_loan_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-5">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Title
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">
                      <i class="la la-tag"></i>
                    </span>
                  </div>
                  <input required type="text" name="title" id="title_edit" placeholder="Title.." class="form-control form_loan_edit_fields m-input"  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">
                  Loan Amount
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">
                      <?php echo $this->config->item('Currency_currency_format'); ?>
                    </span>
                  </div>
                  <input required type="text" name="loan_amount" id="loan_amount_edit"  class="form-control form_loan_edit_fields m-input" placeholder="Loan Amount" aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <div class="col-lg-3">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Loan Date
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <input required type="text" name="loan_date" id="loan_date_edit" data-date-format="dd-mm-yyyy" placeholder="Loan Date.." class="form-control form_loan_edit_fields m-input"  aria-describedby="basic-addon1">
                </div>
                <span class="m-form__help"></span>
              </div>
            </div>
            <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--air m-input-group--pill">
                  <textarea name="description" id="description_edit" class="form-control form_loan_edit_fields m-input " placeholder="Description about Loan" ></textarea>
                </div>
              </div>
              <span class="m-form__help"></span>
            </div>
            <div class="col-lg-6">
              <div class="form-group m-form__group" >
                <label for="user_name">
                  Received To Account (Credit Acc)
                </label>
                <div class="m-select2 m-select2--air m-select2--pill">
                  <select required id="loan_received_acc_edit" name="credit_acc" style="width: 100%" class="form-control m-input form_loan_edit_select"  aria-describedby="basic-addon1">
                    <?php
                    foreach($this->config->item('account_heads') as $row):
                      $selected="";
                      if($row->acc_head_id==1){
                        $selected="selected";
                      }
                      ?>
                      <option <?php echo $selected?> value="<?php echo $row->acc_head_id?>"><?php echo $row->acc_head_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">
            Current Payable Balance
          </label>
          <div class="input-group m-input-group--air m-input-group--pill">
          <div class="input-group-prepend">
          <span class="input-group-text">
          <?php echo $this->config->item('Currency_currency_format'); ?>
        </span>
      </div>
      <input  required type="text" name="payable_balance" id="payable_balance_edit" class="form-control form_loan_edit_fields m-input " placeholder="Payable Balance" >
    </div>
  </div>
  <span class="m-form__help"></span>
</div> -->

<div class="col-lg-6">
  <div class="form-group m-form__group" >
    <label for="user_name">
      Provider Name
    </label>
    <div class="input-group m-input-group m-input-group--pill">
      <div class="input-group-prepend" id='suppler_add_btn'>
        <a href="#supplier_add_modal"   data-toggle="modal" data-target="#provider_add_modal" class="btn btn-focus">
          <i class="fa fa-plus-circle"></i>
        </a>
      </div>
      <select style="width: 80%" class="form-control m-select2 m-select2-general" required id="provider_edit" name="provider" data-placeholder="Select Provider">

      </select>
    </div>
    <span class="m-form__help"></span>
  </div>
</div>
<div class="col-lg-6">
  <div class="form-group m-form__group" style="  !important">
    <label for="user_name">
      Loan Purpose
    </label>
    <div class="m-select2 m-select2--air m-select2--pill">
      <select  style="width: 100%" class="form-control m-select2 form_loan_edit_select" id="work_id_loan_edit"  name="work_id" data-placeholder="Select Purpose">
        <option selected value="0">For Whole Business</option>
        <?php
        foreach($work_contracts as $row): ?>
        <option data-work-status="<?php echo $row->work_status?>" value="<?php echo $row->work_id?>">For Project : <?php echo $row->work_name?></option>
        <?php
      endforeach;
      ?>
    </select>
  </div>
  <span class="m-form__help"></span>
</div>
</div>
<input type="hidden" name="loan_id" id="loan_id_edit" >
<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 form_submit_btn btn-primary">
    Submit
  </button>
</div>
</form>
</div>
</div>
</div>
<div class="modal fade" tabindex="-1" id="loan_delete_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog " role="document" >
    <form autocomplete="off" id="form_loan_delete" data-modal-id="loan_delete_modal"  method="post" action="<?php echo base_url().'loans/delete_loan'?>" 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="loan_id" id="loan_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="provider_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" >
          Create New Loan Provider
        </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_provider_add" data-form-type="provider_create" data-modal-id="provider_add_modal" method="post" action="<?php echo base_url().'loans/create_provider'?>" 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_provider_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">
                  Loan Provider Name
                </label>
                <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                  <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">
                      <i class="la la-tag"></i>
                    </span>
                  </div>
                  <input required type="text" name="name"  class="form-control m-input form_provider_add_fields" placeholder="Provider Name" aria-describedby="basic-addon1">
                </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>

<?php $this->view("modules/include_modals/modal_bills");?>
<?php $this->view("modules/include_modals/modal_transaction_delete");?>

</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/bill_scripts");?>
<?php $this->view("modules/include_scripts/transaction_scripts");?>
<script>

$("#table_filter_loan_purpose,#table_filter_loan_provider").select2({
  placeholder: "Select Option"
})
$("#loan_received_acc_add").select2({
  placeholder: "Select Credit Account",
  dropdownParent: $("#form_loan_add")
})
$("#work_id_loan_add").select2({
  placeholder: "Select Loan Purpose",
  dropdownParent: $("#form_loan_add")
})

$("#loan_received_acc_edit").select2({
  placeholder: "Select Credit Account",
  dropdownParent: $("#form_loan_edit")
})
$("#work_id_loan_edit").select2({
  placeholder: "Select Loan Purpose",
  dropdownParent: $("#form_loan_edit")
})

$("#loans_menu").addClass('m-menu__item--active');

$("#provider_add").select2({
  dropdownParent: $("#form_loan_add"),
  placeholder: "Select Provider"
})
$("#provider_edit").select2({
  dropdownParent: $("#form_loan_edit"),
  placeholder: "Select Provider"
})
function select_provider(select_id,selected_value='',type=''){
  $('#'+select_id).html('');
  if(type=='filter'){
    $('#'+select_id).append("<option value='all' selected >All Providers</option>");
  }else{
    $('#'+select_id).append("<option disabled selected >Select Provider</option>");
  }
  $.post("<?php echo base_url() ?>loans/select_provider",{},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count>0) {
      while (i < obj.data.length) {
        $('#'+select_id).append("<option value=" + obj.data[i]['loan_provider_id'] + ">" +obj.data[i]['loan_provider_name'] + "</option>");
        i++;
      }
      if(selected_value!=''){
        $('#'+select_id).val(selected_value).trigger("change");
      }
    }
  })
}
select_provider('provider_add');
select_provider('provider_edit');
select_provider('table_filter_loan_provider','','filter');

$("#loan_date_add,#loan_date_edit").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 } ?>
  })
  $('body').on('click', '#loan_edit_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#loan_id_edit').val(no.eq(1).text());
    $('#title_edit').val(no.eq(2).text());
    $('#loan_amount_edit').val(no.eq(6).text());
    $('#description_edit').val(no.eq(3).text());
    $('#payable_balance_edit').val(no.eq(7).text());
    $("#loan_received_acc_edit").val(no.eq(14).text()).trigger('change');
    $("#work_id_loan_edit").val(no.eq(15).text()).trigger('change');
    $('#loan_date_edit').datepicker("setDate", no.eq(4).text());
    select_provider('provider_edit',no.eq(16).text());
  })
  $('body').on('click', '#loan_delete_btn', function() {
    var no = $(this).closest('tr').children('td');
    $('#loan_id_delete').val(no.eq(1).text());
  })

  //  client List Data Table
  var Datatable=function() {
    var t=function() {

      function GetSearchProvider(){
        return $('#table_filter_loan_provider').val();
      }
      function GetSearchPurpose(){
        return $('#table_filter_loan_purpose').val();
      }
      function GetSearchClient(){
        return $('#clients').val();
      }
      function GetSearchDate(){
        return $('#date_range').val();
      }
      function GetSearchwork(){
        return $('#work').val();
      }
      var loans_view_table = $('#loans_view_table').DataTable({
        "ajax":{
          url :"<?php echo base_url().'loans/select_loans' ?>", // json datasource
          type: "post", // type of method  , by default would be get
          data: function(d){
            d.loan_purpose = GetSearchPurpose();
            d.loan_provider = GetSearchProvider();
          }
        },
        "bProcessing": true,
        "deferRender": true,
        "bDeferRender": true,
        "scrollX" : true,
        buttons: [
          { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Loans' },
          { 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,7,14,15,16<?php if($this->session->userdata('user_role')=='auditor' && $this->session->userdata('user_data_permission')['adding']!=1) { echo ",13"; } ?> ],
            className: "hidden"
          },
          {
            render: function (data, type, full, meta) {
              return "<div class='text-wrap width-400'>" + data + "</div>";
            },
            targets: 3
          }
        ],
        'order': false
      });
      var loan_payments_view_table = $('#loan_payments_view_table').DataTable({
        "ajax":{
          url :"<?php echo base_url().'accounts/select_account_transactions/loan' ?>", // json datasource
          type: "post", // type of method  , by default would be get
          data: function(d){
            d.date = GetSearchDate();
            d.loan_purpose = GetSearchPurpose();
            d.loan_provider = GetSearchProvider();
          }
        },
        "bProcessing": true,
        "deferRender": true,
        "bDeferRender": true,
        "scrollX" : true,
        buttons: [
          { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Loan Payments' },
          { 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": [3,6,7,8,9,19<?php if($page_name=='reports_activity'){ echo ",18"; }?><?php if($this->session->userdata('user_role')=='auditor') { echo ",18"; } ?>],
            className: "hidden"
          }
        ],
        'order': false
      });
      var loan_providers_view_table = $('#loan_providers_view_table').DataTable({
        "ajax":{
          url :"<?php echo base_url().'loans/select_loan_providers' ?>", // json datasource
          type: "post", // type of method  , by default would be get
          data: function(d){
          }
        },
        "bProcessing": true,
        "deferRender": true,
        "bDeferRender": true,
        "scrollX" : true,
        buttons: [
          { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'} ,title: 'Loan Providers'},
          { 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": [ 3 ],
            className: "hidden"
          },
        ],
        'order': false
      });
      $(".table_filter").on("change", function() {
        loan_payments_view_table.ajax.reload();
        loans_view_table.ajax.reload();
        loan_providers_view_table.ajax.reload();
      });
      $(".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'));
        loan_payments_view_table.ajax.reload();
        loans_view_table.ajax.reload();
        loan_providers_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: {
            payable_balance: {
              number: !0,
              required: !0
            },
            paid_amount: {
              number: !0,
              required: !0
            },
            expense_amount: {
              number: !0,
              required: !0
            },
            received_amount: {
              number: !0,
              required: !0
            },
            budget: {
              number: !0,
              required: !0
            },
            edit_paid_amount: {
              number: !0,
              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[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
              $("#"+modal_id).modal('toggle');
              $("."+form_id+"_fields").val('');
              $("."+form_id+"_select").val('').trigger("change");
              loan_payments_view_table.ajax.reload();
              loans_view_table.ajax.reload();
              loan_providers_view_table.ajax.reload();
              select_provider('provider_add');
              select_provider('provider_edit');
              select_provider('table_filter_loan_provider');
            } 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");
          }
        })
      )
    });
  };
  return {
    init:function() {
      t()
    }
  }
}

();
jQuery(document).ready(function() {
  Datatable.init();
  $(".hidden").hide();
  //            $('#service_view_table').wrap("");
}

);
</script>
<style>
.hidden{
  display: none;
}
</style>
