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

// Definisanje dostupnih metoda plaćanja
$available_payment_methods = [
    'cash' => 'Plaćanje pouzećem',
    'bank_transfer' => 'Bankarski transfer',
    'card' => 'Kreditna/debitna kartica',
    'paypal' => 'PayPal',
    'stripe' => 'Stripe'
];

// Dohvatanje trenutnih podešavanja
$current_active_methods = get_setting('active_payment_methods', '');
$current_active_methods = !empty($current_active_methods) ? explode(',', $current_active_methods) : [];

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

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        // Aktivni načini plaćanja
        $active_methods = isset($_POST['payment_methods']) ? $_POST['payment_methods'] : [];
        $active_methods_string = implode(',', $active_methods);
        
        execute_query("
            INSERT INTO settings (setting_key, setting_value) 
            VALUES ('active_payment_methods', ?) 
            ON DUPLICATE KEY UPDATE setting_value = ?
        ", [$active_methods_string, $active_methods_string]);

        // PayPal konfiguracija
        $paypal_data = [
            'client_id' => trim($_POST['paypal_client_id'] ?? ''),
            'client_secret' => trim($_POST['paypal_client_secret'] ?? ''),
            'mode' => $_POST['paypal_mode'] ?? 'sandbox'
        ];
        
        execute_query("
            INSERT INTO settings (setting_key, setting_value) 
            VALUES ('paypal_config', ?) 
            ON DUPLICATE KEY UPDATE setting_value = ?
        ", [json_encode($paypal_data), json_encode($paypal_data)]);

        // Stripe konfiguracija
        $stripe_data = [
            'public_key' => trim($_POST['stripe_public_key'] ?? ''),
            'secret_key' => trim($_POST['stripe_secret_key'] ?? ''),
            'webhook_secret' => trim($_POST['stripe_webhook_secret'] ?? '')
        ];
        
        execute_query("
            INSERT INTO settings (setting_key, setting_value) 
            VALUES ('stripe_config', ?) 
            ON DUPLICATE KEY UPDATE setting_value = ?
        ", [json_encode($stripe_data), json_encode($stripe_data)]);

        $success = 'Podešavanja plaćanja su uspešno sačuvana.';

        // Osvežavanje trenutnih vrednosti
        $current_active_methods = $active_methods;
        $paypal_config = $paypal_data;
        $stripe_config = $stripe_data;

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

// Naslov stranice
$page_title = 'Načini plaćanja';

// 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 active">
                                    <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">
                                    <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">Aktivni načini plaćanja</h3>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <?php foreach ($available_payment_methods as $method_key => $method_name): ?>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <div class="custom-control custom-checkbox">
                                            <input 
                                                type="checkbox" 
                                                class="custom-control-input" 
                                                id="payment_method_<?php echo $method_key; ?>" 
                                                name="payment_methods[]" 
                                                value="<?php echo $method_key; ?>"
                                                <?php echo in_array($method_key, $current_active_methods) ? 'checked' : ''; ?>
                                            >
                                            <label 
                                                class="custom-control-label" 
                                                for="payment_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">PayPal konfiguracija</h3>
                        </div>
                        <div class="card-body">
                            <div class="form-group">
                                <label for="paypal_client_id">PayPal Client ID</label>
                                <input 
                                    type="text" 
                                    class="form-control" 
                                    id="paypal_client_id" 
                                    name="paypal_client_id"
                                    value="<?php echo htmlspecialchars($paypal_config['client_id'] ?? ''); ?>"
                                >
                            </div>
                            
                            <div class="form-group">
                                <label for="paypal_client_secret">PayPal Client Secret</label>
                                <input 
                                    type="password" 
                                    class="form-control" 
                                    id="paypal_client_secret" 
                                    name="paypal_client_secret"
                                    value="<?php echo htmlspecialchars($paypal_config['client_secret'] ?? ''); ?>"
                                >
                            </div>
                            
                            <div class="form-group">
                                <label for="paypal_mode">PayPal Mod</label>
                                <select 
                                    class="form-control" 
                                    id="paypal_mode" 
                                    name="paypal_mode"
                                >
                                    <option 
                                        value="sandbox" 
                                        <?php echo ($paypal_config['mode'] ?? '') === 'sandbox' ? 'selected' : ''; ?>
                                    >
                                        Sandbox (Test)
                                    </option>
                                    <option 
                                        value="live" 
                                        <?php echo ($paypal_config['mode'] ?? '') === 'live' ? 'selected' : ''; ?>
                                    >
                                        Live (Produkcija)
                                    </option>
                                </select>
                            </div>
                        </div>
                    </div>
                    
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Stripe konfiguracija</h3>
                        </div>
                        <div class="card-body">
                            <div class="form-group">
                                <label for="stripe_public_key">Stripe Javni Ključ</label>
                                <input 
                                    type="text" 
                                    class="form-control" 
                                    id="stripe_public_key" 
                                    name="stripe_public_key"
                                    value="<?php echo htmlspecialchars($stripe_config['public_key'] ?? ''); ?>"
                                >
                            </div>
                            
                            <div class="form-group">
                                <label for="stripe_secret_key">Stripe Tajni Ključ</label>
                                <input 
                                    type="password" 
                                    class="form-control" 
                                    id="stripe_secret_key" 
                                    name="stripe_secret_key"
                                    value="<?php echo htmlspecialchars($stripe_config['secret_key'] ?? ''); ?>"
                                >
                            </div>
                            
                            <div class="form-group">
                                <label for="stripe_webhook_secret">Stripe Webhook Secret</label>
                                <input 
                                    type="password" 
                                    class="form-control" 
                                    id="stripe_webhook_secret" 
                                    name="stripe_webhook_secret"
                                    value="<?php echo htmlspecialchars($stripe_config['webhook_secret'] ?? ''); ?>"
                                >
                            </div>
                        </div>
                    </div>
                    
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Dodatne opcije</h3>
                        </div>
                        <div class="card-body">
                            <div class="form-group">
                                <label for="minimum_online_payment">Minimalni iznos za online plaćanje</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="minimum_online_payment" 
                                        name="minimum_online_payment"
                                        value="<?php echo get_setting('minimum_online_payment', 1000); ?>"
                                        min="0" 
                                        step="100"
                                    >
                                </div>
                                <small class="form-text text-muted">Minimalni iznos za plaćanje online metodama</small>
                            </div>
                            
                            <div class="form-group">
                                <div class="custom-control custom-checkbox">
                                    <input 
                                        type="checkbox" 
                                        class="custom-control-input" 
                                        id="enable_international_payments" 
                                        name="enable_international_payments"
                                        <?php echo get_setting('enable_international_payments', 0) ? 'checked' : ''; ?>
                                    >
                                    <label 
                                        class="custom-control-label" 
                                        for="enable_international_payments"
                                    >
                                        Omogući međunarodna plaćanja
                                    </label>
                                </div>
                            </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'; ?>