<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header('Location: ../admin_login.php');
    exit();
}

$active_page = 'kelola_kapal';
$success = '';
$error = '';

// Proses tambah kapal
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {
    $nama_kapal = trim($_POST['nama_kapal']);
    $jenis = $_POST['jenis'];
    $kapasitas = $_POST['kapasitas'];
    $harga_per_tiket = $_POST['harga_per_tiket'];
    $pelabuhan_asal = $_POST['pelabuhan_asal'];
    $pelabuhan_tujuan = $_POST['pelabuhan_tujuan'];
    $rute = $_POST['rute'];
    $jadwal_keberangkatan = $_POST['jadwal_keberangkatan'];
    $deskripsi = $_POST['deskripsi'];
    $pemilik_id = $_POST['pemilik_id'];
    
    // Validasi
    if (empty($nama_kapal) || empty($kapasitas) || empty($harga_per_tiket)) {
        $error = "Semua field wajib diisi!";
    } else {
        // Upload foto jika ada
        $foto = null;
        if (isset($_FILES['foto']) && $_FILES['foto']['error'] === UPLOAD_ERR_OK) {
            $file = $_FILES['foto'];
            $allowed = ['image/jpeg', 'image/png', 'image/jpg', 'image/webp'];
            $max_size = 2 * 1024 * 1024;
            
            if (!in_array($file['type'], $allowed)) {
                $error = "Format file tidak didukung. Gunakan JPG, PNG, atau WEBP.";
            } elseif ($file['size'] > $max_size) {
                $error = "Ukuran file terlalu besar. Maksimal 2MB.";
            } else {
                $upload_dir = '../uploads/kapal/';
                if (!is_dir($upload_dir)) {
                    mkdir($upload_dir, 0777, true);
                }
                $extension = pathinfo($file['name'], PATHINFO_EXTENSION);
                $foto = 'kapal_' . time() . '_' . uniqid() . '.' . $extension;
                move_uploaded_file($file['tmp_name'], $upload_dir . $foto);
            }
        }
        
        if (empty($error)) {
            $stmt = $conn->prepare("
                INSERT INTO kapal (nama_kapal, jenis, kapasitas, harga_per_tiket, pelabuhan_asal, pelabuhan_tujuan, rute, jadwal_keberangkatan, deskripsi, pemilik_id, foto, status_verifikasi, created_at) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', NOW())
            ");
            $stmt->bind_param("ssiisssssis", $nama_kapal, $jenis, $kapasitas, $harga_per_tiket, $pelabuhan_asal, $pelabuhan_tujuan, $rute, $jadwal_keberangkatan, $deskripsi, $pemilik_id, $foto);
            
            if ($stmt->execute()) {
                $success = "Kapal berhasil ditambahkan! Menunggu verifikasi admin.";
            } else {
                $error = "Gagal menambahkan kapal: " . $conn->error;
            }
            $stmt->close();
        }
    }
}

// Ambil daftar pemilik kapal
$pemilik_list = $conn->query("SELECT id, nama FROM users WHERE role = 'pemilik_kapal' OR role = 'admin' ORDER BY nama");
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tambah Kapal - Admin Samosir Wisata</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: #f0f2f5;
            overflow-x: hidden;
        }

        /* Sidebar */
        .sidebar {
            position: fixed;
            left: 0;
            top: 0;
            width: 280px;
            height: 100%;
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            color: white;
            transition: all 0.3s;
            z-index: 1000;
            box-shadow: 2px 0 10px rgba(0,0,0,0.1);
        }

        .sidebar-header {
            padding: 30px 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        .sidebar-header i {
            font-size: 50px;
            margin-bottom: 10px;
        }

        .sidebar-header h3 {
            font-size: 20px;
            margin-top: 10px;
        }

        .sidebar-header p {
            font-size: 12px;
            opacity: 0.8;
            margin-top: 5px;
        }

        .sidebar-menu {
            padding: 20px 0;
        }

        .menu-item {
            padding: 12px 25px;
            margin: 5px 15px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            gap: 15px;
            color: rgba(255,255,255,0.8);
            text-decoration: none;
            transition: all 0.3s;
        }

        .menu-item:hover {
            background: rgba(255,255,255,0.1);
            color: white;
            transform: translateX(5px);
        }

        .menu-item.active {
            background: rgba(255,255,255,0.2);
            color: white;
        }

        .menu-item i {
            width: 24px;
            font-size: 18px;
        }

        /* Main Content */
        .main-content {
            margin-left: 280px;
            padding: 20px 30px;
        }

        /* Top Bar */
        .top-bar {
            background: white;
            border-radius: 15px;
            padding: 15px 25px;
            margin-bottom: 25px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }

        .page-title h2 {
            font-size: 24px;
            color: #333;
        }

        .page-title p {
            color: #666;
            font-size: 14px;
            margin-top: 5px;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .user-avatar {
            width: 45px;
            height: 45px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 18px;
            color: white;
        }

        .logout-btn {
            background: #dc2626;
            color: white;
            padding: 8px 16px;
            border-radius: 10px;
            text-decoration: none;
            transition: all 0.3s;
        }

        .logout-btn:hover {
            background: #b91c1c;
            transform: translateY(-2px);
        }

        /* Welcome Card */
        .welcome-card {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            border-radius: 20px;
            padding: 25px 30px;
            margin-bottom: 30px;
            color: white;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
        }

        .welcome-text h1 {
            font-size: 24px;
            margin-bottom: 8px;
        }

        .welcome-text p {
            opacity: 0.9;
            font-size: 14px;
        }

        .welcome-date {
            text-align: right;
        }

        .welcome-date .date {
            font-size: 14px;
            opacity: 0.9;
        }

        .welcome-date .time {
            font-size: 20px;
            font-weight: 600;
        }

        /* Form Card */
        .form-card {
            background: white;
            border-radius: 20px;
            padding: 25px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }

        .form-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
            padding-bottom: 15px;
            border-bottom: 1px solid #e0e0e0;
        }

        .form-header h3 {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .form-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 20px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
            font-size: 14px;
        }

        .form-group label i {
            margin-right: 8px;
            color: #2a5298;
        }

        .form-group input,
        .form-group select,
        .form-group textarea {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 12px;
            font-size: 14px;
            font-family: 'Inter', sans-serif;
            transition: all 0.3s;
        }

        .form-group input:focus,
        .form-group select:focus,
        .form-group textarea:focus {
            outline: none;
            border-color: #2a5298;
            box-shadow: 0 0 0 3px rgba(42,82,152,0.1);
        }

        .form-group textarea {
            resize: vertical;
            min-height: 80px;
        }

        .upload-area {
            border: 2px dashed #ccc;
            border-radius: 15px;
            padding: 25px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
            margin-bottom: 10px;
        }

        .upload-area:hover {
            border-color: #2a5298;
            background: #f8f9fa;
        }

        .upload-area i {
            font-size: 40px;
            color: #999;
            margin-bottom: 10px;
        }

        .btn-submit {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            color: white;
            padding: 12px 25px;
            border: none;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            width: 100%;
            margin-top: 20px;
        }

        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(30,60,114,0.3);
        }

        .btn-back {
            background: #f0f2f5;
            color: #2a5298;
            padding: 10px 20px;
            border-radius: 10px;
            text-decoration: none;
            display: inline-block;
            transition: all 0.3s;
        }

        .btn-back:hover {
            background: #2a5298;
            color: white;
        }

        .alert-success {
            background: #d1fae5;
            color: #059669;
            padding: 15px;
            border-radius: 12px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .alert-error {
            background: #fee2e2;
            color: #dc2626;
            padding: 15px;
            border-radius: 12px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .preview-img {
            margin-top: 15px;
            text-align: center;
        }

        .preview-img img {
            max-width: 150px;
            max-height: 150px;
            border-radius: 10px;
            object-fit: cover;
        }

        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
            }
            .main-content {
                margin-left: 0;
            }
            .form-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <!-- Sidebar -->
    <div class="sidebar">
        <div class="sidebar-header">
            <i class="fas fa-user-shield"></i>
            <h3>Admin Panel</h3>
            <p>Samosir Wisata</p>
        </div>
        <div class="sidebar-menu">
            <a href="dashboard.php" class="menu-item">
                <i class="fas fa-tachometer-alt"></i> Dashboard
            </a>
            <a href="kelola_kapal.php" class="menu-item active">
                <i class="fas fa-ship"></i> Kelola Kapal
            </a>
            <a href="verifikasi_kapal.php" class="menu-item">
                <i class="fas fa-check-circle"></i> Verifikasi Kapal
            </a>
            <a href="kelola_pesanan.php" class="menu-item">
                <i class="fas fa-ticket-alt"></i> Kelola Pesanan
            </a>
            <a href="kelola_user.php" class="menu-item">
                <i class="fas fa-users"></i> Kelola User
            </a>
            <a href="laporan.php" class="menu-item">
                <i class="fas fa-chart-line"></i> Laporan
            </a>
        </div>
    </div>

    <!-- Main Content -->
    <div class="main-content">
        <!-- Top Bar -->
        <div class="top-bar">
            <div class="page-title">
                <h2>Tambah Kapal</h2>
                <p><i class="fas fa-plus"></i> Tambah data kapal wisata baru</p>
            </div>
            <div class="user-info">
                <div class="user-avatar">A</div>
                <span>Admin Utama</span>
                <a href="../logout.php" class="logout-btn"><i class="fas fa-sign-out-alt"></i> Logout</a>
            </div>
        </div>

        <!-- Welcome Card -->
        <div class="welcome-card">
            <div class="welcome-text">
                <h1>Tambah Kapal Baru 🚢</h1>
                <p>Isi form berikut untuk menambahkan kapal wisata baru</p>
            </div>
            <div class="welcome-date">
                <div class="date">
                    <i class="fas fa-calendar-alt"></i> <?php echo date('l, d F Y'); ?>
                </div>
                <div class="time">
                    <i class="fas fa-clock"></i> <?php echo date('H:i'); ?> WIB
                </div>
            </div>
        </div>

        <!-- Form Card -->
        <div class="form-card">
            <div class="form-header">
                <h3>
                    <i class="fas fa-ship"></i>
                    Form Tambah Kapal
                </h3>
                <a href="kelola_kapal.php" class="btn-back">
                    <i class="fas fa-arrow-left"></i> Kembali
                </a>
            </div>

            <?php if ($success): ?>
                <div class="alert-success">
                    <i class="fas fa-check-circle"></i>
                    <?php echo $success; ?>
                    <script>
                        setTimeout(function() {
                            window.location.href = 'kelola_kapal.php';
                        }, 2000);
                    </script>
                </div>
            <?php endif; ?>

            <?php if ($error): ?>
                <div class="alert-error">
                    <i class="fas fa-exclamation-circle"></i>
                    <?php echo $error; ?>
                </div>
            <?php endif; ?>

            <form method="POST" enctype="multipart/form-data">
                <div class="form-grid">
                    <div class="form-group">
                        <label><i class="fas fa-ship"></i> Nama Kapal *</label>
                        <input type="text" name="nama_kapal" placeholder="Contoh: KM. Express Bahari" required>
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-tag"></i> Jenis Kapal *</label>
                        <select name="jenis" required>
                            <option value="">Pilih Jenis Kapal</option>
                            <option value="speedboat">Speedboat</option>
                            <option value="kapal_kayu">Kapal Kayu</option>
                            <option value="ferry">Ferry</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-users"></i> Kapasitas (orang) *</label>
                        <input type="number" name="kapasitas" placeholder="Contoh: 50" min="1" required>
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-money-bill-wave"></i> Harga per Tiket *</label>
                        <input type="number" name="harga_per_tiket" placeholder="Contoh: 75000" min="0" required>
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-anchor"></i> Pelabuhan Asal</label>
                        <input type="text" name="pelabuhan_asal" placeholder="Contoh: Parapat" value="Parapat">
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-map-marker-alt"></i> Pelabuhan Tujuan</label>
                        <input type="text" name="pelabuhan_tujuan" placeholder="Contoh: Tuk-Tuk" value="Tuk-Tuk">
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-route"></i> Rute</label>
                        <input type="text" name="rute" placeholder="Contoh: Parapat - Tuk-Tuk" value="Parapat - Tuk-Tuk">
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-clock"></i> Jadwal Keberangkatan</label>
                        <input type="time" name="jadwal_keberangkatan" value="08:00">
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-user"></i> Pemilik Kapal *</label>
                        <select name="pemilik_id" required>
                            <option value="">Pilih Pemilik Kapal</option>
                            <?php while($row = $pemilik_list->fetch_assoc()): ?>
                                <option value="<?php echo $row['id']; ?>"><?php echo htmlspecialchars($row['nama']); ?></option>
                            <?php endwhile; ?>
                        </select>
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-image"></i> Foto Kapal</label>
                        <div class="upload-area" onclick="document.getElementById('foto').click()">
                            <i class="fas fa-cloud-upload-alt"></i>
                            <p>Klik untuk upload foto</p>
                            <p style="font-size: 12px; color: #999;">Format: JPG, PNG, WEBP | Max: 2MB</p>
                        </div>
                        <input type="file" name="foto" id="foto" style="display: none;" accept="image/*" onchange="previewImage(this)">
                        <div id="preview" class="preview-img" style="display: none;">
                            <img id="preview_img">
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label><i class="fas fa-info-circle"></i> Deskripsi</label>
                    <textarea name="deskripsi" placeholder="Masukkan deskripsi kapal, fasilitas, dll..."></textarea>
                </div>

                <button type="submit" name="submit" class="btn-submit">
                    <i class="fas fa-save"></i> Simpan Kapal
                </button>
            </form>
        </div>
    </div>

    <script>
        function previewImage(input) {
            if (input.files && input.files[0]) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    document.getElementById('preview_img').src = e.target.result;
                    document.getElementById('preview').style.display = 'block';
                }
                reader.readAsDataURL(input.files[0]);
            }
        }
    </script>
</body>
</html>