Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/cid/system/
Upload File :
Current File : C:/xampp/htdocs/cid/system/edit_mista_plan.php

<?php
session_start();
@include 'include/config.php';

if (!isset($_SESSION['username_school'])) {
    die("Error: User not logged in.");
}

$username = $_SESSION['username_school'];
$plan_id = $_GET['plan_id'] ?? null;

if (!$plan_id) {
    die("Error: Missing plan ID.");
}

// Fetch the plan
$stmt = $conn->prepare("SELECT * FROM mista_plans WHERE id = ? AND username = ?");
$stmt->bind_param("is", $plan_id, $username);
$stmt->execute();
$plan = $stmt->get_result()->fetch_assoc();

if (!$plan) {
    die("Error: Plan not found.");
}

// Fetch activities
$stmt2 = $conn->prepare("SELECT * FROM mista_plan_activities WHERE plan_id = ?");
$stmt2->bind_param("i", $plan_id);
$stmt2->execute();
$activities = $stmt2->get_result()->fetch_all(MYSQLI_ASSOC);

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = $conn->prepare("UPDATE mista_plans SET division = ?, section = ?, district = ?, month = ?, year = ? WHERE id = ?");
    $stmt->bind_param("sssssi", $_POST['division'], $_POST['section'], $_POST['district'], $_POST['month'], $_POST['year'], $plan_id);
    $stmt->execute();

    $conn->query("DELETE FROM mista_plan_activities WHERE plan_id = $plan_id");

    if (!empty($_POST['date'])) {
        $dates = $_POST['date'];
        $activities_input = $_POST['instructional_supervisory_and_monitoring_activity'];
        $schools = $_POST['school'];
        $school_heads = $_POST['school_head'];
        $remarks = $_POST['remarks'];

        $stmt_act = $conn->prepare("INSERT INTO mista_plan_activities (plan_id, date, instructional_supervisory_and_monitoring_activity, school, school_head, remarks) VALUES (?, ?, ?, ?, ?, ?)");

        for ($i = 0; $i < count($dates); $i++) {
            $stmt_act->bind_param("isssss", $plan_id, $dates[$i], $activities_input[$i], $schools[$i], $school_heads[$i], $remarks[$i]);
            $stmt_act->execute();
        }
        $stmt_act->close();
    }

    header("Location: view_mista_plans.php?updated=1");
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit MISTA Plan</title>
    <link href="../vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .form-control-sm {
            font-size: 0.875rem;
            padding: 0.25rem 0.5rem;
        }
        th, td {
            vertical-align: middle !important;
        }

        /* Ensure the table is responsive */
        @media (max-width: 767px) {
            .table thead {
                display: none;
            }
            .table td {
                display: block;
                text-align: right;
                padding-left: 50%;
            }
            .table td::before {
                content: attr(data-label);
                font-weight: bold;
                text-align: left;
                display: inline-block;
                width: 50%;
            }
        }

        /* Ensure the form fields stack correctly on small screens */
        @media (max-width: 576px) {
            .form-row .col {
                margin-bottom: 15px;
            }
            .form-row .col-md-2 {
                width: 100%;
            }
            .btn {
                width: 100%;
            }
        }
    </style>
</head>
<body class="container-fluid mt-5">

    <h3 class="text-center mb-4">Edit Monthly Instructional Supervisory and Technical Assistance Plan</h3>

    <form method="POST">

        <!-- General Info -->
        <div class="form-row mb-3">
            <div class="col-md-2 col-12 mb-2">
                <label>Division</label>
                <input type="text" name="division" class="form-control form-control-sm" value="<?= htmlspecialchars($plan['division']) ?>" required>
            </div>
            <div class="col-md-2 col-12 mb-2">
                <label>Section/Unit</label>
                <input type="text" name="section" class="form-control form-control-sm" value="<?= htmlspecialchars($plan['section']) ?>" required>
            </div>
            <div class="col-md-2 col-12 mb-2">
                <label>District/Learning Area</label>
                <input type="text" name="district" class="form-control form-control-sm" value="<?= htmlspecialchars($plan['district']) ?>" required>
            </div>
            <div class="col-md-2 col-12 mb-2">
                <label>Month</label>
                <input type="text" name="month" class="form-control form-control-sm" value="<?= htmlspecialchars($plan['month']) ?>" required>
            </div>
            <div class="col-md-2 col-12 mb-2">
                <label>Year</label>
                <input type="text" name="year" class="form-control form-control-sm" value="<?= htmlspecialchars($plan['year']) ?>" required>
            </div>
        </div>

        <!-- Add/Remove Buttons -->
        <div class="form-group text-center mb-3">
            <button type="button" class="btn btn-secondary btn-sm" onclick="addRow()">+ Add Activity</button>
            <button type="button" class="btn btn-danger btn-sm" onclick="removeRow()">- Remove Activity</button>
        </div>

        <!-- Activities Table -->
        <div class="table-responsive">
            <table class="table table-bordered table-sm text-center" id="activityTable">
                <thead class="thead-light">
                    <tr>
                        <th>Date</th>
                        <th>Activity</th>
                        <th>School</th>
                        <th>School Head</th>
                        <th>Remarks</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($activities)): ?>
                        <?php foreach ($activities as $activity): ?>
                            <tr>
                                <td><input type="date" name="date[]" class="form-control form-control-sm" value="<?= htmlspecialchars($activity['date']) ?>" required></td>
                                <td><input type="text" name="instructional_supervisory_and_monitoring_activity[]" class="form-control form-control-sm" value="<?= htmlspecialchars($activity['instructional_supervisory_and_monitoring_activity']) ?>" required></td>
                                <td><input type="text" name="school[]" class="form-control form-control-sm" value="<?= htmlspecialchars($activity['school']) ?>" required></td>
                                <td><input type="text" name="school_head[]" class="form-control form-control-sm" value="<?= htmlspecialchars($activity['school_head']) ?>" required></td>
                                <td><input type="text" name="remarks[]" class="form-control form-control-sm" value="<?= htmlspecialchars($activity['remarks']) ?>"></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td><input type="date" name="date[]" class="form-control form-control-sm" required></td>
                            <td><input type="text" name="instructional_supervisory_and_monitoring_activity[]" class="form-control form-control-sm" required></td>
                            <td><input type="text" name="school[]" class="form-control form-control-sm" required></td>
                            <td><input type="text" name="school_head[]" class="form-control form-control-sm" required></td>
                            <td><input type="text" name="remarks[]" class="form-control form-control-sm"></td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <!-- Submit -->
        <div class="form-group text-center mt-3">
            <button type="submit" class="btn btn-primary">Update Plan</button>
        </div>
    </form>

    <!-- JavaScript -->
    <script>
        function addRow() {
            const tableBody = document.getElementById("activityTable").getElementsByTagName('tbody')[0];
            const newRow = tableBody.rows[0].cloneNode(true);
            Array.from(newRow.querySelectorAll("input")).forEach(input => input.value = "");
            tableBody.appendChild(newRow);
        }

        function removeRow() {
            const tableBody = document.getElementById("activityTable").getElementsByTagName('tbody')[0];
            if (tableBody.rows.length > 1) {
                tableBody.deleteRow(-1);
            } else {
                alert("At least one activity row must remain.");
            }
        }
    </script>

</body>
</html>