<?php
ob_start();
/**
 * Tom's Java Jive - Admin Settings
 */

$pageTitle = 'Store Settings';
require_once __DIR__ . '/includes/header.php';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $section = $_POST['section'] ?? '';
    
    if ($section === 'general') {
        setSetting('store', [
            'name' => trim($_POST['store_name'] ?? ''),
            'email' => trim($_POST['store_email'] ?? ''),
            'phone' => trim($_POST['store_phone'] ?? ''),
            'address' => trim($_POST['store_address'] ?? ''),
            'currency' => $_POST['currency'] ?? 'USD',
            'timezone' => $_POST['timezone'] ?? 'America/New_York'
        ]);
        setFlash('success', 'Store settings updated');
    }
    
    if ($section === 'tax') {
        setSetting('tax', [
            'enabled' => isset($_POST['tax_enabled']),
            'rate' => floatval($_POST['tax_rate'] ?? 0),
            'included_in_price' => isset($_POST['tax_included'])
        ]);
        setFlash('success', 'Tax settings updated');
    }
    
    if ($section === 'checkout') {
        setSetting('checkout', [
            'guest_checkout' => isset($_POST['guest_checkout']),
            'require_phone' => isset($_POST['require_phone']),
            'order_notes' => isset($_POST['order_notes']),
            'terms_required' => isset($_POST['terms_required']),
            'terms_url' => trim($_POST['terms_url'] ?? '')
        ]);
        setFlash('success', 'Checkout settings updated');
    }
    
    header('Location: /admin/settings.php');
    exit;
}

// Get current settings
$store = getSetting('store', [
    'name' => "Tom's Java Jive",
    'email' => '',
    'phone' => '',
    'address' => '',
    'currency' => 'USD',
    'timezone' => 'America/New_York'
]);

$tax = getSetting('tax', [
    'enabled' => false,
    'rate' => 0,
    'included_in_price' => false
]);

$checkout = getSetting('checkout', [
    'guest_checkout' => true,
    'require_phone' => false,
    'order_notes' => true,
    'terms_required' => false,
    'terms_url' => ''
]);
?>

<div class="page-header">
    <h1 class="page-title">Store Settings</h1>
</div>

<?php if (hasFlash('success')): ?>
    <div class="alert alert-success"><i class="fas fa-check-circle"></i> <?= getFlash('success') ?></div>
<?php endif; ?>

<div style="display: grid; grid-template-columns: 200px 1fr; gap: 1.5rem;">
    <!-- Sidebar Navigation -->
    <div>
        <div class="admin-card">
            <div class="admin-card-body" style="padding: 0.5rem;">
                <a href="/admin/settings.php" class="nav-item active"><i class="fas fa-store"></i> General</a>
                <a href="/admin/shipping.php" class="nav-item"><i class="fas fa-truck"></i> Shipping</a>
                <a href="/admin/payments.php" class="nav-item"><i class="fas fa-credit-card"></i> Payments</a>
                <a href="/admin/emails.php" class="nav-item"><i class="fas fa-envelope"></i> Emails</a>
            </div>
        </div>
    </div>
    
    <!-- Settings Forms -->
    <div>
        <!-- General Settings -->
        <form method="POST">
            <input type="hidden" name="section" value="general">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h3 class="admin-card-title">General Settings</h3>
                </div>
                <div class="admin-card-body">
                    <div class="form-group">
                        <label class="form-label">Store Name</label>
                        <input type="text" name="store_name" class="form-input" value="<?= htmlspecialchars($store['name']) ?>">
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">Store Email</label>
                            <input type="email" name="store_email" class="form-input" value="<?= htmlspecialchars($store['email']) ?>">
                        </div>
                        <div class="form-group">
                            <label class="form-label">Store Phone</label>
                            <input type="text" name="store_phone" class="form-input" value="<?= htmlspecialchars($store['phone']) ?>">
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Store Address</label>
                        <textarea name="store_address" class="form-textarea" rows="2"><?= htmlspecialchars($store['address']) ?></textarea>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">Currency</label>
                            <select name="currency" class="form-select">
                                <option value="USD" <?= $store['currency'] === 'USD' ? 'selected' : '' ?>>USD ($)</option>
                                <option value="EUR" <?= $store['currency'] === 'EUR' ? 'selected' : '' ?>>EUR (€)</option>
                                <option value="GBP" <?= $store['currency'] === 'GBP' ? 'selected' : '' ?>>GBP (£)</option>
                                <option value="CAD" <?= $store['currency'] === 'CAD' ? 'selected' : '' ?>>CAD ($)</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label class="form-label">Timezone</label>
                            <select name="timezone" class="form-select">
                                <option value="America/New_York" <?= $store['timezone'] === 'America/New_York' ? 'selected' : '' ?>>Eastern Time</option>
                                <option value="America/Chicago" <?= $store['timezone'] === 'America/Chicago' ? 'selected' : '' ?>>Central Time</option>
                                <option value="America/Denver" <?= $store['timezone'] === 'America/Denver' ? 'selected' : '' ?>>Mountain Time</option>
                                <option value="America/Los_Angeles" <?= $store['timezone'] === 'America/Los_Angeles' ? 'selected' : '' ?>>Pacific Time</option>
                            </select>
                        </div>
                    </div>
                    
                    <button type="submit" class="btn btn-primary">Save General Settings</button>
                </div>
            </div>
        </form>
        
        <!-- Tax Settings -->
        <form method="POST">
            <input type="hidden" name="section" value="tax">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h3 class="admin-card-title">Tax Settings</h3>
                </div>
                <div class="admin-card-body">
                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="tax_enabled" <?= $tax['enabled'] ? 'checked' : '' ?>>
                            Enable tax calculation
                        </label>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">Tax Rate (%)</label>
                        <input type="number" name="tax_rate" class="form-input" step="0.01" min="0" max="100" value="<?= $tax['rate'] ?>" style="max-width: 150px;">
                    </div>
                    
                    <div class="form-group mb-0">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="tax_included" <?= $tax['included_in_price'] ? 'checked' : '' ?>>
                            Prices include tax
                        </label>
                    </div>
                    
                    <button type="submit" class="btn btn-primary mt-2">Save Tax Settings</button>
                </div>
            </div>
        </form>
        
        <!-- Checkout Settings -->
        <form method="POST">
            <input type="hidden" name="section" value="checkout">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h3 class="admin-card-title">Checkout Settings</h3>
                </div>
                <div class="admin-card-body">
                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="guest_checkout" <?= $checkout['guest_checkout'] ? 'checked' : '' ?>>
                            Allow guest checkout
                        </label>
                    </div>
                    
                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="require_phone" <?= $checkout['require_phone'] ? 'checked' : '' ?>>
                            Require phone number
                        </label>
                    </div>
                    
                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="order_notes" <?= $checkout['order_notes'] ? 'checked' : '' ?>>
                            Allow order notes
                        </label>
                    </div>
                    
                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="terms_required" <?= $checkout['terms_required'] ? 'checked' : '' ?>>
                            Require terms acceptance
                        </label>
                    </div>
                    
                    <div class="form-group mb-0">
                        <label class="form-label">Terms & Conditions URL</label>
                        <input type="url" name="terms_url" class="form-input" value="<?= htmlspecialchars($checkout['terms_url']) ?>" placeholder="https://example.com/terms">
                    </div>
                    
                    <button type="submit" class="btn btn-primary mt-2">Save Checkout Settings</button>
                </div>
            </div>
        </form>
    </div>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
