<style type="text/css">
.user-info-row-one {
    margin-bottom: 30px !important;
}

.fee-title p {
    font-weight: 500;
    font-size: 20px;
    padding-bottom: 3px;
    border-bottom: 1px solid #c6c6c6;
    display: inline-block;
} 

.user-info-row-one {
    border-bottom: 3px solid #c6c6c6;
}

.red-pay {
    background-color: red !important;
}

table.dataTable thead th,
table.dataTable thead td {
    padding: 10px 38px !important;
}

table.dataTable th,
table.dataTable tbody td {
    text-align: center !important;
}

.unpaid-fee-container {
    margin-bottom: 40px;
}
</style>
<main class="common_margin" id="main">
    <div class="my-info-inner " style="width:100%">
        <h5><strong> Fee & Payments</strong></h5>
        <div class="my-info-wraper">
            <ul class="my-info-header">
                <li><a href="<?php echo base_url() ?>student/my-info">My Info</a></li>
                <li><a href="<?php echo base_url() ?>student/my-course">Course Details</a></li>
                <li><a href="<?php echo base_url() ?>student/my-payments">Fee & Payments</a></li>
            </ul>

            <?php 

            if(empty($transactions)) {
                echo '<p>No Fees has been payed by the student\'s</p>';
            } else { ?>

            <div class="recent-transaction mb-5">
                <h5 class="mb-3"> Recent Transactions</h5>

                <div class="recent-table">
                    <table class="tbl display table-responsive" style="width: 100% !important">
                        <thead style="width: 100% !important">
                            <tr>
                                <th>Date</th>
                                <th>Transaction ID</th>
                                <th>Payment purpose</th>
                                <th>Payment Status</th>
                                <th>Paid Amount</th>
                                <th>Discount/Scholarship</th>
                                <th>Fine Amount</th>
                                <th>Action</th>
                            </tr> 
                        </thead>
                        <tbody>
                            <?php foreach ($transactions as $key => $value) { 
                                $paid = ((float)$value['payment_amt'] + (float)$value['fine_amount']) - (float)$value['deduction_amount'];
                                ?>
                            <tr>
                                <td><?= date('d-m-Y', strtotime($value['payment_date'])) ?></td>
                                <td><?= $value['txn_id']  ?></td>
                                <td><?= $value['fee_values'] ?><small>(<?= $value['payment_for']  ?>)</small></td>
                                <td><?= $value['status']  ?></td>
                                <td><?= number_format((float)$paid, 2, '.', ''); ?>
                                </td>
                                <td><?= number_format((float)$value['deduction_amount'], 2, '.', ''); ?>
                                                            </td>
                                                            <td><?= number_format((float)$value['fine_amount'], 2, '.', ''); ?></td>
                                <td>
                                    <div class="sub_tabbtnsec">
                                        <div class="sub_viewbtn" title="View"><a target="_blank"
                                                href="<?= base_url(); ?>Student/invoice/<?= urlsafe_b64encode($student_info_array['studentId']) ?>/<?= urlsafe_b64encode($value['txn_id']) ?>/<?= urlsafe_b64encode(0) ?> "><i
                                                    class="fa fa-eye" aria-hidden="true"></i></a>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="my-info-inner">
        <div class="my-info-wraper">
            <?php foreach ($payments as $key => $value) {
                    $flag = true;
                    $installments = json_decode($value['payment_details']);
                    ?>
            <div>
                <div class="fee-title">
                    <p><?= $value['feetype_name'] ?></p>
                    <?php if ($key == 0) {?>
                    <a href="<?php echo base_url()?>student/student_fees" class="float-right">
                        <button class="btn " style="background-color: #032da1;color: #fff;">View Invoice</button>
                    </a>
                    <?php } ?>
                </div>
            </div>
            <div class="user-info-row-one">
                <div class="user-info-one-fees">
                    <p>Programme Fee Total:</p>
                    <p>N-<?= $value['amount'] ?></p>
                </div>
                <div class="user-info-one-fees">
                    <p>Payment Option:</p>
                    <p class="pay_op"><?=$value['payment_type_name'] ?></p>
                </div>

                <div class="user-info-one-fees">
                    <p>Payment Dates:</p>
                    <ul class="sele-sub-opt-two" style="list-style-type: none;">

                        <?php foreach ($installments as $index => $ele) { 
                                    $class = $ele->payment_status == 'yes' ? 'green-pay' : '';
                                    $due_date = date('Y-m-d', strtotime($ele->due_date));
                                    $current_date = date('Y-m-d');
                                    $payClass = $due_date > $current_date ? '' : 'red-pay';
                                    ?>
                        <li>
                            <div class="outter-pay-wrap" style="display: flex;">
                                <div class="outter-check-wrap">
                                    <?php if($ele->payment_status == 'yes') { ?>
                                    <div class="outter-check <?= $class ?> ">
                                        <i class="fa fa-check"></i>
                                    </div>
                                    <?php } else { 
                                                    if($due_date > $current_date) { ?>
                                    <div class="outter-check <?= $class ?> ">
                                        <i class="fa fa-check"></i>
                                    </div>
                                    <?php } else { ?>
                                    <div class="outter-check <?= $payClass ?> ">
                                        <i class="fas fa-times"></i>
                                    </div>
                                    <?php }  ?>


                                    <?php } ?>
                                </div>
                                <p class="paid-para"><?= $ele->feeamount; ?></p>
                            </div>
                            <?php if($ele->payment_status == 'yes') { ?>
                            <div class="paid-space" title="<?= date('d-m-Y', strtotime($ele->paid_date)) ?>"> Paid on:
                                <?= date('d-m-Y', strtotime($ele->paid_date)) ?></div>
                            <?php } else { ?>
                            <div class="paid-space" title="<?= date('d-m-Y', strtotime($ele->due_date)) ?>"> Due on:
                                <?= date('d-m-Y', strtotime($ele->due_date)) ?></div>
                            <?php if($flag) {
                                                        echo '<a href="'. base_url() .'/student/pay_unpaid_fees/'.$value['encrypted'].'" class="fees-pay-btn text-white">Pay Now</a>';
                                                        $flag = false;
                                                    } ?>
                            <?php } ?>

                        </li>
                        <?php } ?>

                    </ul>
                </div>


            </div>


            <?php } 
                    }  ?>
            <div class="user-info-row-one">
                <!-- // NOT PAID INSTALLMENTS -->
                <?php if(isset($not_paid_fees)) { 
                            foreach ($not_paid_fees as $key => $value) {
                                ?>
                <div class="unpaid-fee-container">
                    <div>
                        <div class="fee-title">
                            <p><?= $value['feetype_name'] ?></p>
                        </div>
                    </div>
                    <div class="user-info-one-fees">
                        <p>Programme Fee Total:</p>
                        <p>N-<?= $value['amount'] ?></p>
                    </div>

                    <?php if($value['single_payment'] == 'Yes') { 
                                    $installments = json_decode($value['due_amount_date']);
                                    ?>

                    <div class="user-info-one-fees">
                        <p>Payment Option:</p>
                        <p class="pay_op">Full Payment</p>
                    </div>

                    <div class="user-info-one-fees mb-5">
                        <p>Payment Dates:</p>
                        <ul class="sele-sub-opt-two" style="list-style-type: none;">
                            <?php foreach ($installments as $index => $val) { 
                                                foreach ($val as $key2 => $ele) { 
                                                    $due_date = date('Y-m-d', strtotime($ele->due_date));
                                                    $current_date = date('Y-m-d');
                                                    $payClass = $due_date > $current_date ? '' : 'red-pay';
                                                    ?>
                            <li>
                                <div class="outter-pay-wrap" style="display: flex;">
                                    <div class="outter-check <?= $payClass ?>">
                                        <?php if($due_date > $current_date) {
                                                                    echo '<i class="fas fa-check"></i>';
                                                                } else {
                                                                    echo '<i class="fas fa-times"></i>';
                                                                } ?>
                                    </div>
                                    <p class="paid-para">N-<?=$ele->amount; ?></p>
                                </div>

                                <div class="paid-space"> Due on:
                                    <?= date('d-m-Y', strtotime($ele->due_date)) ?></div>
                                <a href="<?= base_url() ?>/student/pay_unpaid_fees/<?= $value['encrypted']?>"
                                    class="fees-pay-btn text-white">Pay Now</a>
                            </li>
                            <?php } 
                                                } ?>
                        </ul>
                    </div>

                    <?php } else { ?>

                    <div class="multiple-fee-container">
                        <label>Select Type of Payment</label>
                        <div><?= $value['dropdown'] ?></div>
                    </div>
                    <?php  } ?>

                </div>

                <?php }
                            } ?>

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

<script>
$("document").ready(function() {

    //table js
    $('.tbl').DataTable({
        "lengthMenu": [
            [5, 10, 25, 50, -1],
            [5, 10, 25, 50, "All"]
        ]
    });

    $(".my-info-header li a").each(function(index, ele) {
        $(this).parent().removeClass("blue-line");

        let thisEle = $(this).attr('href').split('/').pop();

        var current_path = window.location.pathname.split('/').pop();

        if (thisEle == current_path) {
            $(this).parent().addClass("blue-line");
        }
    });

});


const get_payment_details = function(ele, course_fee_id) {


    let payment_type_id = ele.value;
    let container = $(ele).parent().parent();

    $.ajax({
        type: 'POST',
        url: "<?php echo base_url(); ?>student/ajax_get_fee_installments",
        data: {
            installment_type: payment_type_id,
            course_fee_id: course_fee_id
        },
        success: function(response) {
            $(container).empty();
            if (!response) {
                $(container).append(
                    '<p style="color:red">Could Not fetch amount, Please try again later</p>');
            } else {
                $(container).append(response);
            }

        },
    });

}
</script>