Mini Kabibi Habibi

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

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

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

$username = $_SESSION['username_school'];
$editing = isset($_GET['plan_id']) && is_numeric($_GET['plan_id']);
$plan_id = $editing ? $_GET['plan_id'] : null;
$report_data = $monitoring_data = [];

if ($editing) {
    $stmt = $conn->prepare("SELECT * FROM mista_reports WHERE id = ? AND username = ?");
    $stmt->bind_param("is", $plan_id, $username);
    $stmt->execute();
    $report_data = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    if ($report_data) {
        $stmt = $conn->prepare("SELECT * FROM mista_monitoring_reports WHERE plan_id = ?");
        $stmt->bind_param("i", $plan_id);
        $stmt->execute();
        $monitoring_data = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
        $stmt->close();
    } else {
        die("Error: Report not found.");
    }
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $division = $_POST['division'] ?? null;
    $section = $_POST['section'] ?? null;
    $district = $_POST['learning_area'] ?? null;
    $month = $_POST['month'] ?? null;
    $school_year = $_POST['school_year'] ?? null;

    if ($editing) {
        $stmt = $conn->prepare("UPDATE mista_reports SET division=?, section=?, district=?, month=?, school_year=? WHERE id=? AND username=?");
        $stmt->bind_param("sssssis", $division, $section, $district, $month, $school_year, $plan_id, $username);
    } else {
        $stmt = $conn->prepare("INSERT INTO mista_reports (division, section, district, month, school_year, username) VALUES (?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssss", $division, $section, $district, $month, $school_year, $username);
    }

    if (!$stmt->execute()) {
        die("Error saving report: " . $stmt->error);
    }

    if (!$editing) {
        $plan_id = $conn->insert_id;
    }

    if (!empty($_POST['date'])) {
        $dates = $_POST['date'];
        $schools = $_POST['school'];
        $heads = $_POST['name_of_school_head'];
        $activities = $_POST['activities_undertaken'];
        $findings = $_POST['findings'];
        $assistance = $_POST['technical_assistance_provided'];
        $agreements = $_POST['agreement'];
        $signatures = $_POST['school_head_signature'];

        if ($editing) {
            $stmt = $conn->prepare("DELETE FROM mista_monitoring_reports WHERE plan_id = ?");
            $stmt->bind_param("i", $plan_id);
            $stmt->execute();
            $stmt->close();
        }

        $stmt = $conn->prepare("INSERT INTO mista_monitoring_reports (plan_id, dates, school, name_of_school_head, activities_undertaken, findings, technical_assistance_provided, agreement, school_head_signature) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
        for ($i = 0; $i < count($dates); $i++) {
            $stmt->bind_param("issssssss", $plan_id, $dates[$i], $schools[$i], $heads[$i], $activities[$i], $findings[$i], $assistance[$i], $agreements[$i], $signatures[$i]);
            if (!$stmt->execute()) {
                die("Error saving monitoring entry $i: " . $stmt->error);
            }
        }
        $stmt->close();
    }

    if (isset($_POST['generate_report'])) {
        header("Location: generate_mistar_word.php?plan_id=" . $plan_id);
        exit();
    } else if (isset($_POST['save_only'])) {
        header("Location: view_mista_reports.php");
        exit();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title><?php echo $editing ? 'Edit' : 'Create'; ?> MISTA Report</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;
        }
    </style>
    <script>
        function addRow() {
            const table = document.getElementById("activityTable").getElementsByTagName('tbody')[0];
            const newRow = table.insertRow(table.rows.length);

            for (let i = 0; i < 8; i++) {
                let cell = newRow.insertCell(i);
                let input = document.createElement("input");
                input.classList.add('form-control', 'form-control-sm');

                if (i === 0) input.type = 'date', input.name = 'date[]';
                if (i === 1) input.type = 'text', input.name = 'school[]';
                if (i === 2) input.type = 'text', input.name = 'name_of_school_head[]';
                if (i === 3) input.type = 'text', input.name = 'activities_undertaken[]';
                if (i === 4) input.type = 'text', input.name = 'findings[]';
                if (i === 5) input.type = 'text', input.name = 'technical_assistance_provided[]';
                if (i === 6) input.type = 'text', input.name = 'agreement[]';
                if (i === 7) input.type = 'text', input.name = 'school_head_signature[]';

                cell.appendChild(input);
            }
        }

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

        function confirmSubmission() {
            return confirm("Are you sure you want to submit this form?");
        }
    </script>
</head>
<body class="container-fluid mt-5 px-3">

<h3 class="text-center mb-4"><?php echo $editing ? 'Edit' : 'Create'; ?> Monthly Instructional Supervisory and Technical Assistance Report</h3>

<form action="edit_mista_report.php<?php echo $editing ? '?plan_id=' . $plan_id : ''; ?>" method="POST" onsubmit="return confirmSubmission();">

    <!-- General Info -->
    <div class="row mb-3">
        <?php
        $fields = [
            ['Division', 'division'],
            ['Section/Unit', 'section'],
            ['District/Learning Area', 'learning_area', 'district'],
            ['Month', 'month'],
            ['School Year', 'school_year']
        ];
        foreach ($fields as $field) {
            $label = $field[0];
            $name = $field[1];
            $dbKey = $field[2] ?? $name;
            $value = htmlspecialchars($report_data[$dbKey] ?? ($name === 'school_year' ? date('Y') : ''));
            echo "
            <div class='col-12 col-md-2 mb-2'>
                <label>$label</label>
                <input type='text' name='$name' class='form-control form-control-sm' value='$value' 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>

    <!-- Activity Table -->
    <div class="table-responsive mb-3">
        <table class="table table-bordered table-sm text-center" id="activityTable">
            <thead class="thead-light">
                <tr>
                    <th>Date</th>
                    <th>School</th>
                    <th>School Head</th>
                    <th>Activities Undertaken</th>
                    <th>Findings</th>
                    <th>Technical Assistance</th>
                    <th>Agreement</th>
                    <th>Signature</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $rows = !empty($monitoring_data) ? $monitoring_data : [[]];
                foreach ($rows as $entry) {
                    echo '<tr>';
                    $fields = ['dates', 'school', 'name_of_school_head', 'activities_undertaken', 'findings', 'technical_assistance_provided', 'agreement', 'school_head_signature'];
                    foreach ($fields as $field) {
                        $value = htmlspecialchars($entry[$field] ?? '');
                        $type = ($field === 'dates') ? 'date' : 'text';
                        echo "<td><input type='$type' name='{$field}[]' class='form-control form-control-sm' value='$value' " . ($field !== 'findings' && $field !== 'technical_assistance_provided' && $field !== 'agreement' && $field !== 'school_head_signature' ? 'required' : '') . "></td>";
                    }
                    echo '</tr>';
                }
                ?>
            </tbody>
        </table>
    </div>

    <!-- Submit Buttons -->
    <div class="form-group text-center mt-3">
        <button type="submit" name="generate_report" class="btn btn-success px-3 py-1">Generate Report</button>
        <button type="submit" name="save_only" class="btn btn-warning px-3 py-1">Save Only</button>
    </div>
</form>

</body>
</html>