<?php
require_once 'includes/User.php';
require_once 'includes/db_config.php';

$me = User::getCurrentUser();
$lg = User::isLoggedIn();

$ok_msg = '';
$err_msg = '';

$settings = array();
$db = getDBConnection();
$stmt = $db->query("SELECT setting_key, setting_value, setting_type FROM system_settings");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $settings[$row['setting_key']] = $row['setting_value'];
}

if (empty($settings)) {
    $settings = array(
        'company_address' => 'Riyadh, Saudi Arabia',
        'company_phone' => '0501234567',
        'notification_email' => 'admin@allura.com',
        'working_hours_start' => '08:00',
        'working_hours_end' => '18:00',
        'working_days' => '["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]'
    );
}
$working_days = json_decode(isset($settings['working_days']) ? $settings['working_days'] : '[]', true);
$days_map = array(
    'sunday' => 'Sunday',
    'monday' => 'Monday',
    'tuesday' => 'Tuesday',
    'wednesday' => 'Wednesday',
    'thursday' => 'Thursday',
    'friday' => 'Friday',
    'saturday' => 'Saturday'
);

$work_days = array();
$off_days = array();
$all_days = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
foreach ($all_days as $day) {
    if (in_array($day, $working_days)) {
        $work_days[] = $day;
    } else {
        $off_days[] = $day;
    }
}

$start_time = date('g:i A', strtotime($settings['working_hours_start'] ?? '08:00'));
$end_time = date('g:i A', strtotime($settings['working_hours_end'] ?? '18:00'));

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $subject = trim($_POST['subject'] ?? '');
    $message = trim($_POST['message'] ?? '');
    
    if (empty($name) || empty($email) || empty($subject) || empty($message)) {
        $err_msg = 'Please fill in all required fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $err_msg = 'Please enter a valid email address.';
    } else {
        try {
            $db = getDBConnection();
            $st = $db->prepare("INSERT INTO contact_messages (name, email, phone, subject, message) VALUES (?, ?, ?, ?, ?)");
            $st->execute([$name, $email, $phone, $subject, $message]);
            
            $ok_msg = 'Thanks for reaching out! We\'ll get back to you shortly.';
            
            $name = $email = $phone = $subject = $message = '';
        } catch (PDOException $e) {
            $err_msg = 'Sorry, there was an issue sending your message. Please try again.';
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact Us - Allura Dry Clean</title>
    <?php include 'includes/header.php'; ?>
</head>
<body>
    <div class="contact-container">
        <div class="page-header">
            <h1>Contact</h1>
            <p>We're here to help! Send us a message and we'll respond as soon as we can.</p>
        </div>
        
        <div class="contact-content">
            <div class="contact-info">
                <div class="contact-card">
                    <div class="contact-icon">
                        <i class="fas fa-map-marker-alt"></i>
                    </div>
                    <h3>Our Location</h3>
                    <p><?php echo htmlspecialchars($settings['company_address'] ?? 'N/A'); ?></p>
                </div>
                
                <div class="contact-card">
                    <div class="contact-icon">
                        <i class="fas fa-phone"></i>
                    </div>
                    <h3>Call Us</h3>
                    <p><?php echo htmlspecialchars($settings['company_phone'] ?? 'N/A'); ?></p>
                </div>
                
                <div class="contact-card">
                    <div class="contact-icon">
                        <i class="fas fa-envelope"></i>
                    </div>
                    <h3>Email Us</h3>
                    <p><?php echo htmlspecialchars($settings['notification_email'] ?? 'N/A'); ?></p>
                </div>
                
                <div class="contact-card">
                    <div class="contact-icon">
                        <i class="fas fa-clock"></i>
                    </div>
                    <h3>Hours</h3>
                    <p>
                        <?php 
                        if (count($work_days) > 0) {
                            if (count($work_days) >= 5) {
                                echo ucfirst($work_days[0]) . ' - ' . ucfirst(end($work_days)) . ': ' . $start_time . ' - ' . $end_time;
                            } else {
                                echo implode(', ', array_map('ucfirst', $work_days)) . ': ' . $start_time . ' - ' . $end_time;
                            }
                        }
                        
                        if (count($off_days) > 0) {
                            echo '<br>' . implode(', ', array_map('ucfirst', $off_days)) . ': Closed';
                        }
                        ?>
                    </p>
                </div>
            </div>
            
            <div class="contact-form-container">
                <form class="contact-form" method="POST" action="">
                    <h2>Send a Message</h2>
                    
                    <?php if (!empty($ok_msg)): ?>
                        <div class="success-message">
                            <i class="fas fa-check-circle"></i>
                            <?php echo htmlspecialchars($ok_msg); ?>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (!empty($err_msg)): ?>
                        <div class="error-message">
                            <i class="fas fa-exclamation-circle"></i>
                            <?php echo htmlspecialchars($err_msg); ?>
                        </div>
                    <?php endif; ?>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label for="name">Full Name *</label>
                            <input type="text" id="name" name="name" value="<?php echo htmlspecialchars($name ?? ''); ?>" required>
                        </div>
                        
                        <div class="form-group">
                            <label for="email">Email Address *</label>
                            <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($email ?? ''); ?>" required>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label for="phone">Phone Number</label>
                            <input type="tel" id="phone" name="phone" value="<?php echo htmlspecialchars($phone ?? ''); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="subject">Subject *</label>
                            <input type="text" id="subject" name="subject" value="<?php echo htmlspecialchars($subject ?? ''); ?>" required>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label for="message">Your Message *</label>
                        <textarea id="message" name="message" rows="6" required><?php echo htmlspecialchars($message ?? ''); ?></textarea>
                    </div>
                    
                    <button type="submit" class="btn">
                        <i class="fas fa-paper-plane"></i>
                        Send Message
                    </button>
                </form>
            </div>
        </div>
    </div>
    <?php include 'includes/footer.php'; ?>
</body>
</html>
