<?php
require_once "config.php";
require_once "auth.php";

// Only admin can access this page
requireAdmin();

$message = '';
$error = '';

// Get komitent ID from URL
$komitent_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// Get komitent data
$stmt = $conn->prepare("SELECT * FROM komitenti WHERE id = ?");
$stmt->bind_param("i", $komitent_id);
$stmt->execute();
$result = $stmt->get_result();
$komitent = $result->fetch_assoc();

if (!$komitent) {
    header('Location: komitenti.php');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $naziv = trim($_POST['naziv'] ?? '');
    $grad = trim($_POST['grad'] ?? '');
    $kontakt_osoba = trim($_POST['kontakt_osoba'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $adresa = trim($_POST['adresa'] ?? '');
    $telefon = trim($_POST['telefon'] ?? '');
    $pib = trim($_POST['pib'] ?? '');
    $maticni_broj = trim($_POST['maticni_broj'] ?? '');
    $cena_do_aerodroma = !empty($_POST['cena_do_aerodroma']) ? $_POST['cena_do_aerodroma'] : null;
    $cena_od_aerodroma = !empty($_POST['cena_od_aerodroma']) ? $_POST['cena_od_aerodroma'] : null;
    
    if (empty($naziv) || empty($grad)) {
        $error = "Naziv i grad su obavezna polja.";
    } elseif (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = "Email adresa nije validna.";
    } else {
        if ($cena_do_aerodroma !== null && $cena_do_aerodroma !== '') {
            $cena_do_aerodroma = floatval($cena_do_aerodroma);
        } else {
            $cena_do_aerodroma = null;
        }
        if ($cena_od_aerodroma !== null && $cena_od_aerodroma !== '') {
            $cena_od_aerodroma = floatval($cena_od_aerodroma);
        } else {
            $cena_od_aerodroma = null;
        }
        
        if ($cena_do_aerodroma === null && $cena_od_aerodroma === null) {
            $stmt = $conn->prepare("UPDATE komitenti SET naziv = ?, grad = ?, kontakt_osoba = ?, email = ?, adresa = ?, telefon = ?, pib = ?, maticni_broj = ?, cena_do_aerodroma = NULL, cena_od_aerodroma = NULL WHERE id = ?");
            $stmt->bind_param("ssssssssi", $naziv, $grad, $kontakt_osoba, $email, $adresa, $telefon, $pib, $maticni_broj, $komitent_id);
        } elseif ($cena_do_aerodroma === null) {
            $stmt = $conn->prepare("UPDATE komitenti SET naziv = ?, grad = ?, kontakt_osoba = ?, email = ?, adresa = ?, telefon = ?, pib = ?, maticni_broj = ?, cena_do_aerodroma = NULL, cena_od_aerodroma = ? WHERE id = ?");
            $stmt->bind_param("ssssssssdi", $naziv, $grad, $kontakt_osoba, $email, $adresa, $telefon, $pib, $maticni_broj, $cena_od_aerodroma, $komitent_id);
        } elseif ($cena_od_aerodroma === null) {
            $stmt = $conn->prepare("UPDATE komitenti SET naziv = ?, grad = ?, kontakt_osoba = ?, email = ?, adresa = ?, telefon = ?, pib = ?, maticni_broj = ?, cena_do_aerodroma = ?, cena_od_aerodroma = NULL WHERE id = ?");
            $stmt->bind_param("ssssssssdi", $naziv, $grad, $kontakt_osoba, $email, $adresa, $telefon, $pib, $maticni_broj, $cena_do_aerodroma, $komitent_id);
        } else {
            $stmt = $conn->prepare("UPDATE komitenti SET naziv = ?, grad = ?, kontakt_osoba = ?, email = ?, adresa = ?, telefon = ?, pib = ?, maticni_broj = ?, cena_do_aerodroma = ?, cena_od_aerodroma = ? WHERE id = ?");
            $stmt->bind_param("ssssssssddi", $naziv, $grad, $kontakt_osoba, $email, $adresa, $telefon, $pib, $maticni_broj, $cena_do_aerodroma, $cena_od_aerodroma, $komitent_id);
        }
        
        if ($stmt->execute()) {
            $message = "Komitent je uspešno ažuriran.";
            // Refresh komitent data
            $stmt = $conn->prepare("SELECT * FROM komitenti WHERE id = ?");
            $stmt->bind_param("i", $komitent_id);
            $stmt->execute();
            $result = $stmt->get_result();
            $komitent = $result->fetch_assoc();
        } else {
            $error = "Greška prilikom ažuriranja komitenta.";
        }
    }
}
?>

    <?php include 'header.php'; ?>
    
    <div class="container main-container mt-4">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-body">
                        <h2 class="card-title mb-4">Izmena komitenta</h2>
                        
                        <?php if ($message): ?>
                            <div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
                        <?php endif; ?>
                        
                        <?php if ($error): ?>
                            <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
                        <?php endif; ?>
                        
                        <form method="POST" action="">
                            <div class="mb-3">
                                <label for="naziv" class="form-label">Naziv</label>
                                <input type="text" class="form-control" id="naziv" name="naziv" value="<?php echo htmlspecialchars($komitent['naziv']); ?>" required>
                            </div>
                            
                            <div class="mb-3">
                                <label for="grad" class="form-label">Grad</label>
                                <input type="text" class="form-control" id="grad" name="grad" value="<?php echo htmlspecialchars($komitent['grad']); ?>" required>
                            </div>
                            
                            <div class="mb-3">
                                <label for="adresa" class="form-label">Adresa</label>
                                <input type="text" class="form-control" id="adresa" name="adresa" value="<?php echo htmlspecialchars($komitent['adresa'] ?? ''); ?>">
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="kontakt_osoba" class="form-label">Kontakt osoba</label>
                                    <input type="text" class="form-control" id="kontakt_osoba" name="kontakt_osoba" value="<?php echo htmlspecialchars($komitent['kontakt_osoba'] ?? ''); ?>">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="email" class="form-label">Email adresa</label>
                                    <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($komitent['email'] ?? ''); ?>">
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="telefon" class="form-label">Kontakt telefon</label>
                                    <input type="tel" class="form-control" id="telefon" name="telefon" value="<?php echo htmlspecialchars($komitent['telefon'] ?? ''); ?>">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="pib" class="form-label">PIB</label>
                                    <input type="text" class="form-control" id="pib" name="pib" value="<?php echo htmlspecialchars($komitent['pib'] ?? ''); ?>">
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="maticni_broj" class="form-label">Matični broj</label>
                                    <input type="text" class="form-control" id="maticni_broj" name="maticni_broj" value="<?php echo htmlspecialchars($komitent['maticni_broj'] ?? ''); ?>">
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="cena_do_aerodroma" class="form-label">Cena do aerodroma</label>
                                    <input type="number" step="0.01" class="form-control" id="cena_do_aerodroma" name="cena_do_aerodroma" value="<?php echo !empty($komitent['cena_do_aerodroma']) ? htmlspecialchars($komitent['cena_do_aerodroma']) : ''; ?>" placeholder="0.00">
                                    <div class="form-text">Cena u RSD</div>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="cena_od_aerodroma" class="form-label">Cena od aerodroma</label>
                                    <input type="number" step="0.01" class="form-control" id="cena_od_aerodroma" name="cena_od_aerodroma" value="<?php echo !empty($komitent['cena_od_aerodroma']) ? htmlspecialchars($komitent['cena_od_aerodroma']) : ''; ?>" placeholder="0.00">
                                    <div class="form-text">Cena u RSD</div>
                                </div>
                            </div>
                            
                            <div class="d-flex justify-content-between">
                                <a href="komitenti.php" class="btn btn-secondary"><i class="bi bi-arrow-left me-1"></i>Nazad</a>
                                <button type="submit" class="btn btn-primary"><i class="bi bi-floppy me-1"></i>Sačuvaj izmene</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php include 'footer.php'; ?>