<?php
require_once '../../includes/config.php';
require_once '../../includes/db.php';
require_once '../../includes/functions.php';
require_once '../includes/admin-functions.php';

// Provera da li je korisnik prijavljen kao admin
if (!is_admin()) {
    header('Location: /admin/login.php');
    exit;
}

$errors = [];
$success = '';

// Dostupne metode dostave
$shipping_methods = [
    'personal_pickup' => 'Lično preuzimanje',
    'standard_delivery' => 'Standardna dostava',
    'express_delivery' => 'Brza dostava',
    'weekend_delivery' => 'Dostava vikendom'
];

// Trenutna podešavanja dostave
$active_shipping_methods = get_setting('active_shipping_methods', '');
$active_shipping_methods = !empty($active_shipping_methods) ? explode(',', $active_shipping_methods) : [];

$shipping_prices = get_setting('shipping_prices', '{}');
$shipping_prices = json_decode($shipping_prices, true) ?: [];

$free_shipping_threshold = get_setting('free_shipping_threshold', 0);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        // Aktivne metode dostave
        $active_methods = isset($_POST['shipping_methods']) ? $_POST['shipping_methods'] : [];
        $active_methods_string = implode(',', $active_methods);
        
        execute_query("
            INSERT INTO settings (setting_key, setting_value) 
            VALUES ('active_shipping_methods', ?) 
            ON DUPLICATE KEY UPDATE setting_value = ?
        ", [$active_methods_string, $active_methods_string]);

        // Cene dostave
        $new_shipping_prices = [];
        foreach ($shipping_methods as $method_key => $method_name) {
            $price = floatval($_POST['shipping_price'][$method_key] ?? 0);
            $new_shipping_prices[$method_key] = $price;
        }
        
        execute_query("
            INSERT INTO settings (setting_key, setting_value) 
            VALUES ('shipping_prices', ?) 
            ON DUPLICATE KEY UPDATE setting_value = ?
        ", [json_encode($new_shipping_prices), json_encode($new_shipping_prices)]);

        // Besplatna dostava
        $free_shipping = floatval($_POST['free_shipping_threshold'] ?? 0);
        
        execute_query("
            INSERT INTO settings (setting_key, setting_value) 
            VALUES ('free_shipping_threshold', ?) 
            ON DUPLICATE KEY UPDATE setting_value = ?
        ", [$free_shipping, $free_shipping]);

        $success = 'Podešavanja dostave su uspešno sačuvana.';

        // Osvežavanje trenutnih vrednosti
        $active_shipping_methods = $active_methods;
        $shipping_prices = $new_shipping_prices;
        $free_shipping_threshold = $free_shipping;

    } catch (Exception $e) {
        $errors[] = 'Došlo je do greške: ' . $e->getMessage();
    }
}

// Naslov stranice
$page_title = 'Dostava';

// Uključivanje header-a
require_once '../includes/header.php';
?>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0">Podešavanja</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="/admin/">Kontrolna tabla</a></li>
                    <li class="breadcrumb-item active">Podešavanja</li>
                </ol>
            </div>
        </div>
    </div>
</div>

<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-3">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">Kategorije podešavanja</h3>
                    </div>
                    <div class="card-body p-0">
                        <ul class="nav nav-pills flex-column">
                            <li class="nav-item">
                                <a href="/admin/settings/general.php" class="nav-link">
                                    <i class="fas fa-cog mr-2"></i> Opšta podešavanja
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="/admin/settings/payment.php" class="nav-link">
                                    <i class="fas fa-credit-card mr-2"></i> Načini plaćanja
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="/admin/settings/shipping.php" class="nav-link active">
                                    <i class="fas fa-truck mr-2"></i> Dostava
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="/admin/settings/email.php" class="nav-link">
                                    <i class="fas fa-envelope mr-2"></i> Email podešavanja
                                </a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="col-md-9">
                <?php if (!empty($errors)): ?>
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        <?php foreach ($errors as $error): ?>
                        <li><?php echo $error; ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
                <?php endif; ?>
                
                <?php if (!empty($success)): ?>
                <div class="alert alert-success">
                    <?php echo $success; ?>
                </div>
                <?php endif; ?>
                
                <form action="" method="post">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Aktivne metode dostave</h3>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <?php foreach ($shipping_methods as $method_key => $method_name): ?>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <div class="custom-control custom-checkbox">
                                            <input 
                                                type="checkbox" 
                                                class="custom-control-input" 
                                                id="shipping_method_<?php echo $method_key; ?>" 
                                                name="shipping_methods[]" 
                                                value="<?php echo $method_key; ?>"
                                                <?php echo in_array($method_key, $active_shipping_methods) ? 'checked' : ''; ?>
                                            >
                                            <label 
                                                class="custom-control-label" 
                                                for="shipping_method_<?php echo $method_key; ?>"
                                            >
                                                <?php echo $method_name; ?>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>
                    
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Cene dostave</h3>
                        </div>
                        <div class="card-body">
                            <?php foreach ($shipping_methods as $method_key => $method_name): ?>
                            <div class="form-group">
                                <label for="shipping_price_<?php echo $method_key; ?>">
                                    <?php echo $method_name; ?>
                                </label>
                                <div class="input-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text">RSD</span>
                                    </div>
                                    <input 
                                        type="number" 
                                        class="form-control" 
                                        id="shipping_price_<?php echo $method_key; ?>" 
                                        name="shipping_price[<?php echo $method_key; ?>]"
                                        value="<?php echo $shipping_prices[$method_key] ?? 0; ?>"
                                        min="0" 
                                        step="50"
                                    >
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                    
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Besplatna dostava</h3>
                        </div>
                        <div class="card-body">
                            <div class="form-group">
                                <label for="free_shipping_threshold">Besplatna dostava iznad</label>
                                <div class="input-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text">RSD</span>
                                    </div>
                                    <input 
                                        type="number" 
                                        class="form-control" 
                                        id="free_shipping_threshold" 
                                        name="free_shipping_threshold"
                                        value="<?php echo $free_shipping_threshold; ?>"
                                        min="0" 
                                        step="100"
                                    >
                                </div>
                                <small class="form-text text-muted">
                                    Unesite iznos iznad kojeg je dostava besplatna. 
                                    Postavite na 0 ako ne želite besplatnu dostavu.
                                </small>
                            </div>
                        </div>
                    </div>
                    
                    <div class="card">
                        <div class="card-body">
                            <button type="submit" class="btn btn-primary btn-block">
                                Sačuvaj podešavanja
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>

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