<style type="text/css">
  .fa-3x {
    font-size: 2em !important;
  }

  .report_view_th {
    background-color: #428bca;
    color: #fff !important;
    font-size: 14px;
  }

  .table-responsive td {
    font-size: 14px;
  }

  h4 {
    font-size: 13px;
  }
</style>

<style>
  .upload-box {
    border: 2px dashed #ccc;
    border-radius: 8px;
    padding: 15px;
    width: 250px;
    text-align: center;
    margin: 10px;
    display: inline-block;
    vertical-align: top;
  }

  .upload-box img {
    max-width: 100%;
    max-height: 150px;
    margin-bottom: 10px;
  }

  .progress-bar {
    height: 6px;
    background: #4caf50;
    width: 0%;
    transition: width 0.3s;
  }

  .file-name {
    font-size: 12px;
    color: #555;
    margin-top: 5px;
  }
</style>

<style>
  .upload-box {
    border: 2px dashed #ccc;
    border-radius: 8px;
    padding: 15px;
    width: 225px;
    text-align: center;
    margin: 10px;
    display: inline-block;
    vertical-align: top;
  }

  .upload-box img {
    max-width: 100%;
    max-height: 150px;
    margin-bottom: 10px;
  }

  .progress-bar {
    height: 6px;
    background: #4caf50;
    width: 0%;
    transition: width 0.3s;
  }

  .file-name {
    font-size: 12px;
    color: #555;
    margin-top: 5px;
  }

  .upload-box .img-thumb {
    max-width: 100px;
    max-height: 100px;
    border-radius: 5px;
    border: 1px solid #ddd;
  }

  .upload-box .pdf-thumb {
    width: 60px;
    height: 60px;
  }
</style>
<style>
  .preview img {
    width: 150px;
    border: 1px solid #ddd;
    margin: 5px;
  }

  .upload-box {
    margin-bottom: 15px;
  }

  #uploadContainer {
    margin-top: 15px;
    ;
  }
</style>
<?php $this->load->view("common/header"); ?>
<!-- end: HEAD -->
<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
<!-- end: HEAD -->
<!-- start: BODY -->

<body>
  <!-- start: HEADER -->
  <div class="navbar navbar-inverse navbar-fixed-top">
    <!-- start: TOP NAVIGATION CONTAINER -->
    <div class="container">
      <div class="navbar-header">
        <!-- start: RESPONSIVE MENU TOGGLER -->
        <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button"> <span class="clip-list-2"></span> </button>
        <!-- end: RESPONSIVE MENU TOGGLER -->
        <!-- start: LOGO -->
        <?php $this->load->view("common/logo"); ?>
        <!-- end: LOGO -->
      </div>
      <div class="navbar-tools">
        <!-- start: TOP NAVIGATION MENU -->
        <?php $this->load->view("common/notifications.php"); ?>
        <!-- end: TOP NAVIGATION MENU -->
      </div>
    </div>
    <!-- end: TOP NAVIGATION CONTAINER -->
  </div>
  <!-- end: HEADER -->
  <!-- start: MAIN CONTAINER -->
  <div class="main-container">
    <div class="navbar-content">
      <!-- start: SIDEBAR -->
      <?php $this->load->view("common/navigation"); ?>
      <!-- end: SIDEBAR -->
    </div>
    <!-- start: PAGE -->
    <div class="main-content">
      <!-- end: SPANEL CONFIGURATION MODAL FORM -->
      <div class="container">
        <!-- start: PAGE HEADER -->
        <div class="row">
          <div class="col-sm-12">
            <!-- start: PAGE TITLE & BREADCRUMB -->
            <ol class="breadcrumb">
              <li> <a href="<?php echo base_url('dashboard'); ?>"> Dashboard </a> </li>
              <li> <a href="<?php echo base_url('loan'); ?>"> List Loan </a> </li>
              <li class="active"> View </li>
              <li class="search-box">
                <form class="sidebar-search">
                  <div class="form-group">
                    <input type="text" placeholder="Start Searching...">
                    <button class="submit"> <i class="clip-search-3"></i> </button>
                  </div>
                </form>
              </li>
            </ol>
            <div class="page-header">
              <?php
              $message = $this->session->flashdata('message');
              if ($message) { ?>
                <div class="alert alert-success">
                  <button type="button" class="close" data-dismiss="alert">×</button>
                  <?php echo $message ?>
                </div>
              <?php
              }
              ?>
            </div>
          </div>
        </div>
        <!-- end: PAGE HEADER -->
        <!-- start: PAGE CONTENT 
                    <!-- start grid -->
        <div class="row">
          <div class="col-md-12">
            <!-- start: DYNAMIC TABLE PANEL -->
            <div class="panel panel-default">
              <div class="panel-heading"> <i class="fa fa-external-link-square"></i> Reference ID <?php echo $loan_id ?>
                <div class="panel-tools" style="top:2px;">
                  <?php if ($this->session->userdata('ss_group_id') == 1 && $loan_details->approval_status == 0) { ?>
                    <button class="btn btn-blue dropdown-toggle" data-toggle="dropdown"> <i class="clip-list-5"></i> Modify Loan </button>

                  <?php } ?>
                  <ul class="dropdown-menu dropdown-light pull-right">
                    <li> <a id="" data-toggle="modal" href="<?php echo base_url("loan/loan_modify/$loan_id"); ?>"> <i class="fa fa-plus"></i> Modify this Loan Details </a> </li>
                  </ul>
                </div>
                <!--panel-tools-->
              </div>
              <!--panel-heading-->
              <div class="panel-body">
                <div class="well well-sm">
                  <div class="col-xs-4 border-right">
                    <div class="col-xs-1"><i class="fa fa-2x fa-file-text-o  text-muted"></i></div>
                    <div class="col-xs-10">
                      <h4 class="">Reference ID : <?php echo $loan_details->loan_ref; ?></h4>
                      <p>Created Date: <?php echo $loan_details->loan_creted_date; ?></p>
                      <p>Transaction date : <?php echo $loan_details->loan_date; ?></p>
                      <?php if ($loan_details->loan_payment_term == 1) { ?>
                        <p>Due date : <?php echo $loan_details->loan_close_date; ?></p>
                      <?php } ?>
                      <p style="display: none;">Total Holidays : <?php echo $holiday_count; ?> ( Include : <?php echo $include_holi_date; ?> Extend : <?php echo $addition_holi_date; ?> )</p>



                    </div>
                    <div class="clearfix"></div>
                  </div>
                  <div class="col-xs-4 border-right">
                    <div class="col-xs-1"><i class="fa fa-2x fa-file-text-o padding010 text-muted"></i></div>
                    <div class="col-xs-10">
                      <h4>Loan amount : Rs. <?php echo number_format($loan_details->loan_tot_amount, 2, '.', ','); ?></h4>
                      <p>Payment Term : <?php echo $loan_details->payment_term_name; ?> </p>
                      <p>Interest : <?php echo $loan_details->loan_interest; ?> %</p>
                      <?php if ($loan_details->loan_payment_term == 1) { ?>
                        <p style="display:none">Total Interest : Rs. <?php echo $loan_details->total_interest; ?> </p>
                      <?php } else { ?>
                        <p style="display:none">Total Interest : Rs. <?php echo number_format($loan_details->loan_tot_amount - $loan_details->loan_payable_total_amount, 2, '.', ',') ?> </p>
                      <?php } ?>
                      <p>Pay amount : Rs. <?php echo number_format($loan_details->loan_payable_total_amount, 2, '.', ',') ?></p>
                      <p>No. of Terms : <?php echo $loan_details->loan_no_of_terms; ?></p>
                      <?php if ($loan_details->loan_payment_term == 1) { ?>
                        <p><strong>Term Amount : Rs. <?php echo number_format($loan_details->loan_term_amount, 2, '.', ','); ?></strong></p>
                      <?php } ?>
                    </div>
                    <div class="clearfix"></div>
                  </div>
                  <div class="col-xs-4 border-right">
                    <div class="col-xs-1"><i class="fa fa-2x fa-user padding010 text-muted"></i></div>
                    <div class="col-xs-10">
                      <h4 class=""><?php echo $customer_details['business_name']; ?></h4>
                      <p><?php echo $customer_details['registered_address']; ?></p>
                      <p>Tel: <?php echo $customer_details['contact_number']; ?> </p>

                      <?php if ($spouse_info) { ?>
                        <p>Garanter: </p>
                        <p>Name: <?php echo $spouse_info['spouse_name']; ?></p>
                        <p>Address: <?php echo $spouse_info['spouse_em_address']; ?></p>
                        <p>Phone Number : <?php echo $spouse_info['spouse_contact']; ?></p>
                      <?php } else if ($guarantor_info) { ?>
                        <p>Garanter: </p>
                        <p>Name: <?php echo $guarantor_info['guarantor_name']; ?></p>
                        <p>Address: <?php echo $spouse_info['guarantor_address']; ?></p>
                        <p>Phone Number : <?php echo $spouse_info['guarantor_contact']; ?></p>
                      <?php } ?>

                    </div>
                    <div class="clearfix"></div>
                  </div>
                  <div class="clearfix"></div>
                </div>

                <!--col-xs-4-->

                <div class="col-xs-5">
                  <input name="loan_id" type="hidden" id="loan_id" value="<?php echo $loan_id ?>">
                  <input type="hidden" id="sale_type" name="sale_type" value="sale">
                  <div class="clearfix"></div>
                </div>
                <!--col-xs-4"-->
                <div class="clearfix"></div>
                <div class="clearfix"></div>

                <!--  start -->

                <!--  start -->

                <div class="well well-sm">
                  <div class="col-xs-12 border-right">
                    <div class="col-xs-1"><i class="fa fa-2x fa-text-o text-muted"></i></div>
                    <div class="col-xs-10">
                      <h4 class=""> Note </h4>
                      <p> <?php echo $loan_details->note; ?> </p>


                      <hr>
                      <p style="color:#D00"> <?php echo $loan_details->close_reason; ?> </p>
                    </div>
                    <div class="clearfix"></div>
                  </div>


                  <div class="clearfix"></div>
                </div>
                <div class="clearfix"></div>

                <!-- DOCUMENTS -->
                <div class="tab-pane" id="documents">
                  <div class="panel panel-default">
                    <div class="panel-heading"><strong>Documents</strong></div>
                    <div class="panel-body">

                      <?php if (!empty($documents)): ?>
                        <div class="row">
                          <?php foreach ($documents as $doc): ?>
                            <?php
                            $filePath = base_url($doc->file_path . $doc->file_name);
                            //$filePath = $doc->file_path . $doc->file_name;
                            $ext = strtolower(pathinfo($doc->file_name, PATHINFO_EXTENSION));
                            $isImage = in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']);
                            ?>
                            <div class="col-sm-4" style="margin-bottom:15px;">
                              <div class="panel panel-info">
                                <div class="panel-heading">
                                  <?= htmlspecialchars($doc->doc_type) ?>
                                </div>
                                <div class="panel-body" style="text-align:center;">
                                  <?php if ($isImage): ?>
                                    <a href="<?= $filePath ?>" target="_blank">
                                      <img src="<?= $filePath ?>" alt="<?= htmlspecialchars($doc->doc_type) ?>"
                                        class="img-responsive img-thumbnail" style="max-height:200px; margin:auto;">
                                    </a>
                                  <?php else: ?>
                                    <p><i class="glyphicon glyphicon-file" style="font-size:40px;"></i></p>
                                    <a href="<?= $filePath ?>" target="_blank"><?= htmlspecialchars($doc->file_name) ?></a>
                                  <?php endif; ?>
                                </div>
                              </div>
                            </div>
                          <?php endforeach; ?>
                        </div>
                      <?php else: ?>
                        <p>No documents uploaded.</p>
                      <?php endif; ?>
                    </div>


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


              <!------------------------------------------------------------------------------------------->


              <div class="clearfix"></div>
            </div>






            <!-- end-->

            <!-- payment list -->
            <div style="display: block;" class="col-xs-12 border-right">
              <h4>Terms List</h4>
            </div>
            <div style="display: block;" class="row">
              <div class="col-xs-12">
                <div class="table-responsive">
                  <table class="table table-bordered" id="loan_terms_table">
                    <thead>
                      <tr>
                        <th>Term No</th>
                        <th>Term Date</th>
                        <th>Realize Date</th>
                        <th>Term Amount</th>
                        <th>Paid Amount</th>
                        <th>Penalty</th>
                        <th>Status</th>
                        <th>Payment Date</th>
                        <th>Days</th>
                        <th>Delay Charges</th>
                      </tr>
                    </thead>
                    <tbody>
                      <?php
                      $total_charges = 0; // initialize total
                      if (!empty($loan_terms)) {
                        foreach ($loan_terms as $term) {
                          $total_charges += $term->delay_charge; // add each delay charge
                      ?>
                          <tr>
                            <td><?= $term->term_no ?></td>
                            <td><?= !empty($term->term_date) ? date("Y-m-d", strtotime($term->term_date)) : '-' ?></td>
                            <td><?= !empty($term->realize_date) ? date("Y-m-d", strtotime($term->realize_date)) : '-' ?></td>
                            <td><?= number_format($term->term_amount, 2) ?></td>
                            <td><?= number_format($term->paid_amount, 2) ?></td>
                            <td><?= number_format($term->penalty_amount, 2) ?></td>
                            <td>
                              <?php if ($term->status == "Pending") { ?>
                                <span class="label label-warning">Pending</span>
                              <?php } elseif ($term->status == "Paid") { ?>
                                <span class="label label-success">Paid</span>
                              <?php } elseif ($term->status == "Not Collected") { ?>
                                <span class="label label-info">Not Collected</span>
                              <?php } else { ?>
                                <span class="label label-danger">Colsed</span>
                              <?php } ?>
                            </td>
                            <td><?= !empty($term->payment_date) ? date("d-m-Y", strtotime($term->payment_date)) : '-' ?></td>
                            <td><?= $term->different_days ?></td>
                            <td><?= number_format($term->delay_charge, 2) ?></td>
                          </tr>
                        <?php }
                      } else { ?>
                        <tr>
                          <td colspan="11" class="text-center">No loan terms found.</td>
                        </tr>
                      <?php } ?>
                    </tbody>
                    <?php if (!empty($loan_terms)) { ?>
                      <tfoot>
                        <tr>
                          <td colspan="9" class="text-right"><strong>Total Delay Charges:</strong></td>
                          <td><strong><?= number_format($total_charges, 2) ?></strong></td>
                        </tr>
                      </tfoot>
                    <?php } ?>
                  </table>


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

            <div class="col-md-12" style="margin-right:2%;">
              <div class="form-group"><br>
                Customer Loan History Summary
                <div class="panel-body">
                  <div id="error"></div>
                  <table class="table table-bordered table-condensed table-hover table-striped dataTable" id="products_table">
                    <thead>
                      <tr>
                        <th></th>
                        <th>Loan No.</th>
                        <th>Loan Date</th>
                        <th>Payment Term</th>
                        <th>No. of Terms</th>
                        <th>Term Amount </th>
                        <th>Status</th>
                        <th>Amount</th>
                        <th>Paid</th>
                        <th>Balance</th>
                      </tr>
                    </thead>
                    <tfoot>
                      <tr>
                        <th></th>
                        <th>Loan No.</th>
                        <th>Loan Date</th>
                        <th>Payment Term</th>
                        <th>No. of Terms</th>
                        <th>Term Amount </th>
                        <th>Status</th>
                        <th>Amount</th>
                        <th>Paid</th>
                        <th>Balance</th>
                      </tr>
                    </tfoot>
                  </table>
                </div>
              </div>
            </div>


            <!-- end payment list-->

            <div class="buttons">
              <div class="btn-group btn-group-justified">
                <div class="btn-group" onClick="fbs_click(<?php echo $loan_id; ?>)"><a title="" class="tip btn btn-primary" data-original-title="Print"><i class="fa fa-print"></i> <span class="hidden-sm hidden-xs">Print</span></a> </div>
              </div>
            </div>
            <!--buttons-->

          </div>
          <!--panel-body-->
        </div>
        <!--panel-->

      </div>
      <!--col-md-12-->
    </div>
    <!--row-->

    <!-- end grid -->

  </div>
  <!-- end: PAGE -->
  </div>
  <!-- end: MAIN CONTAINER -->
  <!-- start: FOOTER -->
  <div class="footer clearfix">
    <div class="footer-inner"> <?php echo date("Y"); ?> &copy; Vehical Maintacne Management System by sallelanka solutions. </div>
    <div class="footer-items"> <span class="go-top"><i class="clip-chevron-up"></i></span> </div>
  </div>
  <!-- end: FOOTER -->
  <!-- start: RIGHT SIDEBAR -->
  <!-- end: RIGHT SIDEBAR -->
  <div id="event-management" class="modal fade" tabindex="-1" data-width="760" style="display: none;">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button>
          <h4 class="modal-title">Event Management</h4>
        </div>
        <div class="modal-body"></div>
        <div class="modal-footer">
          <button type="button" data-dismiss="modal" class="btn btn-light-grey"> Close </button>
          <button type="button" class="btn btn-danger remove-event no-display"> <i class='fa fa-trash-o'></i> Delete Event </button>
          <button type='submit' class='btn btn-success save-event'> <i class='fa fa-check'></i> Save </button>
        </div>
      </div>
    </div>
  </div>
  </div>

  <!-- start ajax model -->
  <div id="ajax-modal" class="modal fade" data-backdrop="static" data-keyboard="false" tabindex="-1" style="display: none;"></div>
  <!-- end ajax model -->

  <!-- start: MAIN JAVASCRIPTS -->
  <?php $this->load->view("common/footer"); ?>
  <!-- end: MAIN JAVASCRIPTS -->

  <!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY -->
  <script>
    jQuery(document).ready(function() {
      load_customer_old_recod();
    });
  </script>
  <script type="text/javascript" language="javascript">

    function load_customer_old_recod(cus_id) {
      $('#products_table').DataTable({
        "ajax": "<?php echo base_url('loan/get_previous_list_loans_by_customer_id?cus_id='.$loan_details->cus_id.'&loan_id='.$loan_details->cus_id) ?>",
        "bDestroy": true,
        "iDisplayLength": 50,
        "order": [
          [0, "desc"]
        ]
      });

    }
    function fbs_click(id, d) {
      u = location.href;
      t = document.title;
      window.open('<?php echo base_url() ?>loan/print_loan_invoice/' + id, 'sharer', 'toolbar=0,status=0,width=700,height=700, left=10, top=10,scrollbars=yes');
      return false;
    }
  </script>
</body>
<!-- end: BODY -->

</html>