<?php
session_start();
require_once '../config.php';

if (!isset($_SESSION['user_id']) || !isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    die("Access Denied");
}

function e($v) {
    return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8');
}

$message = '';
$messageType = '';

$pharmacy_name = '';
$owner_name    = '';
$phone         = '';
$address       = '';
$city          = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pharmacy_name = trim($_POST['pharmacy_name'] ?? '');
    $owner_name    = trim($_POST['owner_name'] ?? '');
    $phone         = trim($_POST['phone'] ?? '');
    $address       = trim($_POST['address'] ?? '');
    $city          = trim($_POST['city'] ?? '');

    if ($pharmacy_name === '') {
        $message = 'Pharmacy name is required.';
        $messageType = 'error';
    } else {
        $stmt = $conn->prepare("
            INSERT INTO pharmacies (pharmacy_name, owner_name, phone, address, city)
            VALUES (?, ?, ?, ?, ?)
        ");

        if (!$stmt) {
            $message = 'Database prepare failed: ' . $conn->error;
            $messageType = 'error';
        } else {
            $stmt->bind_param("sssss", $pharmacy_name, $owner_name, $phone, $address, $city);

            if ($stmt->execute()) {
                $stmt->close();
                header("Location: " . $_SERVER['PHP_SELF'] . "?success=1");
                exit;
            } else {
                $message = 'Failed to add pharmacy: ' . $stmt->error;
                $messageType = 'error';
                $stmt->close();
            }
        }
    }
}

if (isset($_GET['success'])) {
    $message = 'Pharmacy added successfully.';
    $messageType = 'success';
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Add Pharmacy</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<style>
    * { box-sizing: border-box; }
    body {
        margin: 0;
        font-family: Inter, Arial, sans-serif;
        background: linear-gradient(180deg, #f8fafc 0%, #eef2f7 100%);
        color: #0f172a;
    }
    .page {
        padding: 70px;
        margin-left: 260px;
    }
    .wrap {
        max-width: 980px;
        margin: 0 auto;
    }
    .hero {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 16px;
        margin-bottom: 18px;
        flex-wrap: wrap;
    }
    .hero h1 {
        margin: 0;
        font-size: 28px;
        line-height: 1.1;
    }
    .hero p {
        margin: 8px 0 0;
        color: #64748b;
        font-size: 14px;
    }
    .card {
        background: #fff;
        border: 1px solid #e2e8f0;
        border-radius: 22px;
        box-shadow: 0 10px 30px rgba(15, 23, 42, 0.06);
        overflow: hidden;
    }
    .card-head {
        padding: 18px 22px;
        border-bottom: 1px solid #e2e8f0;
        background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
    }
    .card-head h2 {
        margin: 0;
        font-size: 18px;
    }
    .card-head small {
        display: block;
        margin-top: 6px;
        color: #64748b;
    }
    .alert {
        margin: 18px 22px 0;
        padding: 14px 16px;
        border-radius: 14px;
        font-size: 14px;
        font-weight: 600;
    }
    .alert.success {
        background: #ecfdf5;
        color: #047857;
        border: 1px solid #a7f3d0;
    }
    .alert.error {
        background: #fef2f2;
        color: #b91c1c;
        border: 1px solid #fecaca;
    }
    .form {
        padding: 22px;
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 18px;
    }
    .field {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }
    .field.full {
        grid-column: 1 / -1;
    }
    label {
        font-size: 13px;
        font-weight: 700;
        color: #334155;
    }
    .input, .textarea {
        width: 100%;
        border: 1px solid #dbe4ee;
        background: #fbfdff;
        border-radius: 14px;
        padding: 12px 14px;
        font-size: 14px;
        outline: none;
        transition: 0.2s ease;
        color: #0f172a;
    }
    .input:focus, .textarea:focus {
        border-color: #2563eb;
        box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.10);
        background: #fff;
    }
    .textarea {
        min-height: 120px;
        resize: vertical;
    }
    .help {
        font-size: 12px;
        color: #64748b;
    }
    .actions {
        grid-column: 1 / -1;
        display: flex;
        justify-content: flex-end;
        gap: 10px;
        padding-top: 4px;
    }
    .btn {
        border: none;
        border-radius: 14px;
        padding: 12px 18px;
        font-size: 14px;
        font-weight: 700;
        cursor: pointer;
        text-decoration: none;
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }
    .btn-primary {
        background: #1f2937;
        color: #fff;
    }
    .btn-primary:hover {
        background: #111827;
    }
    .btn-light {
        background: #e2e8f0;
        color: #0f172a;
    }
    .btn-light:hover {
        background: #cbd5e1;
    }
    .summary {
        margin: 0 22px 22px;
        padding: 16px;
        border-radius: 18px;
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        color: #334155;
        font-size: 13px;
        line-height: 1.6;
    }
    .summary strong {
        color: #0f172a;
    }
    @media (max-width: 768px) {
        .page {
            margin-left: 0;
            padding: 14px;
        }
        .form {
            grid-template-columns: 1fr;
            padding: 16px;
        }
        .hero h1 {
            font-size: 22px;
        }
        .actions {
            flex-direction: column;
        }
        .btn {
            width: 100%;
            justify-content: center;
        }
    }
</style>
</head>
<body>

<?php include 'adminsidebar.php'; ?>
<?php include '../header.php'; ?>

<div class="page">
    <div class="wrap">
        <div class="hero">
            <div>
                <h1>Add Pharmacy</h1>
                <p>Admin can add pharmacy details here.</p>
            </div>
        </div>

        <div class="card">
            <div class="card-head">
                <h2><i class="fa-solid fa-prescription-bottle-medical"></i> Pharmacy Information</h2>
                <small>Fill the required fields and save the pharmacy record.</small>
            </div>

            <?php if ($message): ?>
                <div class="alert <?php echo $messageType === 'success' ? 'success' : 'error'; ?>">
                    <?php echo e($message); ?>
                </div>
            <?php endif; ?>

            <form method="post" class="form" autocomplete="off">
                <div class="field full">
                    <label for="pharmacy_name">Pharmacy Name *</label>
                    <input
                        class="input"
                        type="text"
                        id="pharmacy_name"
                        name="pharmacy_name"
                        value="<?php echo e($pharmacy_name); ?>"
                        placeholder="Enter pharmacy name"
                        required
                    >
                </div>

                <div class="field">
                    <label for="owner_name">Owner Name</label>
                    <input
                        class="input"
                        type="text"
                        id="owner_name"
                        name="owner_name"
                        value="<?php echo e($owner_name); ?>"
                        placeholder="Enter owner name"
                    >
                </div>

                <div class="field">
                    <label for="phone">Phone Number</label>
                    <input
                        class="input"
                        type="text"
                        id="phone"
                        name="phone"
                        value="<?php echo e($phone); ?>"
                        placeholder="Enter phone number"
                    >
                </div>

                <div class="field full">
                    <label for="address">Address</label>
                    <textarea
                        class="textarea"
                        id="address"
                        name="address"
                        placeholder="Enter full address"
                    ><?php echo e($address); ?></textarea>
                </div>

                <div class="field full">
                    <label for="city">City</label>
                    <input
                        class="input"
                        type="text"
                        id="city"
                        name="city"
                        value="<?php echo e($city); ?>"
                        placeholder="Enter city"
                    >
                </div>

                <div class="actions">
                    <a href="javascript:history.back()" class="btn btn-light">
                        <i class="fa-solid fa-arrow-left"></i> Back
                    </a>
                    <button type="submit" class="btn btn-primary">
                        <i class="fa-solid fa-plus"></i> Add Pharmacy
                    </button>
                </div>
            </form>

            
        </div>
    </div>
</div>

</body>
</html>