<?php
    include('session.php');

    date_default_timezone_set('Asia/Singapore');
    
    $id = $_GET['id'] ?? '';
    
    $sql = "SELECT * FROM $tblcompetition WHERE AutoIndex LIKE '$id'";

       if($result=mysqli_query($conn,$sql)){
            while ($row=mysqli_fetch_array($result)){
                $i=$i+1;
                
                $title = $row["Title"];
                $posterurl = $row["PosterURL"];
                $closingdate = $row["ClosingDate"];
                $dbtablename = $row["DbTableName"];
                
            }
       }
?>


<!DOCTYPE html>
<html lang="en" class="h-100">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, shrink-to-fit=9">
        <meta name="description" content="<?php echo $sitetitle ?>">
        <meta name="author" content="<?php echo $sitetitle ?>">
        <title><?php echo $sitetitle ?></title>
		
		<!-- Favicon Icon -->
        <link rel="icon" type="image/png" href="images/nkn-logo-horizontal.png">
		
        <!-- Stylesheets -->
        <?php include 'plugincss.php' ?>
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css">


	</head>

<body class="d-flex flex-column h-100">

        <!-- Create Coupon Model Start-->
        <div class="modal fade" id="AddRecords" tabindex="-1" aria-labelledby="AddRecordsLabel" aria-hidden="false">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="AddRecordsLabel">New Record</h5>
                        <button type="button" class="close-model-btn" data-bs-dismiss="modal" aria-label="Close"><i class="uil uil-multiply"></i></button>
                    </div>
                    <form id="FormNewEntry" class="form" method="post" action="">
                        <div class="modal-body">
                            <div class="model-content main-form">
                                <div class="row">
                                        <div class="model-content main-form">
                                            <div class="row">
                                                <div class="col-lg-12 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Name</label>
                                                        <input class="form-control h_40" id="name" name="name" type="text" required>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-8 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">NRIC/MyKid</label>
                                                        <input class="form-control h_40" id="nric" name="nric" type="text" required>
                                                    </div>
                                                </div>
                                                <div class="col-lg-4 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Age</label>
                                                        <input class="form-control h_40" id="age" name="age" type="text" required>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-9 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Email Add</label>
                                                        <input class="form-control h_40" id="email" name="email" type="text" required>
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Contact No</label>
                                                        <input class="form-control h_40" id="contact" name="contact" type="text" required>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-12 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">School Name</label>
                                                        <input class="form-control h_40" id="school" name="school" type="text" required>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-12 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Category</label>
                                                        <select class="form-control h_40" name="category" required>
                                                            <option value=""></option>
                                                        
                                                            <option value="CATEGORY 1 : 7 YEAR TO 9 YEARS"
                                                                <?php if(empty($rowrec['Category']) || $rowrec['Category'] == 'CATEGORY 1 : 7 YEAR TO 9 YEARS')  ?>>
                                                                CATEGORY 1 : 7 YEAR TO 9 YEARS
                                                            </option>
                                                        
                                                            <option value="CATEGORY 2 : 10 YEARS TO 12 YEARS"
                                                                <?php if($rowrec['Category'] == 'CATEGORY 2 : 10 YEARS TO 12 YEARS') ?>>
                                                                CATEGORY 2 : 10 YEARS TO 12 YEARS
                                                            </option>
                                                        
                                                            <option value="CATEGORY 3 : 13 YEARS TO 17 YEARS"
                                                                <?php if($rowrec['Category'] == 'CATEGORY 3 : 13 YEARS TO 17 YEARS') ?>>
                                                                CATEGORY 3 : 13 YEARS TO 17 YEARS
                                                            </option>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>
                                            
                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <input type="hidden" name="action" id="action" value="saveRecord" />
                                    <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-dismiss="modal"><i class="fa-solid fa-cancel me-3"></i>Close</button>
                                    <button type="submit" class="btn btn-success" id = "saveRecord1" name = "saveRecord1"><i class="fa-solid fa-save me-3"></i>Save</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    </div>
                    </div>



	<!-- Add Organisation Model End-->
	<!-- Header Start-->
	<header class="header">
        <?php include 'topmenu.php' ?>
	</header>
	<!-- Header End-->
	<!-- Left Sidebar Start -->
	<nav class="vertical_nav">
        <?php include 'sidemenu.php' ?>
	</nav>



	<!-- Left Sidebar End -->
	<!-- Body Start -->
	<div class="wrapper wrapper-body">
		<div class="dashboard-body">
			<div class="container-fluid">
				<div class="row">
					<div class="col-md-12">
						<div class="d-main-title">
							<h3><i class="fa-solid fa-trophy me-3"></i><?php echo $title ;?></h3>
						</div>
						<h1></h1>
					</div>
					
					<div class="col-md-12">
                        <div class="d-md-flex flex-wrap align-items-center">
                            <div class="rs ms-auto mt_r4">
                                <button type="button" class="btn btn-danger" id="btnBack" onclick="window.open('competition.php', '_parent');"><i class="fa-solid fa-arrow-left"></i> Back</button>
                                <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#AddRecords"><i class="fa-solid fa-plus me-3"></i> New</button>
                            </div>

                        </div>
                    </div>
                    

                        <div class="event-list">
                            <div class="tab-content">
                                <div class="tab-pane fade show active" id="orders-tab" role="tabpanel">
                                    <div class="table-card mt-4">
                                        <div class="main-table ps-3">
                                            <div class="table-responsive">
                                                <br>
                                                <div class="d-md-flex flex-wrap align-items-center">
                                                    <div class="col-md-3 me-2">
                                                        <select class="form-select" id="filtercat" onchange="filterCategory()">
                                                            <?php $selected = $_GET['category'] ?? 'ALL'; ?>
                                                        
                                                            <option value="ALL" <?= ($selected=='ALL')?'selected':''; ?>>ALL</option>
                                                            <option value="CATEGORY 1 : 7 YEAR TO 9 YEARS" <?= ($selected=='CATEGORY 1 : 7 YEAR TO 9 YEARS')?'selected':''; ?>>
                                                                CATEGORY 1 : 7 YEAR TO 9 YEARS
                                                            </option>
                                                            <option value="CATEGORY 2 : 10 YEARS TO 12 YEARS" <?= ($selected=='CATEGORY 2 : 10 YEARS TO 12 YEARS')?'selected':''; ?>>
                                                                CATEGORY 2 : 10 YEARS TO 12 YEARS
                                                            </option>
                                                            <option value="CATEGORY 3 : 13 YEARS TO 17 YEARS" <?= ($selected=='CATEGORY 3 : 13 YEARS TO 17 YEARS')?'selected':''; ?>>
                                                                CATEGORY 3 : 13 YEARS TO 17 YEARS
                                                            </option>
                                                        </select>
                                                    </div>
                                                    <div class="col-md-3 me-2">
                                                        <select class="form-select" id="filterver" onchange="filterVerified()">
                                                            <?php $selected = $_GET['verified'] ?? 'ALL'; ?>
                                                            <option value="ALL" <?= ($selected=='ALL')?'selected':''; ?>>ALL</option>
                                                            <option value="VERIFIED" <?= ($selected=='VERIFIED')?'selected':''; ?>>
                                                                VERIFIED
                                                            </option>
                                                            <option value="PENDING VERIFICATION" <?= ($selected=='PENDING VERIFICATION')?'selected':''; ?>>
                                                                PENDING VERIFICATION
                                                            </option>
                                                        </select>
                                                    </div>
                                                    <div class="col-md-2 me-2">
                                                        <button type="button" class="btn btn-warning" id="btnPdf"  onclick="openPdf()"><i class="fa-solid fa-file-pdf"></i> Download</button>
                                                    </div>
                                                    
                                                </div>
                            
                                                <table id="dataTable" class="table">
                                                    <thead class="thead-dark">
                                                        <tr>
                                                            <th scope="col" style="width: 5%;">#</th>
                                                            <th scope="col" style="width: 25%;">Name</th>
                                                            <th scope="col" style="width: 15%;">MyKid/NRIC</th>
                                                            <th scope="col" style="width: 5%;">Age</th>
                                                            <th scope="col" style="width: 15%;">Email / Contact No</th>
                                                            <th scope="col" style="width: 15%;">Category</th>
                                                            <th scope="col" style="width: 15%;">Verified</th>
                                                            <th scope="col" style="width: 10%;">Action</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                                $i=0;

                                                                $category = $_GET['category'] ?? 'ALL';
                                                                $verified = $_GET['verified'] ?? 'ALL';
                                                                
                                                                $where = [];
                                                                
                                                                // Category filter
                                                                if($category != "ALL"){
                                                                    $where[] = "Category = '".mysqli_real_escape_string($conn, $category)."'";
                                                                }
                                                                
                                                                if($verified == "VERIFIED"){
                                                                    $where[] = "VerifyDateTime IS NOT NULL";
                                                                }
                                                                elseif($verified == "PENDING VERIFICATION"){
                                                                    $where[] = "VerifyDateTime IS NULL";
                                                                }
                                                                
                                                                // Build SQL
                                                                $sql = "SELECT * FROM $dbtablename";
                                                                
                                                                if(!empty($where)){
                                                                    $sql .= " WHERE " . implode(" AND ", $where);
                                                                }
                                                                
                                                                $sql .= " ORDER BY FullName ASC";

                                                                   if($resultrec=mysqli_query($conn,$sql)){
                                                                        while ($rowrec=mysqli_fetch_array($resultrec)){
                                                                            $i=$i+1;
                                                                        
                                                                    ?>
                                                                        <tr>
                                                                            <td align = "left"><?php echo $i?></td>
                                                                            <td align = "left"><?php echo strtoupper($rowrec['FullName'])."<br>".strtoupper($rowrec['SchoolName']) ;?></td>
                                                                            <td align = "left"><?php echo strtoupper($rowrec['NRICMyKid']) ;?></td>
                                                                            <td align = "left"><?php echo strtoupper($rowrec['Age']) ;?></td>
                                                                            <td align = "left"><?php echo strtolower($rowrec['EmailAdd'])."<br>".strtoupper($rowrec['ContactNo']) ;?></td>
                                                                            <td align = "left"><?php echo $rowrec['Category'] ;?></td>
                                                                            <td align="left">
                                                                                <?php 
                                                                                if(!empty($rowrec['VerifyDateTime'])){
                                                                                    echo date("d-m-Y H:i:s", strtotime($rowrec['VerifyDateTime']));
                                                                                } else {
                                                                                    echo "";
                                                                                }
                                                                                ?>
                                                                            </td>
                                                                           
                                                                            <td align ="left">
                                                                            <button type="button" id = "btnUpdate" name = "btnUpdate" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#editRecord<?php echo $rowrec['AutoIndex']?>"><i class="fa-regular fa-pen"></i></button>
                                                                            
                                                                                <!-- Create Coupon Model Start-->
                                                                                <div class="modal fade" id="editRecord<?php echo $rowrec['AutoIndex']?>" tabindex="-1" aria-labelledby="editRecordLabel<?php echo $rowrec['AutoIndex']?>" aria-hidden="false">
                                                                                    <div class="modal-dialog modal-lg">
                                                                                        <div class="modal-content">
                                                                                            <div class="modal-header">
                                                                                                <h5 class="modal-title" id="editRecordLabel<?php echo $rowrec['AutoIndex']?>">Update Record</h5>
                                                                                                <button type="button" class="close-model-btn" data-bs-dismiss="modal" aria-label="Close"><i class="uil uil-multiply"></i></button>
                                                                                            </div>
                                                                                                <form id="FormEditEntry<?php echo $rowrec['AutoIndex'] ?>" class="form-edit-entry" method="post" action="">
                                                                                                        <div class="modal-body">
                                                                                                            <div class="model-content main-form">
                                                                                                                <div class="row">
                                                                                                                        <div class="model-content main-form">
                                                                                                                            <div class="row">
                                                                                                                                <div class="form-group mt-4">
                                                                                                                                    <input class="form-control h_40" id="recordid" name="recordid" type="hidden" value="<?php echo $rowrec['AutoIndex'];?>">
                                                                                                                                </div>
                                                                                                                                <div class="col-lg-12 col-md-12">
                                                                                                                                    <div class="form-group mt-4">
                                                                                                                                        <label class="form-label">Name</label>
                                                                                                                                        <input class="form-control h_40" id="name<?php echo $rowrec['AutoIndex']; ?>" name="name" type="text" value="<?php echo $rowrec['FullName'] ;?>">
                                                                                                                                    </div>
                                                                                                                                </div>
                                                                                                                                <div class="col-lg-6 col-md-12">
                                                                                                                                    <div class="form-group mt-4">
                                                                                                                                        <label class="form-label">NRIC/MyKid</label>
                                                                                                                                        <input class="form-control h_40" id="nric<?php echo $rowrec['AutoIndex']; ?>" name="nric" type="text" value="<?php echo $rowrec['NRICMyKid'] ;?>">
                                                                                                                                    </div>
                                                                                                                                </div>
                                                                                                                                <div class="col-lg-6 col-md-12">
                                                                                                                                    <div class="form-group mt-4">
                                                                                                                                        <label class="form-label">Age</label>
                                                                                                                                        <input class="form-control h_40" id="age<?php echo $rowrec['AutoIndex']; ?>" name="age" type="text" value="<?php echo $rowrec['Age'] ;?>">
                                                                                                                                    </div>
                                                                                                                                </div>
                                                                                                                                <div class="col-lg-8 col-md-12">
                                                                                                                                    <div class="form-group mt-4">
                                                                                                                                        <label class="form-label">Email Add</label>
                                                                                                                                        <input class="form-control h_40" id="email<?php echo $rowrec['AutoIndex']; ?>" name="email" type="text" value="<?php echo $rowrec['EmailAdd'] ;?>">
                                                                                                                                    </div>
                                                                                                                                </div>
                                                                                                                                <div class="col-lg-4 col-md-12">
                                                                                                                                    <div class="form-group mt-4">
                                                                                                                                        <label class="form-label">Contact No</label>
                                                                                                                                        <input class="form-control h_40" id="contactno<?php echo $rowrec['AutoIndex']; ?>" name="contactno" type="text" value="<?php echo $rowrec['ContactNo'] ;?>">
                                                                                                                                    </div>
                                                                                                                                </div>
                                                                                                                                <div class="col-lg-12 col-md-12">
                                                                                                                                    <div class="form-group mt-4">
                                                                                                                                        <label class="form-label">School Name</label>
                                                                                                                                        <input class="form-control h_40" id="school<?php echo $rowrec['AutoIndex']; ?>" name="school" type="text" value="<?php echo $rowrec['SchoolName'] ;?>">
                                                                                                                                    </div>
                                                                                                                                </div>
                                                                                                                                <div class="col-lg-12 col-md-12">
                                                                                                                                    <div class="form-group mt-4">
                                                                                                                                        <label class="form-label">Category</label>
                                                                                                                                        <select class="form-control h_40" id="cat<?php echo $rowrec['AutoIndex']; ?>" name="cat">
                                                                                                                                            <option value="">-- Select Category --</option>
                                                                                                                                
                                                                                                                                            <option value="CATEGORY 1 : 7 YEAR TO 9 YEARS" <?php if($rowrec['Category'] == 'CATEGORY 1 : 7 YEAR TO 9 YEARS') echo 'selected'; ?>>
                                                                                                                                                CATEGORY 1 : 7 YEAR TO 9 YEARS
                                                                                                                                            </option>
                                                                                                                                
                                                                                                                                            <option value="CATEGORY 2 : 10 YEARS TO 12 YEARS" <?php if($rowrec['Category'] == 'CATEGORY 2 : 10 YEARS TO 12 YEARS') echo 'selected'; ?>>
                                                                                                                                                CATEGORY 2 : 10 YEARS TO 12 YEARS
                                                                                                                                            </option>
                                                                                                                                
                                                                                                                                            <option value="CATEGORY 3 : 13 YEARS TO 17 YEARS" <?php if($rowrec['Category'] == 'CATEGORY 3 : 13 YEARS TO 17 YEARS') echo 'selected'; ?>>
                                                                                                                                                CATEGORY 3 : 13 YEARS TO 17 YEARS
                                                                                                                                            </option>
                                                                                                                                        </select>
                                                                                                                                    </div>
                                                                                                                                </div>
                                                                                                                            </div>
                                                                                                                        </div>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <div class="modal-footer">
                                                                                                                    <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-dismiss="modal"><i class="fa-solid fa-cancel me-3"></i>Close</button>
                                                                                                                    <button type="submit" class="btn btn-danger" id = "deleteRecord" name = "deleteRecord" hidden><i class="fa-solid fa-trash me-3"></i>Delete</button>
                                                                                                                    <button type="submit" class="btn btn-success" id = "updateRecord" name = "updateRecord"><i class="fa-solid fa-save me-3"></i>Save</button>
                                                                                                                </div>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                    </form>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                                
                                                                                
                                                                                
                                                                                </td>
                                                                            </tr>
                                                                    <?php
                                                                    }
                                                                       }

                                                                    ?>

                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th scope="col" style="width: 5%;">#</th>
                                                            <th scope="col" style="width: 25%;">Name</th>
                                                            <th scope="col" style="width: 15%;">MyKid/NRIC</th>
                                                            <th scope="col" style="width: 10%;">Age</th>
                                                            <th scope="col" style="width: 15%;">Email / Contact No</th>
                                                            <th scope="col" style="width: 15%;">Category</th>
                                                            <th scope="col" style="width: 15%;">Verified</th>
                                                            <th scope="col" style="width: 10%;">Action</th>
                                                        </tr>
                                                    </tfoot>

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

                </div>
            </div>
        </div>
    </div>
	
    <div class="copyright-footer">
        <?php echo $copyrights ?>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- Include DataTable JS -->
    <script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
    <script>
        // Initialize DataTable
        $(document).ready(function() {
          $('#dataTable').DataTable({
                  "ordering": false,
                  "searching": true,
                  "lengthChange": false,
                  "pageLength": 10
              });
        });
    </script>
    
    <script>
        function filterCategory(){
            let category = document.getElementById("filtercat").value;
            let id = <?php echo json_encode($id); ?>;
            window.location.href = "?id=" + encodeURIComponent(id) + "&category=" + encodeURIComponent(category);
        }
    </script>
    
    <script>
        function filterVerified(){
            let verified = document.getElementById("filterver").value;
            let id = <?php echo json_encode($id); ?>;
            window.location.href = "?id=" + encodeURIComponent(id) + "&verified=" + encodeURIComponent(verified);
        }
    </script>

    <script>
        let clickedButton = null;
        
        document.getElementById("FormNewEntry").addEventListener("submit", function(event) {
        event.preventDefault();
    
        const modal = bootstrap.Modal.getInstance(document.querySelector('#AddRecords'));
        modal.hide();
        
        const formData = new FormData(this);
    
        fetch('save-rej-rec.php', {
            method: 'POST',
            body: formData
        })
        .then(response => response.text())
        .then(data => {
            alert(data);
            location.reload();
        })
        .catch(error => console.error('Error:', error));
    });
    
    // EDIT FORM
    document.querySelectorAll('.form-edit-entry').forEach(form => {
    
        form.querySelectorAll('button[type="submit"]').forEach(button => {
            button.addEventListener('click', function () {
                clickedButton = this;
            });
        });
    
        form.addEventListener('submit', function(event) {
            event.preventDefault();
    
            const modalId = this.id.replace('FormEditEntry', 'editRecord');
            const modal = bootstrap.Modal.getInstance(document.querySelector(`#${modalId}`));
            if(modal) modal.hide();
    
            const formData = new FormData(this);
    
            if (clickedButton) {
                formData.append(clickedButton.name, clickedButton.value);
            }
            //alert("A");
            
            fetch('update-rej-rec.php', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    alert(data.error);
                } else if (data.success) {
                    alert(data.success);
                } else {
                    alert('Unexpected response format.');
                }
                location.reload();
            })
            .catch(error => console.error('Error:', error));
        });
    });
                              
    </script>

    <script>
        function openPdf() {
            let id = <?php echo json_encode($id); ?>;
            let category = document.getElementById("filtercat").value;
            let verified = document.getElementById("filterver").value;
        
            let url = "print-reg-list.php?id=" + encodeURIComponent(id) +
                      "&category=" + encodeURIComponent(category) +
                      "&verified=" + encodeURIComponent(verified);
        
            window.open(url, '_blank');
        }
        </script>


	<!-- Body End -->
    <?php include 'pluginjs.php' ?>

</body>
</html>
