<?php
session_start();
error_reporting(0);
include('includes/config.php');
if (strlen($_SESSION['adminlogin']) == 0) {
    header('location:index.php');
} else {

    if (isset($_GET['success'])) {
        echo "<script>alert('Delete Success')</script>";
    }
?>

<!doctype html>
<html lang="en" class="no-js">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <meta name="theme-color" content="#3e454c">

    <title>Manage Server Copy</title>

    <!-- Font awesome -->
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <!-- Sandstone Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!-- Admin Style -->
    <link rel="stylesheet" href="css/style.css">
    <style>
        .errorWrap {
            padding: 10px;
            margin: 0 0 20px 0;
            background: #dd3d36;
            color: #fff;
            box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
        }

        .succWrap {
            padding: 10px;
            margin: 0 0 20px 0;
            background: #5cb85c;
            color: #fff;
            box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
        }
    </style>

</head>

<body>
    <?php include('includes/header.php'); ?>

    <div class="ts-main-content">
        <?php include('includes/leftbar.php'); ?>
        <div class="content-wrapper">
            <div class="container-fluid">

                <div class="row">
                    <div class="col-md-12">

                        <h2 class="page-title">Nid Copy Order Dashboard</h2>

                        <!-- Search Box -->
                        <input type="text" id="searchBox" class="form-control" placeholder="Search by Name, NID, or Phone" onkeyup="searchTable()">

                        <!-- New Section for cardcopy_sub -->
                        <div class="panel panel-default">
                            <div class="panel-heading">Card Copy Submissions</div>
                            <div class="panel-body">

                                <table id="zctb" class="display table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Name</th>
                                            <th>NID Number</th>
                                            <th>Form Number</th>
                                            <th>Voter Number</th>
                                            <th>BRN</th>
                                            <th>Phone Number</th>
                                            <th>Submission Type</th>
                                            <th>Father NID</th>
                                            <th>Mother NID</th>
                                            <th>Submitted By</th>
                                            <th>Status</th>
                                            <th>Submission Date</th>
                                            <th>File Uploaded At</th>
                                            <th>Comments</th>
                                        </tr>
                                    </thead>

                                    <tbody>
                                        <?php
                                        $sql2 = "SELECT * FROM cardcopy_sub WHERE status != 0 ORDER BY id DESC";
                                        $query2 = $dbh->prepare($sql2);
                                        $query2->execute();
                                        $results2 = $query2->fetchAll(PDO::FETCH_OBJ);

                                        if ($query2->rowCount() > 0) {
                                            foreach ($results2 as $result2) { ?>
                                                <tr>
                                                    <td><?php echo htmlentities($result2->id); ?></td>
                                                    <td><?php echo htmlentities($result2->Name); ?></td>
                                                    <td><?php echo htmlentities($result2->nfvb); ?></td>
                                                    <td><?php echo htmlentities($result2->form_num); ?></td>
                                                    <td><?php echo htmlentities($result2->voter_num); ?></td>
                                                    <td><?php echo htmlentities($result2->brn); ?></td>
                                                    <td><?php echo htmlentities($result2->Phone_number); ?></td>
                                                    <td><?php echo htmlentities($result2->sub_type); ?></td>
                                                    <td><?php echo htmlentities($result2->Father_nid); ?></td>
                                                    <td><?php echo htmlentities($result2->Mother_nid); ?></td>
                                                    <td><?php echo htmlentities($result2->Sub_by); ?></td>
                                                    <td><?php echo ($result2->status == 1) ? "<p class='btn btn-warning'>Pending</p>" : (($result2->status == 2) ? "<p class='btn btn-success'>Success</p>" : (($result2->status == 3) ? "<p class='btn btn-danger'>Failed</p>" : "<p class='btn btn-danger'>Inactive</p>")); ?></td>
                                                    <td><?php echo htmlentities($result2->sub_at); ?></td>
                                                    <td><?php echo htmlentities($result2->fileup_at); ?></td>
                                                    <td><?php echo htmlentities($result2->comments); ?></td>
                                                </tr>
                                            <?php }
                                        } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                    </div>
                </div>

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

    <!-- Loading Scripts -->
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap-select.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/main.js"></script>
    <script type="text/javascript">
        function searchTable() {
            const input = document.getElementById("searchBox");
            const filter = input.value.toLowerCase();
            const table = document.getElementById("zctb");
            const tr = table.getElementsByTagName("tr");

            for (let i = 1; i < tr.length; i++) {
                let td = tr[i].getElementsByTagName("td");
                let rowVisible = false;

                for (let j = 0; j < td.length; j++) {
                    if (td[j]) {
                        const txtValue = td[j].textContent || td[j].innerText;
                        if (txtValue.toLowerCase().indexOf(filter) > -1) {
                            rowVisible = true;
                            break;
                        }
                    }
                }
                tr[i].style.display = rowVisible ? "" : "none";
            }
        }

        $(document).ready(function() {
            setTimeout(function() {
                $('.succWrap').slideUp("slow");
            }, 3000);
        });
    </script>

</body>

</html>
<?php } ?>
