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

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    setSetting('shipping', [
        'flat_rate_enabled' => isset($_POST['flat_rate_enabled']),
        'flat_rate_amount' => floatval($_POST['flat_rate_amount'] ?? 0),
        'free_shipping_enabled' => isset($_POST['free_shipping_enabled']),
        'free_shipping_threshold' => floatval($_POST['free_shipping_threshold'] ?? 0),
        'local_pickup_enabled' => isset($_POST['local_pickup_enabled']),
        'processing_time' => trim($_POST['processing_time'] ?? '')
    ]);
    setFlash('success', 'Shipping settings updated');
    header('Location: /admin/shipping.php');
    exit;
}

$shipping = getSetting('shipping', [
    'flat_rate_enabled' => true,
    'flat_rate_amount' => 5.99,
    'free_shipping_enabled' => true,
    'free_shipping_threshold' => 50,
    'local_pickup_enabled' => false,
    'processing_time' => '1-2 business days'
]);
?>

<div class="page-header">
    <h1 class="page-title">Shipping 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;">
    <div>
        <div class="admin-card">
            <div class="admin-card-body" style="padding: 0.5rem;">
                <a href="/admin/settings.php" class="nav-item"><i class="fas fa-store"></i> General</a>
                <a href="/admin/shipping.php" class="nav-item active"><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>
    
    <div>
        <form method="POST">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h3 class="admin-card-title">Shipping Methods</h3>
                </div>
                <div class="admin-card-body">
                    <!-- Flat Rate -->
                    <div style="border: 1px solid var(--admin-border); border-radius: var(--admin-radius); padding: 1rem; margin-bottom: 1rem;">
                        <div class="form-group">
                            <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer; font-weight: 600;">
                                <input type="checkbox" name="flat_rate_enabled" <?= $shipping['flat_rate_enabled'] ? 'checked' : '' ?>>
                                <i class="fas fa-box"></i> Flat Rate Shipping
                            </label>
                        </div>
                        <div class="form-group mb-0">
                            <label class="form-label">Flat Rate Amount</label>
                            <div style="display: flex; align-items: center; gap: 0.5rem;">
                                <span>$</span>
                                <input type="number" name="flat_rate_amount" class="form-input" step="0.01" min="0" 
                                       value="<?= $shipping['flat_rate_amount'] ?>" style="max-width: 120px;">
                            </div>
                        </div>
                    </div>
                    
                    <!-- Free Shipping -->
                    <div style="border: 1px solid var(--admin-border); border-radius: var(--admin-radius); padding: 1rem; margin-bottom: 1rem;">
                        <div class="form-group">
                            <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer; font-weight: 600;">
                                <input type="checkbox" name="free_shipping_enabled" <?= $shipping['free_shipping_enabled'] ? 'checked' : '' ?>>
                                <i class="fas fa-gift"></i> Free Shipping (Threshold)
                            </label>
                        </div>
                        <div class="form-group mb-0">
                            <label class="form-label">Minimum Order for Free Shipping</label>
                            <div style="display: flex; align-items: center; gap: 0.5rem;">
                                <span>$</span>
                                <input type="number" name="free_shipping_threshold" class="form-input" step="0.01" min="0" 
                                       value="<?= $shipping['free_shipping_threshold'] ?>" style="max-width: 120px;">
                            </div>
                        </div>
                    </div>
                    
                    <!-- Local Pickup -->
                    <div style="border: 1px solid var(--admin-border); border-radius: var(--admin-radius); padding: 1rem; margin-bottom: 1rem;">
                        <div class="form-group mb-0">
                            <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer; font-weight: 600;">
                                <input type="checkbox" name="local_pickup_enabled" <?= $shipping['local_pickup_enabled'] ? 'checked' : '' ?>>
                                <i class="fas fa-store"></i> Local Pickup
                            </label>
                            <small class="text-muted">Allow customers to pick up orders at your location</small>
                        </div>
                    </div>
                    
                    <!-- Processing Time -->
                    <div class="form-group mb-0">
                        <label class="form-label">Processing Time</label>
                        <input type="text" name="processing_time" class="form-input" 
                               value="<?= htmlspecialchars($shipping['processing_time']) ?>" 
                               placeholder="e.g., 1-2 business days">
                        <small class="text-muted">Displayed to customers during checkout</small>
                    </div>
                    
                    <button type="submit" class="btn btn-primary mt-2">Save Shipping Settings</button>
                </div>
            </div>
        </form>
    </div>
</div>

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