<style>
    *,
    html,
    body {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-size: 14px;
        font-weight: 500 !important;
    }
</style>
<main class="common_margin" id="main">
    <div class="main-wrap">
        <div class="classes-section">
            <div class="class-head">
                <h3 class="common-heading">School Library</h3>
            </div>
            <form method="post" action="<?= base_url(); ?>student/library">
                <div class="top-filter report_filter">
                    <div class="filter_lbl">
                        <i class="fas fa-filter"></i>
                        Filter
                    </div>
                    <?php
                    $startDate = (isset($start_date) && !empty($start_date)) ? date('Y-m-d', strtotime($start_date)) : '';
                    $endtDate = (isset($end_date) && !empty($end_date)) ? date('Y-m-d', strtotime($end_date)) : '';
                    $status = (isset($status) && !empty($status)) ? $status : '';
                    ?>
                    <div class="date-filter">
                        <div class="date-fil-row">
                            <label>Start Date</label>
                            <input type="text" name="start_date" class="nepal-date-student form-control" placeholder="Please Select Start Date" value="<?= $startDate; ?>">
                        </div>
                        <div class="date-fil-row">
                            <label>End Date</label>
                            <input type="text" name="end_date" id="end_date" class="nepal-date-student form-control" placeholder="Please Select End Date" value="<?= $endtDate; ?>">
                        </div>
                    </div>
                    <div class="subject-filter">
                        <label>Status</label>
                        <select id="status" name="status" class=" form-control">
                            <option value="">All</option>
                            <option value="due" <?php echo (!empty($status) ? 'selected' : ''); ?>> Due </option>
                        </select>
                    </div>
                    <div class="filter-reset">
                        <button type="submit" name="searchfilter" id="filter_reset" class="btn btn-success" value="search_filter">Search Filter</button>
                        <a href=" ">
                            <button type="button" class="subject_addbtn subject_addbtn_nobg"><i class="fas fa-redo"></i>Reset</button></a>
                    </div>
                </div>
            </form>
            <table id="tbl1" class="display table-responsive dataTable" style="display:table;">
                <thead>
                    <tr>
                        <th>Sl.No</th>
                        <th>Book Name</th>
                        <th>Borrowed Date</th>
                        <th>Last Date</th>
                        <th>Submitted Date</th>
                        <th>Remaining Days</th>
                        <th>Overdue</th>
                        <th>Fine <?php echo '(' . $general_setting['currency'] . ')' ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php

                    $generalfee = $general_setting['due_fee_per_day'];
                    $sno = 1;
                    $totalfee = 0;
                    foreach ($book_data as $bvalue) {
                        $issuedDate = date('d-m-Y', strtotime($bvalue['issued_date']));
                        $returnedDate = date('d-m-Y', strtotime($bvalue['return_date']));
                        $today = (!empty($bvalue['returned_on_date'])) ? date('d-m-Y', strtotime($bvalue['returned_on_date'])) : $todayDate;
                        $diff = strtotime($returnedDate) - strtotime($today);
                        $remaingDays = ($diff / 86400);
                        $total_duedays = ($remaingDays < 0) ? abs($remaingDays) : 0;

                        $fine = ($bvalue['fine'] > 0) ? $bvalue['fine'] : $generalfee;
                        $fineamt = ($total_duedays * $fine);

                    ?>

                        <?php
                        if ($status == 'due') {

                            if ($total_duedays > 0) {

                        ?>
                                <tr>
                                    <td><?php echo $sno; ?></td>
                                    <td><?= $bvalue['title']; ?></td>
                                    <td><?= $issuedDate; ?></td>
                                    <td><?= $returnedDate; ?></td>
                                    <td><?= (!empty($bvalue['returned_on_date'])) ? date('d-m-Y', strtotime($bvalue['returned_on_date'])) : '-' ?></td>
                                    <td><?php


                                        if ($remaingDays == 0)
                                            echo "<label style='color:red;font-weight:bold;'>Due Today</label>";
                                        else if ($remaingDays > 0)
                                            echo $remaingDays . " Days";
                                        else if ($remaingDays < 0)
                                            echo "0 Days";
                                        ?>
                                    </td>
                                    <td><?php
                                        if ($total_duedays > 0) {
                                            echo "<label style='color:red;font-weight:400;'>" . $total_duedays . " Days</label>";
                                        } else {
                                            echo "<label style='font-weight:400;'>" . $total_duedays . " Days</label>";
                                        }

                                        ?>
                                    </td>
                                    <td><?php echo $fineamt; ?> </td>
                                </tr>
                            <?php
                                $totalfee += $fineamt;
                                $sno++;
                            }
                        } else {

                            ?>

                            <tr>
                                <td><?php echo $sno; ?></td>
                                <td><?= $bvalue['title']; ?></td>
                                <td><?= $issuedDate; ?></td>
                                <td ><?= $returnedDate; ?></td>
                                <td class="text-center"><?= (!empty($bvalue['returned_on_date'])) ? date('d-m-Y', strtotime($bvalue['returned_on_date'])) : ''; ?></td>
                                <td class="text-center"><?php

                                    $diff = strtotime($returnedDate) - strtotime($today);
                                    $remaingDays = ($diff / 86400);
                                    if ($remaingDays == 0)
                                        echo "<label style='color:red;font-weight:bold;'>Due Today</label>";
                                    else if ($remaingDays > 0)
                                        echo $remaingDays . " Days";
                                    else if ($remaingDays < 0)
                                        echo "0 Days";
                                    ?>
                                </td>
                                <td class="text-center"><?php

                                    if ($total_duedays > 0) {
                                        echo "<label style='color:red;font-weight:400;'>" . $total_duedays . " Days</label>";
                                    } else {
                                        echo "<label style='font-weight:400;'>" . $total_duedays . " Days</label>";
                                    }
                                    ?>
                                </td>
                                <td class="text-center"><?php echo $fineamt; ?> </td>
                            </tr>
                    <?php
                            $totalfee += $fineamt;
                            $sno++;
                        }
                    } ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="7">
                            <label style="float: right;font-size: 13px;">Total Late Fee:</label>
                        </td>
                        <td style="font-size: 13px;">l
                            <?= $totalfee . " " . $general_setting['currency']; ?>
                        </td>

                    </tr>
                </tfoot>
            </table>

        </div>
    </div>
</main>
<script src="<?php echo base_url(); ?>assets_student/js/jquery.dataTables.min.js"></script>
<script>
    $('#tbl1').DataTable({
        "lengthMenu": [
            [5, 10, 25, 50, -1],
            [5, 10, 25, 50, "All"]
        ]
    });
</script>