Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/clinic/
Upload File :
Current File : C:/xampp/htdocs/clinic/user_dashboard.php

<?php
session_start();
include 'includes/db.php';

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'user') {
    header("Location: login.php");
    exit();
}

$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];
$page_title = "User Dashboard";

$success = $error = "";

// Retrieve success/error messages from session (after redirect)
if (isset($_SESSION['success'])) {
    $success = $_SESSION['success'];
    unset($_SESSION['success']);
}
if (isset($_SESSION['error'])) {
    $error = $_SESSION['error'];
    unset($_SESSION['error']);
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $log_date = $_POST['log_date'] ?? '';
    $patient_name = $_POST['patient_name'] ?? '';
    $client_type = $_POST['client_type'] ?? '';
    $school = $_POST['school'] ?? '';
    $age = $_POST['age'] ?? '';
    $sex = $_POST['sex'] ?? '';
    $address = $_POST['address'] ?? '';
    $contact_number = $_POST['contact_number'] ?? '';
    $signatureData = $_POST['signature'] ?? '';
    $attended_by = $_POST['attended_by'] ?? '';

    // Validate all fields including signature data URL
    if ($log_date && $patient_name && $client_type && $school && $age && $sex && $address && $contact_number && $signatureData && $attended_by) {
        
        $signature = null;
        
        // Check if signature data is a base64 image string (PNG)
        if (preg_match('/^data:image\/png;base64,/', $signatureData)) {
            $signatureDataClean = str_replace('data:image/png;base64,', '', $signatureData);
            $signatureDataClean = str_replace(' ', '+', $signatureDataClean);
            $decodedImage = base64_decode($signatureDataClean);

            if ($decodedImage !== false) {
                // Generate a unique filename for the signature
                $filename = 'signature_' . time() . '_' . bin2hex(random_bytes(4)) . '.png';
                $filepath = __DIR__ . '/signatures/' . $filename;

                // Save the image file to /signatures folder
                if (file_put_contents($filepath, $decodedImage) !== false) {
                    $signature = $filename;
                } else {
                    $_SESSION['error'] = "Failed to save the signature image.";
                    header("Location: " . $_SERVER['PHP_SELF']);
                    exit();
                }
            } else {
                $_SESSION['error'] = "Invalid signature data.";
                header("Location: " . $_SERVER['PHP_SELF']);
                exit();
            }
        } else {
            $_SESSION['error'] = "Signature format not recognized.";
            header("Location: " . $_SERVER['PHP_SELF']);
            exit();
        }

        // Insert into database with the filename of the signature image
        $sql = "INSERT INTO logs (
                    user_id, log_date, patient_name, client_type, school, age, sex, address, contact_number, signature, attended_by
                ) VALUES (
                    :user_id, :log_date, :patient_name, :client_type, :school, :age, :sex, :address, :contact_number, :signature, :attended_by
                )";

        $stmt = $pdo->prepare($sql);
        $stmt->execute([
            ':user_id' => $user_id,
            ':log_date' => $log_date,
            ':patient_name' => $patient_name,
            ':client_type' => $client_type,
            ':school' => $school,
            ':age' => $age,
            ':sex' => $sex,
            ':address' => $address,
            ':contact_number' => $contact_number,
            ':signature' => $signature,
            ':attended_by' => $attended_by
        ]);

        $_SESSION['success'] = "Log entry added successfully.";
        header("Location: " . $_SERVER['PHP_SELF']);
        exit();
    } else {
        $_SESSION['error'] = "All fields are required.";
        header("Location: " . $_SERVER['PHP_SELF']);
        exit();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
    .table-responsive {
        max-height: 500px; /* Adjust height as needed */
        overflow-y: auto;
    }

    .table thead th {
        position: sticky;
        top: 0;
        background-color: #f8f9fa; /* Match Bootstrap's table-light */
        z-index: 10;
    }
</style>

    <meta charset="UTF-8">
    <title><?= htmlspecialchars($page_title) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div style="background-color: #004d00; color: white; padding: 15px 30px;">
    <h4 class="mb-0">SDO Batangas City - Clinic Management System (v1.0)</h4>
</div>

<div class="container-fluid mt-4 px-5">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <div class="text-start"></div>
        <a href="logout.php" class="btn btn-danger">Logout</a>
    </div>

   <h4 class="text-center text-black fw-bold mb-4">CLIENTS'/PATIENTS' LOGSHEET</h4>

    <?php if ($success): ?>
        <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
    <?php endif; ?>
    <?php if ($error): ?>
        <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <!-- Log Entry Form -->
    <form method="POST" onsubmit="return checkSignature()" class="mb-4">
        <div class="table-responsive">
            <table class="table table-bordered align-middle">
    <thead class="table-light">
        <tr>
            <th style="width: 120px;">Date</th>
            <th style="width: 180px;">Name of Client/Patient</th>
            <th style="width: 120px;">Type</th>
            <th style="width: 150px;">School</th>
            <th style="width: 70px;">Age</th>
            <th style="width: 100px;">Sex</th>
            <th style="width: 200px;">Address</th>
            <th style="width: 140px;">Contact No.</th>
            <th style="width: 220px;">Signature</th>
            <th style="width: 160px;">Attended by</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="date" name="log_date" class="form-control" required></td>
            <td><input type="text" name="patient_name" class="form-control" required></td>
            <td>
                <select name="client_type" class="form-select" required>
                    <option value="">Select</option>
                    <option value="Teaching">Teaching</option>
                    <option value="Non-Teaching">Non-Teaching</option>
                    <option value="Learners">Learner</option>
                </select>
            </td>
            <td><input type="text" name="school" class="form-control" required></td>
            <td><input type="text" name="age" class="form-control" required></td>
            <td>
                <select name="sex" class="form-select" required>
                    <option value="">Select</option>
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                </select>
            </td>
            <td><input type="text" name="address" class="form-control" required></td>
            <td><input type="text" name="contact_number" class="form-control" required></td>
            <td>
                <canvas id="signature-pad" width="200" height="100" style="border:1px solid #ccc;"></canvas>
                <input type="hidden" name="signature" id="signature" required>
                <button type="button" class="btn btn-sm btn-secondary mt-1" onclick="clearSignature()">Clear</button>
            </td>
            <td><input type="text" name="attended_by" class="form-control" required></td>
        </tr>
    </tbody>
</table>

        </div>

        <!-- ✅ Data Privacy Statement -->
        <div style="border: 2px solid #004d00; background-color: #e6f2e6; padding: 16px; border-radius: 8px; line-height: 1.4;" class="mt-3">
            <strong style="font-size: 1.5rem; color: #004d00;">🔐 Data Privacy Notice:</strong><br>
            We value your privacy. In compliance with the <strong>Data Privacy Act of 2012 (RA 10173)</strong>, all personal and sensitive information you provide will be collected, stored, and processed strictly for medical and administrative purposes only.
            
            <br>
            
            Your data will be treated with the <strong>highest level of confidentiality</strong> and will <strong>not be shared</strong> with any unauthorized individuals or third parties without your consent, unless required by law.
            
            <br>
            
            By submitting this form, you acknowledge and consent to the lawful processing of your data in accordance with this statement.
        </div>

        <div class="text-center mt-3">
            <button type="submit" class="btn btn-primary">Save</button>
        </div>
    </form>
</div>
<script>
    let canvas = document.getElementById('signature-pad');
    let signatureInput = document.getElementById('signature');
    let ctx = canvas.getContext('2d');
    let drawing = false;

    canvas.addEventListener('mousedown', function (e) {
        drawing = true;
        ctx.beginPath();
        ctx.moveTo(e.offsetX, e.offsetY);
    });

    canvas.addEventListener('mousemove', function (e) {
        if (drawing) {
            ctx.lineTo(e.offsetX, e.offsetY);
            ctx.stroke();
        }
    });

    canvas.addEventListener('mouseup', function () {
        drawing = false;
        saveSignature();
    });

    canvas.addEventListener('mouseleave', function () {
        drawing = false;
    });

    function saveSignature() {
        const dataURL = canvas.toDataURL();
        signatureInput.value = dataURL;
    }

    function clearSignature() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        signatureInput.value = '';
    }

    function checkSignature() {
        if (!signatureInput.value) {
            alert("Please sign before submitting.");
            return false;
        }
        return true;
    }
</script>
</body>
</html>