<?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'; // 

// Get POST data and sanitize
$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']);
$password       = $mysqli->real_escape_string($_POST['password']);
$confirm_pass   = $mysqli->real_escape_string($_POST['confirm_password']);

// Simple password match check
if ($password !== $confirm_pass) {
    echo "<script>alert('Passwords do not match.'); window.history.back();</script>";
    exit;
}

// Hash password for security
$hashed_password = md5($password);


// Insert query
$sql = "INSERT INTO clinics (c_name, email, phone, location, designation, specification, pass)
        VALUES ('$clinic_name', '$email', '$phone', '$location', '$designation', '$specification', '$hashed_password')";

if ($mysqli->query($sql) === TRUE) {
    // HTML email content with improved UI
    $to = $email;
    $subject = "Your Clinic Account Has Been Created";
    
    $message = '
    <!DOCTYPE html>
    <html>
    <head>
        <style>
            body {
                font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
                background-color: #f5f7fa;
                margin: 0;
                padding: 0;
                color: #333;
            }
            .email-container {
                max-width: 600px;
                margin: 0 auto;
                background: #ffffff;
                border-radius: 8px;
                overflow: hidden;
                box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            }
            .email-header {
                background: #4f46e5;
                color: white;
                padding: 30px 20px;
                text-align: center;
            }
            .email-body {
                padding: 30px;
            }
            .details-card {
                background: #f8fafc;
                border-radius: 6px;
                padding: 20px;
                margin: 20px 0;
                border-left: 4px solid #4f46e5;
            }
            .detail-item {
                margin-bottom: 12px;
            }
            .detail-label {
                font-weight: 600;
                color: #64748b;
                display: inline-block;
                width: 120px;
            }
            .login-button {
                display: inline-block;
                background: green;
                color: blue;
                text-decoration: none;
                padding: 12px 24px;
                border-radius: 6px;
                font-weight: 500;
                margin: 20px 0;
            }
            .email-footer {
                text-align: center;
                padding: 20px;
                color: #64748b;
                font-size: 14px;
                border-top: 1px solid #e2e8f0;
            }
        </style>
    </head>
    <body>
        <div class="email-container">
            <div class="email-header">
                <h1>Welcome to Phaseon Clinical Research Analysis</h1>
                <p style="color:white">Your clinic account is ready to use</p>
            </div>
            
            <div class="email-body">
                <p>Dear ' . htmlspecialchars($clinic_name) . ',</p>
                <p>Your clinic account has been successfully created with the following credentials:</p>
                
                <div class="details-card">
                    <div class="detail-item">
                        <span class="detail-label">Clinic Name:</span>
                        <strong>' . htmlspecialchars($clinic_name) . '</strong>
                    </div>
                    <div class="detail-item">
                        <span class="detail-label">Email:</span>
                        ' . htmlspecialchars($email) . '
                    </div>
                    <div class="detail-item">
                        <span class="detail-label">Password:</span>
                        ' . htmlspecialchars($password) . '
                    </div>
                    <div class="detail-item">
                        <span class="detail-label">Location:</span>
                        ' . htmlspecialchars($location) . '
                    </div>
                </div>
                
                <p>For security reasons, please change your password after logging in for the first time.</p>
                
                <center>
                    <a href="bhavik.kitret.com/CRA/Clinics/" class="login-button" style="color:white">Access Your Account</a>
                </center>
                
                <p>If you have any questions or didn\'t request this account, please contact our support team immediately.</p>
                
                <p>Best regards,<br>Phaseon Clinical Research Analysis</p>
            </div>
            
            <div class="email-footer">
                <p>&copy; ' . date('Y') . ' Phaseon Clinical Research Analysis. All rights reserved.</p>
                <p>This is an automated message, please do not reply directly to this email.</p>
            </div>
        </div>
    </body>
    </html>';

    // Email headers (unchanged)
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=UTF-8\r\n";
    $headers .= "From: PhaseOn <noreply@yourdomain.com>\r\n";
    $headers .= "Reply-To: support@yourdomain.com\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion();

    mail($to, $subject, $message, $headers);

    echo "<script>alert('Clinic added successfully! Login details sent to email.'); window.location.href='all_clinics.php';</script>";
} else {
    echo "<script>alert('Error: " . $mysqli->error . "'); window.history.back();</script>";
}

$mysqli->close();
?>