<?php
session_start();

if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}

// Optional: access admin ID like this
$admin_id = $_SESSION['admin_id'];
include 'dbconn.php'; 

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $id           = intval($_POST['id']);
    $clinic_name  = $mysqli->real_escape_string($_POST['clinic_name']);
    $email        = $mysqli->real_escape_string($_POST['email']);
    $phone        = $mysqli->real_escape_string($_POST['phone']);
    $location     = $mysqli->real_escape_string($_POST['location']);
    $designation  = $mysqli->real_escape_string($_POST['designation']);
    $specification= $mysqli->real_escape_string($_POST['specification']);

    $sql = "UPDATE clinics SET 
                c_name = '$clinic_name',
                email = '$email',
                phone = '$phone',
                location = '$location',
                designation = '$designation',
                specification = '$specification'
            WHERE id = $id";

    if ($mysqli->query($sql) === TRUE) {
        echo "<script>alert('Clinic updated successfully!'); window.location.href='all_clinics.php';</script>";
    } else {
        echo "<script>alert('Error: " . $mysqli->error . "'); window.history.back();</script>";
    }
    $mysqli->close();
} else {
    echo "<script>alert('Invalid request.'); window.history.back();</script>";
}
?>