<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>
        Payments
    </title>
    <!--     Fonts and icons     -->
    {% include 'admin/includes/imports.top.html' %}
</head>

<body class="g-sidenav-show  bg-gray-100">
    {% include 'admin/includes/part.menu.html' %}
    <main class="main-content position-relative max-height-vh-100 h-100 border-radius-lg ">
        <!-- Navbar -->
        {% include 'admin/includes/part.header.html' %}
        <!-- End Navbar -->
        <div class="container-fluid py-4">
            <div class="row">
                <div class="col-12">
                    <div>
                        <button class="btn btn-sm bg-gradient-dark" onclick="history.back()">Go Back</button>
                    </div>
                    <div class="card mb-4">
                        <div class="card-header pb-0">

                            <div class="d-flex justify-content-between">
                                <h6>Payments Table for {{ customer.name }}</h6>
                                {% if loan %}
                                <h6>Total Loan Amount: <span class="text-danger fw-bold">Kshs
                                        {{"%.2f"|format(loan.amount|float)}}</span></h6>
                                {% endif %}
                            </div>

                        </div>
                        <div class="card-body px-0 pt-0 pb-2">
                            <div class="table-responsive p-0">
                                <table class="table align-items-center mb-0">
                                    <thead>
                                        <tr>
                                            <th></th>
                                            <th
                                                class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">
                                                Status</th>
                                            <th
                                                class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">
                                                Progress</th>
                                            <th
                                                class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">
                                                Total Amount Repaid</th>
                                            <th
                                                class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">
                                                Amount</th>
                                            <th
                                                class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">
                                                Last Pay</th>
                                            <!-- <th class="text-secondary opacity-7"></th> -->
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% if loan %}
                                        {% set am = [0] %}
                                        {% for payments in loan.payments %}
                                        {% if am.append(am.pop()+ payments.payment_amount) -%}{% endif %}

                                        {% set per = (am[0] / loan.amount) * 100 %}
                                        {% if per >= 80 %}
                                        {% set color = 'success' %}
                                        {% elif per < 80 and per> 50 %}
                                            {% set color = 'info' %}
                                            {% else %}
                                            {% set color = 'danger' %}
                                            {% endif %}
                                            <tr>
                                                <td>
                                                    <div class="d-flex px-2 py-1">
                                                        <div>
                                                            <img src="{{url_for('static', filename='admin/img/lipanampesa.jpg')}}"
                                                                class="avatar avatar-sm border me-3" alt="user1">
                                                        </div>

                                                    </div>
                                                </td>
                                                <td class="align-middle text-center text-sm">
                                                    <span class="badge badge-sm bg-gradient-success">Received</span>
                                                </td>

                                                <td class="align-middle text-center">
                                                    <div class="d-flex align-items-center justify-content-center">
                                                        <span
                                                            class="me-2 text-xs font-weight-bold">{{"%.0f"|format(per|float)}}%</span>
                                                        <div>
                                                            <div class="progress">
                                                                <div class="progress-bar bg-gradient-{{color}}"
                                                                    role="progressbar" aria-valuenow="{{per}}"
                                                                    aria-valuemin="0" aria-valuemax="100"
                                                                    style="width: {{per}}%;"></div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </td>
                                                <td class="align-middle text-center">
                                                    <span class="text-secondary text-xs font-weight-bold">Kshs
                                                        {{"%.2f"|format(loan.paid_amount|float)}}</span>
                                                </td>
                                                <td class="align-middle text-center">
                                                    <span class="text-secondary text-xs font-weight-bold">Kshs
                                                        {{"%.2f"|format(payments.payment_amount|float)}}</span>
                                                </td>
                                                <td class="align-middle text-center">
                                                    <span
                                                        class="text-secondary text-xs font-weight-bold">{{payments.payment_date.strftime("%d/%m/%Y,
                                                        %H:%M:%S")}}</span>
                                                </td>
                                            </tr>
                                            {% endfor %}
                                            {%else%}
                                            <tr>
                                                <td colspan="6">
                                                    <h4>No entries!</h4>
                                                </td>
                                            </tr>
                                            {%endif%}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            {% include 'admin/includes/part.footer.html' %}
        </div>
    </main>
    <!--   Core JS Files   -->
    {% include 'admin/includes/imports.bottom.html' %}
    <script>
        var win = navigator.platform.indexOf('Win') > -1;
        if (win && document.querySelector('#sidenav-scrollbar')) {
            var options = {
                damping: '0.5'
            }
            Scrollbar.init(document.querySelector('#sidenav-scrollbar'), options);
        }
    </script>
</body>

</html>