<?php include('header.php')?>
<?php
$session = session();
$role = $session->get('role');
$role_type = $session->get('role_type');
?>


<style>
table td {
    word-wrap: break-word !important;
    white-space: normal !important;
}

</style>

        <!-- Page Sidebar Ends-->
        <div class="dashboard-main-body">

        <div class="d-flex flex-wrap align-items-center justify-content-between gap-3 mb-24">
          <h6 class="fw-semibold mb-0">Lease Documents</h6>
          <ul class="d-flex align-items-center gap-2">
            <li class="fw-medium">
              <a href="<?php echo base_url(); ?>" class="d-flex align-items-center gap-1 hover-text-primary">
                <iconify-icon icon="solar:home-smile-angle-outline" class="icon text-lg"></iconify-icon>
                Dashboard
              </a>
            </li>
            <li>-</li>
            <li class="fw-medium">Lease Documents</li>
          </ul>
        </div>

        
<?php
$session = \Config\Services::session();
$db = \Config\Database::connect();
?>
          
<div class="py-1">
    <?php if ($session->get('message')) { ?>
        <div class="alert alert-success bg-success-100 text-success-600 border-success-600 border-start-width-4-px border-top-0 border-end-0 border-bottom-0 px-24 py-13 mb-0 fw-semibold text-lg radius-4 d-flex align-items-center justify-content-between" role="alert">
            <div class="d-flex align-items-center gap-2">
                <iconify-icon icon="akar-icons:double-check" class="icon text-xl"></iconify-icon>
                <?php echo $session->get('message'); ?>
            </div>
            <button class="remove-button text-success-600 text-xxl line-height-1"> <iconify-icon icon="iconamoon:sign-times-light" class="icon"></iconify-icon></button>
        </div>
    <?php } ?>
    <?php if ($session->get('error')) { ?>
        <div class="alert alert-danger bg-danger-100 text-danger-600 border-danger-600 border-start-width-4-px border-top-0 border-end-0 border-bottom-0 px-24 py-13 mb-0 fw-semibold text-lg radius-4 d-flex align-items-center justify-content-between" role="alert">
            <div class="d-flex align-items-center gap-2">
                <iconify-icon icon="mdi:alert-circle-outline" class="icon text-xl"></iconify-icon>
                <?php echo $session->get('message'); ?>
            </div>
            <button class="remove-button text-danger-600 text-xxl line-height-1"> <iconify-icon icon="iconamoon:sign-times-light" class="icon"></iconify-icon></button>
        </div>
    <?php } ?>
</div>









<div class="card datatable-init">
    <div class="card-body">
        
        <div class="table-responsive custom-scrollbar">
          <table class="display table-striped border" id="basic-1">
    <thead>
        <tr>
            <th>Sno</th>
            <th>Date</th>
            <th>Doc Name</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody id="task_list_body">
        <?php if (!empty($task_list)) { 
            foreach ($task_list as $key => $val) { ?>
                <tr>
                    <td><?= $key + 1; ?></td>
                    <td><?= esc($val['dater']); ?></td>
                    <td><?= esc($val['docname']); ?></td>
                    <td>
                        <div class="d-flex gap-3">
                            <?php if ($val['approve'] === 'yes') { ?>
                                <button type="button" class="btn btn-primary openModalBtn" 
                                        data-bs-toggle="modal" 
                                        data-bs-target="#paymentModal" 
                                        data-id="<?= esc($val['id']); ?>">
                                    Pay Now
                                </button>
                            <?php }else{ ?>
                            <span class="bg-warning-focus text-warning-main px-24 py-4 rounded-pill fw-medium text-sm">Pending</span>
                            <?php } ?>
                            
                            <a type="button" class="btn btn-primary " href="<?= esc($val['signed_pdf_url']); ?>" download>
                                    Download PDF
                                </a>
                        </div>
                    </td>
                </tr>
            <?php } 
        } ?>
    </tbody>
</table>

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




</div>



<!-- Bootstrap Modal -->
<div class="modal fade" id="paymentModal" tabindex="-1" aria-labelledby="paymentModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="paymentModalLabel">Payment Details</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form action="<?php echo base_url('home/paynow');?>" method="post">
                    <input type="hidden" id="propertyId" name="id">
                    
                    <div class="mb-3">
                        <label class="form-label">Property Address</label>
                        <input type="text" class="form-control" name="paddress" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Number of Applicants</label>
                        <input type="number" class="form-control" name="applicants" id="applicants" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Number of Adults</label>
                        <input type="number" class="form-control" name="adults" id="adults" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Application Fees <i class="fa fa-info-circle" tabindex="0" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-content="" data-bs-original-title="Eash Applicant cost 50$" ></i></label>
                        <input type="number" class="form-control" name="fees" id="fees" required>
                    </div>

                    <button type="submit" class="btn btn-success">Proceed to Payment</button>
                </form>
            </div>
        </div>
    </div>
</div>



<?php include('footer.php')?>
<!-- JavaScript to Set ID in Hidden Field -->
<script>
document.addEventListener("DOMContentLoaded", function() {
    document.querySelectorAll(".openModalBtn").forEach(button => {
        button.addEventListener("click", function() {
            let id = this.getAttribute("data-id");
            document.getElementById("propertyId").value = id;
        });
    });
});
</script>


<script>
document.addEventListener("DOMContentLoaded", function() {
    // Get references to the input fields
    const applicantsInput = document.getElementById("applicants");
    const adultsInput = document.getElementById("adults");
    const feesInput = document.getElementById("fees");

    // Function to update fees field based on applicants value
    function updateFees() {
        // Parse the applicants value as an integer; default to 0 if not a number.
        let adultsValue = parseInt(adultsInput.value, 10) || 0;
        // Set fees = applicants * 100.
        feesInput.value = adultsValue * 50;
    }

    // Function to validate the adults value (should be <= applicants)
    function validateAdults() {
        let applicantsValue = parseInt(applicantsInput.value, 10) || 0;
        let adultsValue = parseInt(adultsInput.value, 10) || 0;
        if (adultsValue > applicantsValue) {
            // Option 1: Automatically adjust the adults value to match applicants.
            adultsInput.value = applicantsValue;
            
            // Option 2 (alternative): You can also show an error message if needed.
            // alert("Number of adults cannot exceed number of applicants.");
        }
    }

    // When the applicants field changes, update fees and validate the adults field.
    applicantsInput.addEventListener("input", function() {
        updateFees();
        validateAdults();
    });

    // When the adults field changes, validate it.
    adultsInput.addEventListener("input", function() {
        validateAdults();
        updateFees();
    });
});
</script>


<script>
$(document).ready(function() {
  var table = $('#dataTable').DataTable();

  // Event listener to the two range filtering inputs to redraw on input
  $('#mois, #annee').keyup(function() {
    table.draw();
  });

  $.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        var month = parseInt( $('#mois').val(), 10 );
        var year = parseInt( $('#annee').val(), 10 );
        var date = data[1].split('-');
        console.log(year)
        if ( ( isNaN( year ) && isNaN( month ) ) ||
             ( isNaN( month ) && year == date[0] ) ||
             ( date[1] == month && isNaN( year ) ) ||
             ( date[1] == month && year == date[0] )
            )
        {
            return true;
        }
        return false;
    }
);
});

</script>