????????????????????
??????????????????
ÿØÿà
 JFIF      ÿÛ C      


!"$"$ÿÛ C    
ÿÂ p 
" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................????????????????????
??????????????????
ÿØÿà
 JFIF      ÿÛ C      


!"$"$ÿÛ C    
ÿÂ p 
" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................<?php
include('session.php');
include 'connection.php';
 
// Delete record
if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $sql = "DELETE FROM certificates WHERE id=$id";
    mysqli_query($conn, $sql);
    echo "<script>alert('Certificate deleted successfully!'); window.location='';</script>";
}

// Insert record
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $target_dir = "uploads/";
    if (!is_dir($target_dir)) mkdir($target_dir);

    $cert_image_path = "";
    // Certificate Image upload
    if (!empty($_FILES["certificate_image"]["name"])) {
        $cert_image_path = $target_dir . time() . '_cert_' . basename($_FILES["certificate_image"]["name"]);
        if (!move_uploaded_file($_FILES["certificate_image"]["tmp_name"], $cert_image_path)) {
            echo "<script>alert('Sorry, there was an error uploading the certificate image.');</script>";
            $cert_image_path = ""; // Clear path on failure
        }
    }

    // Student Photo upload
    $student_photo_path = "";
    if (!empty($_FILES["student_photo"]["name"])) {
        $student_photo_path = $target_dir . time() . '_student_' . basename($_FILES["student_photo"]["name"]);
        if (!move_uploaded_file($_FILES["student_photo"]["tmp_name"], $student_photo_path)) {
            echo "<script>alert('Sorry, there was an error uploading the student photo.');</script>";
            $student_photo_path = ""; // Clear path on failure
        }
    }

    // Use prepared statements to prevent SQL injection
    $sql = "INSERT INTO certificates (student_name, father_name, registration_no, certificate_no, course_name, session, duration, grade, exam_held, issue_date, certificate_image, student_photo) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    
    if ($stmt = mysqli_prepare($conn, $sql)) {
        mysqli_stmt_bind_param($stmt, "ssssssssssss", 
            $_POST['student_name'], $_POST['father_name'], $_POST['registration_no'], $_POST['certificate_no'], 
            $_POST['course_name'], $_POST['session'], $_POST['duration'], $_POST['grade'], 
            $_POST['exam_held'], $_POST['issue_date'], $cert_image_path, $student_photo_path
        );
        
        mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);
        echo "<script>alert('Certificate uploaded successfully!'); window.location='';</script>";
    } else {
        echo "<script>alert('Sorry, there was an error saving the certificate.');</script>";
    }
}

$result = mysqli_query($conn, "SELECT * FROM certificates ORDER BY id DESC");

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Fee Report |Xcel Training Institute</title>
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" href="../images/logo-wide.png">

    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>

    <!-- Base Styling  -->
    <link rel="stylesheet" href="assets/main/css/fonts.css">
    <link rel="stylesheet" href="assets/main/css/style.css">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js" integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=" crossorigin="anonymous"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f8f9fa;
            margin: 0;
            padding: 40px;
        }
        .form-container {
            background: #fff;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 40px;
        }
        label { display: block; margin-top: 12px; font-weight: 600; }
        input[type="text"], input[type="date"], input[type="file"] {
            width: 100%; padding: 10px; margin-top: 5px;
            border: 1px solid #ccc; border-radius: 6px;
        }
        button {
            background: #007bff; color: #fff; border: none;
            padding: 10px 18px; border-radius: 6px;
            margin-top: 20px; cursor: pointer;
        }
        button:hover { background: #0056b3; }
        table { width: 100%; border-collapse: collapse; }
        th, td {
            padding: 10px; border-bottom: 1px solid #ddd;
            text-align: left;
        }
        th { background: #007bff; color: #fff; }
        .certificate-page img { width: 60px; height: 60px; border-radius: 4px; }
        .action-btn {
            padding: 6px 10px;
            border-radius: 5px;
            color: #fff;
            text-decoration: none;
            display: inline-block;
            margin: 2px 5px;
        }
        .edit-btn { background: #28a745; }
        .delete-btn { background: #dc3545; }
		
		.form-row {
            display: flex;
            flex-wrap: wrap;
            margin-right: -15px;
            margin-left: -15px;
        }
        .form-group {
            flex: 0 0 50%;
            max-width: 50%;
            padding-right: 15px;
            padding-left: 15px;
        }
		
		
		
    </style>
</head>

<body>
    <div id="main-wrapper" class="show">
 
        <?php include "sidebar.php"; ?>
        <?php include "header.php"; ?>
 
        <div class="content-body">
            <div class="warper container-fluid">
                <div class="new-patients main_container">
                    
                    <div class="row">
<div class="certificate-page">
                      <div class="form-container">
    <h2>Certificate Upload (Admin)</h2>
   <form method="POST" enctype="multipart/form-data" onsubmit="return validateForm()"> 
        <div class="form-row">
            <div class="form-group">
                <label>Student Name:</label>
                <input type="text" name="student_name" id="student_name" required>
            </div>
            <div class="form-group">
                <label>Father's Name:</label>
                <input type="text" name="father_name" id="father_name" required>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group">
                <label>Registration No:</label>
                <input type="text" name="registration_no" id="registration_no" required>
            </div>
            <div class="form-group">
                <label>Certificate No:</label>
                <input type="text" name="certificate_no" id="certificate_no" required>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group">
                <label>Course Name:</label>
                <input type="text" name="course_name" id="course_name" required>
            </div>
            <div class="form-group">
                <label>Session:</label>
                <input type="text" name="session" id="session" required>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group">
                <label>Duration:</label>
                <input type="text" name="duration" id="duration" required>
            </div>
            <div class="form-group">
                <label>Grade:</label>
                <input type="text" name="grade" id="grade" required>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group"><label>Exam Held:</label><input type="text" name="exam_held" id="exam_held" required></div>
            <div class="form-group"><label>Certificate Issue Date:</label><input type="date" name="issue_date" id="issue_date" required></div>
        </div>
        <div class="form-row">
            <div class="form-group"><label>Certificate Image:</label><input type="file" name="certificate_image" id="certificate_image" accept="image/*"></div>
            <div class="form-group"><label>Student Photo:</label><input type="file" name="student_photo" id="student_photo" accept="image/*"></div>
        </div>

        <button type="submit">Save</button>
    </form>
</div>

<div class="form-container">
    <h3>Uploaded Certificates</h3>
    <table id="certTable">
        <thead>
        <tr>
            <th>ID</th>
            <th>Student Name</th>
            <th>Father's Name</th>
            <th>Registration No</th>
            <th>Certificate No</th>
            <th>Course</th>
            <th>Session</th>
            <!--<th>Duration</th>
            <th>Grade</th>
            <th>Exam Held</th> -->
            <th>Issue Date</th>
            <th>Image</th>
           <!-- <th>Student Photo</th>-->
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        <?php while ($row = mysqli_fetch_assoc($result)) { ?>
        <tr>
            <td><?= $row['id'] ?></td>
            <td><?= htmlspecialchars($row['student_name']) ?></td>
            <td><?= htmlspecialchars($row['father_name']) ?></td>
            <td><?= htmlspecialchars($row['registration_no']) ?></td>
            <td><?= htmlspecialchars($row['certificate_no']) ?></td>
            <td><?= htmlspecialchars($row['course_name']) ?></td>
            <td><?= htmlspecialchars($row['session']) ?></td>
            <!--<td><?= htmlspecialchars($row['duration']) ?></td>
            <td><?= htmlspecialchars($row['grade']) ?></td>
            <td><?= htmlspecialchars($row['exam_held']) ?></td> -->
            <td><?= date('d-m-Y', strtotime($row['issue_date'])) ?></td>
            <td><img src="<?= $row['certificate_image'] ?>" alt="Certificate"></td>
           <!-- <td><img src="<?= $row['student_photo'] ?>" alt="Student Photo"></td> -->
            <td>
                <a href="edit_certificate.php?id=<?= $row['id'] ?>" class="action-btn edit-btn">Edit</a>
                <a href="?delete=<?= $row['id'] ?>" class="action-btn delete-btn" onclick="return confirm('Are you sure?')">Delete</a>
            </td>
        </tr>
        <?php } ?>
        </tbody>
    </table>
</div>

<script>
    $(document).ready(function () {
        $('#certTable').DataTable({
            "order": [[ 0, "desc" ]]
        });
    });

    function validateForm() {
        const file = document.getElementById("certificate_image").files[0];
        if (file && file.size > 3 * 1024 * 1024) {
            alert("File size should be less than 3MB!");
            return false;
        }
        return true;
    }
</script>
                    </div>
                    </div>
                    
                </div>
            </div>
        </div>
        <!-- End section content -->

        <?php include('footer.php'); ?>
    </div>

    <!-- Student Details Modal -->
    <div id="studentDetailsModal" class="modal">
        <div class="modal-content">
            <span class="close">&times;</span>
            <h2 id="modalTitle">Student Details</h2>
            <div id="modalBody">
                <!-- Content will be loaded here -->
            </div>
        </div>
    </div>

    <script>
    // Modal functionality
    var modal = document.getElementById("studentDetailsModal");
    var span = document.getElementsByClassName("close")[0];

    span.onclick = function() {
        modal.style.display = "none";
    }

    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }

    // Toggle fee status via AJAX (Approved <-> Pending)
    function toggleFeeStatus(feeId, btnEl) {
        if (!feeId) return;
        var $btn = $(btnEl);
        var current = ($btn.text() || '').trim().toLowerCase();
        var nextStatus = current === 'approved' ? 'Pending' : 'Approved';
        var savingLabel = nextStatus === 'Approved' ? 'Approving...' : 'Setting Pending...';
        $btn.prop('disabled', true).text(savingLabel);
        $.post('update_fee_status.php', { id: feeId, status: nextStatus }, function(resp) {
            try { resp = (typeof resp === 'object') ? resp : JSON.parse(resp); } catch(e) { resp = { success:false, message: e.message }; }
            if (resp.success) {
                if (nextStatus === 'Approved') {
                    $btn.removeClass('btn-warning').addClass('btn-success').text('Approved');
                } else {
                    $btn.removeClass('btn-success').addClass('btn-warning').text('Pending');
                }
            } else {
                alert('Failed to update: ' + (resp.message || 'Unknown error'));
                // Revert to opposite label if failed
                if (current === 'approved') {
                    $btn.removeClass('btn-success').addClass('btn-warning').text('Pending');
                } else {
                    $btn.removeClass('btn-warning').addClass('btn-success').text('Approved');
                }
            }
            $btn.prop('disabled', false);
        }).fail(function(){
            alert('Request failed');
            // Revert UI to the other state label
            if (current === 'approved') {
                $btn.removeClass('btn-success').addClass('btn-warning').text('Pending');
            } else {
                $btn.removeClass('btn-warning').addClass('btn-success').text('Approved');
            }
            $btn.prop('disabled', false);
        });
    }

    // Function to show student details
    function showStudentDetails(admission_no) {
        modal.style.display = "block";
        
        // Show loading
        document.getElementById("modalBody").innerHTML = '<div style="text-align: center; padding: 20px;"><i class="fas fa-spinner fa-spin"></i> Loading...</div>';
        
        // Fetch student details via AJAX
        $.post("get_student_details.php", {admission_no: admission_no}, function(response) {
            console.log('Raw response:', response);
            console.log('Response type:', typeof response);
            
            var data;
            
            // Check if response is already parsed by jQuery
            if (typeof response === 'object') {
                data = response;
            } else {
                try {
                    data = JSON.parse(response);
                } catch (e) {
                    console.error('JSON Parse Error:', e);
                    document.getElementById("modalBody").innerHTML = '<div class="alert alert-danger">Error parsing response: ' + e.message + '</div>';
                    return;
                }
            }
            
            if (data.success) {
                displayStudentDetails(data.student, data.account, data.payments);
            } else {
                document.getElementById("modalBody").innerHTML = '<div class="alert alert-danger">' + data.message + '</div>';
            }
        }).fail(function(xhr, status, error) {
            console.error('AJAX Error:', status, error);
            document.getElementById("modalBody").innerHTML = '<div class="alert alert-danger">Failed to load student details: ' + error + '</div>';
        });
    }

    // Function to display student details in modal
    function displayStudentDetails(student, account, payments) {
        var html = '';
        
        // Student Info
        html += '<div class="student-info">';
        html += '<h4>Student Information</h4>';
        html += '<div class="row">';
        html += '<div class="col-md-6"><strong>Name:</strong> ' + student.student_name + '</div>';
        html += '<div class="col-md-6"><strong>Father\'s Name:</strong> ' + student.fathers_name + '</div>';
        html += '<div class="col-md-6"><strong>Admission No:</strong> ' + student.admission_no + '</div>';
        html += '<div class="col-md-6"><strong>Mobile:</strong> ' + student.mobile_no + '</div>';
        html += '<div class="col-md-6"><strong>Course:</strong> ' + student.course_name + '</div>';
        html += '<div class="col-md-6"><strong>Course Fee:</strong> ₹' + parseFloat(student.total_fee).toFixed(2) + '</div>';
        html += '</div>';
        html += '</div>';
        
        // Account Summary
        html += '<div class="summary-box">';
        html += '<h4>Account Summary</h4>';
        html += '<div class="row">';
        html += '<div class="col-md-3"><strong>Total Fees:</strong><br>₹' + parseFloat(account.total_fees || student.total_fee).toFixed(2) + '</div>';
        html += '<div class="col-md-3"><strong>Due Amount:</strong><br>₹' + parseFloat(account.due_amount || student.total_fee).toFixed(2) + '</div>';
        html += '<div class="col-md-3"><strong>Paid Amount:</strong><br>₹' + parseFloat(account.paid_amount || 0).toFixed(2) + '</div>';
        html += '<div class="col-md-3"><strong>Total Received:</strong><br>₹' + parseFloat(account.received_amount || 0).toFixed(2) + '</div>';
        html += '</div>';
        html += '</div>';
        
        // Payment History
        html += '<div class="payment-history">';
        html += '<h4>Payment History</h4>';
        
        if (payments && payments.length > 0) {
            payments.forEach(function(payment, index) {
                html += '<div class="payment-row">';
                html += '<div class="row">';
                html += '<div class="col-md-2"><strong>Payment #' + (index + 1) + '</strong></div>';
                html += '<div class="col-md-2">₹' + parseFloat(payment.received_amount).toFixed(2) + '</div>';
                html += '<div class="col-md-2">' + payment.payment_date + '</div>';
                html += '<div class="col-md-2">MR: ' + payment.mr_no + '</div>';
                html += '<div class="col-md-2">' + (payment.transaction_id || '-') + '</div>';
                html += '<div class="col-md-2">' + (payment.bank_upi || '-') + '</div>';
                html += '</div>';
                html += '</div>';
            });
        } else {
            html += '<div class="payment-row">No payment history found</div>';
        }
        
        html += '</div>';
        
        document.getElementById("modalBody").innerHTML = html;
    }
    </script>

    <!-- Include other scripts -->
   <script src="assets/plugins/popper/popper.min.js"></script>

    <!-- Bootstrap -->
    <script src="assets/plugins/bootstrap/js/bootstrap.js"></script>

    <!-- Moment -->
    <script src="assets/plugins/moment/moment.min.js"></script>

    <!-- Date Range Picker -->
    <script src="assets/plugins/daterangepicker/daterangepicker.min.js"></script>

    <!-- Datatable -->
    <script src="assets/plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="assets/js/init-tdatatable.js"></script>

    <!-- DataTables Buttons (CDN) for export) -->
    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.4.1/css/buttons.dataTables.min.css">
    <script src="https://cdn.datatables.net/buttons/2.4.1/js/dataTables.buttons.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.print.min.js"></script>

    <script>
    // Initialize export buttons on fee report table
    (function(){
      if (!jQuery.fn.dataTable) return;
      if (!jQuery.fn.dataTable.Buttons) return;
      var sel = '#feeReportTable';
      if (!jQuery(sel).length) return;
      try { jQuery(sel).DataTable().destroy(); } catch(e) {}
      jQuery(sel).DataTable({
        order: [[0,'desc']],
        dom: 'Bfrtip',
        buttons: [
          {extend: 'copy', title: 'Fee Report'},
          {extend: 'csv', title: 'Fee Report'},
          {extend: 'excel', title: 'Fee Report'},
          {extend: 'pdf', title: 'Fee Report'},
          {extend: 'print', title: 'Fee Report'}
        ]
      });
    })();
    </script>

    <!-- Chart js -->
    <script src="assets/plugins/chart/chart/Chart.min.js"></script>
    <script src="assets/js/charts-custom.js"></script>

    <!-- Main Custom JQuery -->
    <script src="assets/js/toggleFullScreen.js"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>