<?php $this->load->view("common/header"); ?>

<body>
  <div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
      <div class="navbar-header">
        <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button"> <span class="clip-list-2"></span> </button>
        <?php $this->load->view("common/logo"); ?>
      </div>
      <div class="navbar-tools">
        <?php $this->load->view("common/notifications.php"); ?>
      </div>
    </div>
  </div>
  <div class="main-container">
    <div class="navbar-content">
      <?php $this->load->view("common/navigation"); ?>
    </div>
    <div class="main-content">
      <div class="container">
        <div class="row">
          <div class="col-sm-12">
            <ol class="breadcrumb">
              <li> <a href="<?php echo base_url('dashboard'); ?>"> Dashboard </a> </li>
              <li> <a href="#"> Reports </a> </li>
              <li class="active"> Cash Report </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">
              <h1>Collection Officer's Report</h1>
            </div>
            <p>Please use the table below to navigate or filter the results.</p>
          </div>
        </div>
        <div class="row">
          <div class="col-md-12">
            <div class="panel panel-default">
              <div class="panel-heading"> <i class="fa fa-external-link-square"></i> Collection Officer's Report
                <div class="panel-tools" style="top:2px;"></div>
              </div>
              <div class="panel-body">
                <div id="error"></div>
                <div class="col-md-12">
                  <div class="panel panel-default">
                    <div style="font-weight: 700;" class="panel-heading"></div>
                    <div class="panel-body">
                      <form method="post" action="<?php echo base_url('reports_loan/collection_report'); ?>">
                        <div class="col-sm-3">
                          <div class="form-group">
                            <label>Collectors </label>
                            <select id="user_id" class="form-control" name="user_id">
                              <option value="">All Collectors</option>
                              <?php foreach ($user_list as $row): ?>
                                <option value="<?php echo $row->user_id; ?>" <?php echo $row->user_id == $selected_collector ? 'selected' : ''; ?>>
                                  <?php echo $row->user_first_name . " - " . $row->user_group_name; ?>
                                </option>
                              <?php endforeach; ?>
                            </select>
                          </div>
                        </div>
                        <div class="col-sm-3">
                          <div class="form-group">
                            <label>Date </label>
                            <input id="srh_from_date" name="srh_from_date" type='text' class="form-control date" value="<?php echo $selected_date; ?>" data-bv-field="date" />
                          </div>
                        </div>
                        <div class="col-sm-4 pull-right">
                          <div class="form-group">
                            <label for="s2id_autogen1">&nbsp;<br><br></label>
                            <input type="submit" name="add_category" value="Search" class="btn btn-primary">
                            <input type="submit" name="add_category" value="Reset" class="btn btn-danger" onclick="searchDetailsReset()">
                          </div>
                        </div>
                      </form>
                    </div>
                  </div>
                  <div id="printableArea">
                    <div class="page-header">
                      <table class="table table-bordered table-condensed table-striped table-hover dataTable">
                        <tbody>
                          <tr>
                            <td>
                              <h4>Collection Report Summary</h4> [ Route - <span id="route_display"><?php echo $report ? $report[0]['route_id'] : 'N/A'; ?></span> |
                              User - <span id="user_display"><?php echo $selected_collector && isset($user_list[array_search($selected_collector, array_column($user_list, 'user_id'))]) ? $user_list[array_search($selected_collector, array_column($user_list, 'user_id'))]->user_first_name : 'All Collectors'; ?></span> |
                              Date: <span id="f-date-txt"><?php echo $selected_date; ?></span>]
                            </td>
                          </tr>
                          <tr>
                            <td width="25%"><strong>Total Collection</strong></td>
                            <td width="25%"><strong><span id="total_collection">Rs. <?php echo number_format($summary['total_paid'], 2); ?></span></strong></td>
                          </tr>
                          <tr>
                            <td width="25%">Penalty Collection</td>
                            <td width="25%"><span id="penelty_collection">Rs. <?php echo number_format($summary['panalty_received'], 2); ?></span></td>
                          </tr>
                        </tbody>
                      </table>
                    </div>
                  </div>


                  <table class="table table-bordered table-condensed table-hover dataTable" id="warehouse_table">
                    <thead>
                      <tr>
                        <th>#</th>
                        <th>Loan Number</th>
                        <th>Customer Name</th>
                        <th class="text-right">Term Amount</th>
                        <th class="text-right">Paid Amount</th>
                        <th>No Payment Reason</th>
                      </tr>
                    </thead>
                    <tbody>
                      <?php if ($report): ?>
                        <?php $i = 1;
                        foreach ($report as $row): ?>
                          <tr>
                            <td><?php echo $i++; ?></td>
                            <td><?php echo $row['loan_number']; ?></td>
                            <td><?php echo $row['customer_name'] ?: 'N/A'; ?></td>
                            <td class="text-right"><?php echo number_format($row['term_value'], 2); ?></td>
                            <td class="text-right"><?php echo number_format($row['paid_amount'], 2); ?></td>
                            <td><?php echo $row['not_collected_reason']; ?></td>
                          </tr>
                        <?php endforeach; ?>
                      <?php else: ?>
                        <tr>
                          <td colspan="10">No terms due for collection on the selected date.</td>
                        </tr>
                      <?php endif; ?>
                    </tbody>
                    <tfoot>
                      <tr>
                        <th colspan="3" class="text-right">Total</th>
                        <th class="text-right">Rs. <?php echo number_format(array_sum(array_column($report, 'term_value')), 2); ?></th>
                        <th class="text-right">Rs. <?php echo number_format(array_sum(array_column($report, 'paid_amount')), 2); ?></th>
                        <th colspan="1"></th>
                      </tr>
                    </tfoot>
                  </table>


                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="footer clearfix">
      <div class="footer-inner"> <?php echo date("Y"); ?> &copy; A.S.S.K Loan Management System </div>
      <div class="footer-items"> <span class="go-top"><i class="clip-chevron-up"></i></span> </div>
    </div>
    <?php $this->load->view("common/footer"); ?>
    <script src="<?php echo asset_url(); ?>js/jquery-ui.js"></script>
    <script src="<?php echo asset_url(); ?>js/moment-with-locales.js"></script>
    <script type="text/javascript" src="<?php echo asset_url(); ?>js/bootstrap-datetimepicker.js"></script>
    <script type="text/javascript" src="<?php echo asset_url(); ?>js/bootbox.min.js"></script>
    <script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.js"></script>
    <script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.min.js"></script>
    <script type="text/javascript" src="<?php echo asset_url(); ?>js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="<?php echo asset_url(); ?>js/dataTables.bootstrap.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
    <script>
      function searchDetailsReset() {
        $('#srh_from_date').val('');
        $('#user_id').val('');
        window.location.reload();
      }

      function searchDetails() {
        $('#warehouse_table').DataTable({
          dom: 'Blfrtip',
          buttons: [{
            extend: 'print',
            text: 'Print',
            title: 'Collection Report Summary',
            footer: true,
            customize: function(win) {
              $(win.document.body).find('#printableArea').prependTo($(win.document.body));
            }
          }],
          "data": <?php echo json_encode($report); ?>,
          "columns": [{
              "data": null,
              "render": function(data, type, row, meta) {
                return meta.row + 1;
              }
            },
            {
              "data": "user"
            },
            {
              "data": "term_date"
            },
            {
              "data": "loan_number"
            },
            {
              "data": "customer_name",
              "render": function(data) {
                return data || 'N/A';
              }
            },
            {
              "data": "term_no",
              "render": function(data) {
                return 'Term ' + data;
              }
            },
            {
              "data": "term_value",
              "render": function(data) {
                return accounting.formatMoney(data, "Rs. ", 2, ",", ".");
              }
            },
            {
              "data": "previous_outstanding",
              "render": function(data) {
                return accounting.formatMoney(data, "Rs. ", 2, ",", ".");
              }
            }
          ],
          "bDestroy": true,
          "iDisplayLength": 20,
          "order": [
            [2, "desc"]
          ],
          "footerCallback": function(row, data, start, end, display) {
            var api = this.api();
            var total = api.column(6).data().reduce(function(a, b) {
              return a + parseFloat(b);
            }, 0);
            $(api.column(6).footer()).html('Rs. ' + accounting.formatMoney(total, "", 2, ",", "."));
          }
        });

        $('#f-date-txt').text($('#srh_from_date').val());
        $('#user_display').text($('#user_id option:selected').text() || 'All Collectors');
        $('#route_display').text(<?php echo json_encode($report[0]['route_id'] ?? 'N/A'); ?>);
        $('#total_collection').text('Rs. ' + accounting.formatMoney(<?php echo $summary['total_paid']; ?>, "", 2, ",", "."));
        $('#penelty_collection').text('Rs. ' + accounting.formatMoney(<?php echo $summary['panalty_received']; ?>, "", 2, ",", "."));
      }

      jQuery(document).ready(function() {
        $('#srh_from_date').datetimepicker({
          defaultDate: new Date(),
          format: "YYYY-MM-DD"
        });
        <?php if ($report): ?>
          searchDetails();
        <?php endif; ?>
      });

      $(document).on('click', 'tr', function(event) {
        $('tr').css('background-color', 'inherit');
        $(this).css('background-color', '#0F6');
      });
    </script>
</body>

</html>