����JFIF��`�`�����Viewing File: /home/u820193700/domains/throtllr.in/public_html/admin/add_bike.php
<?php
require_once '../includes/db.php';
require_once '../includes/functions.php';
require_once '../includes/bike_models.php';
if(!is_logged_in()) { redirect("login.php"); }

if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $model_name = $_POST['model_name'] ?? '';
    $price = $_POST['price'] ?? 0;
    $year = $_POST['year'] ?? date('Y');
    $km_driven = $_POST['km_driven'] ?? 0;
    $location = $_POST['location'] ?? '';
    $map_url = $_POST['map_url'] ?? '';
    
    $colour = $_POST['colour'] ?? '';
    $engine_capacity = $_POST['engine_capacity'] ?? '';
    $insurance_status = $_POST['insurance_status'] ?? '';
    $ownership = $_POST['ownership'] ?? '1st';
    
    $fuel_tank_capacity = $_POST['fuel_tank_capacity'] ?? '';
    $insurance_valid_upto = $_POST['insurance_valid_upto'] ?? '';
    $rc_status = $_POST['rc_status'] ?? 'Available';
    $rto_code = $_POST['rto_code'] ?? '';
    
    $engine_condition = $_POST['engine_condition'] ?? 'Good';
    $tyres_condition = $_POST['tyres_condition'] ?? 'Good';
    $brakes_condition = $_POST['brakes_condition'] ?? 'Good';
    $battery_condition = $_POST['battery_condition'] ?? 'Good';
    $suspension_condition = $_POST['suspension_condition'] ?? 'Good';

    $reference_id = 'RE-' . strtoupper(substr(uniqid(), -6));
    
    try {
        $stmt = $pdo->prepare("INSERT INTO bikes (reference_id, model_name, price, year, km_driven, location, map_url, status, colour, engine_capacity, insurance_status, ownership, engine_condition, tyres_condition, brakes_condition, battery_condition, suspension_condition, fuel_tank_capacity, insurance_valid_upto, rc_status, rto_code) VALUES (?, ?, ?, ?, ?, ?, ?, 'available', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([$reference_id, $model_name, $price, $year, $km_driven, $location, $map_url, $colour, $engine_capacity, $insurance_status, $ownership, $engine_condition, $tyres_condition, $brakes_condition, $battery_condition, $suspension_condition, $fuel_tank_capacity, $insurance_valid_upto, $rc_status, $rto_code]);
        $bike_id = $pdo->lastInsertId();
        
        // Handle image upload
        if (!empty($_FILES['main_image']['name'])) {
            $uploadDir = '../assets/uploads/bikes/' . $bike_id . '/';
            if (!file_exists($uploadDir)) {
                mkdir($uploadDir, 0777, true);
            }
            $fileName = basename($_FILES['main_image']['name']);
            $targetPath = $uploadDir . $fileName;
            
            if (move_uploaded_file($_FILES['main_image']['tmp_name'], $targetPath)) {
                $dbPath = 'assets/uploads/bikes/' . $bike_id . '/' . $fileName;
                $imgStmt = $pdo->prepare("INSERT INTO bike_images (bike_id, image_url, is_main) VALUES (?, ?, TRUE)");
                $imgStmt->execute([$bike_id, $dbPath]);
            }
        }

        if (!empty($_FILES['gallery_images']['name'][0])) {
            foreach ($_FILES['gallery_images']['name'] as $key => $name) {
                if ($_FILES['gallery_images']['error'][$key] == UPLOAD_ERR_OK) {
                    $fileName = uniqid() . '_' . basename($name);
                    $targetPath = $uploadDir . $fileName;
                    if (move_uploaded_file($_FILES['gallery_images']['tmp_name'][$key], $targetPath)) {
                        $dbPath = 'assets/uploads/bikes/' . $bike_id . '/' . $fileName;
                        $imgStmt = $pdo->prepare("INSERT INTO bike_images (bike_id, image_url, is_main) VALUES (?, ?, FALSE)");
                        $imgStmt->execute([$bike_id, $dbPath]);
                    }
                }
            }
        }
        
        if (!empty($_FILES['video']['name'])) {
            $fileName = uniqid() . '_' . basename($_FILES['video']['name']);
            $targetPath = $uploadDir . $fileName;
            if (move_uploaded_file($_FILES['video']['tmp_name'], $targetPath)) {
                $dbPath = 'assets/uploads/bikes/' . $bike_id . '/' . $fileName;
                $vidStmt = $pdo->prepare("INSERT INTO bike_videos (bike_id, video_url) VALUES (?, ?)");
                $vidStmt->execute([$bike_id, $dbPath]);
            }
        }
        
        if (!empty($_FILES['zip_360']['name'])) {
            $zipPath = $_FILES['zip_360']['tmp_name'];
            $zip = new ZipArchive;
            if ($zip->open($zipPath) === TRUE) {
                $three60Dir = $uploadDir . '360/';
                if (!file_exists($three60Dir)) mkdir($three60Dir, 0777, true);
                
                // Extract to a temporary directory first
                $tempExtractDir = $three60Dir . 'temp_extract/';
                if (!file_exists($tempExtractDir)) mkdir($tempExtractDir, 0777, true);
                
                $zip->extractTo($tempExtractDir);
                $zip->close();
                
                // Recursively find all images
                $images = [];
                $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tempExtractDir));
                foreach ($iterator as $file) {
                    if ($file->isFile()) {
                        $ext = strtolower($file->getExtension());
                        if (in_array($ext, ['jpg', 'jpeg', 'png', 'webp'])) {
                            $images[] = $file->getPathname();
                        }
                    }
                }
                
                natsort($images);
                $frameCount = count($images);
                if($frameCount > 0) {
                    $sequence = 1;
                    foreach($images as $oldPath) {
                        $ext = strtolower(pathinfo($oldPath, PATHINFO_EXTENSION));
                        $newPath = $three60Dir . $sequence . '.' . $ext;
                        rename($oldPath, $newPath);
                        $sequence++;
                    }
                    $dbFolderPath = 'assets/uploads/bikes/' . $bike_id . '/360/';
                    
                    $stmt360 = $pdo->prepare("INSERT INTO bike_360_images (bike_id, folder_path, frame_count) VALUES (?, ?, ?)");
                    $stmt360->execute([$bike_id, $dbFolderPath, $frameCount]);
                }
                
                // Cleanup temp dir
                $rmIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tempExtractDir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
                foreach ($rmIterator as $file) {
                    $file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname());
                }
                rmdir($tempExtractDir);
            }
        }
        
        redirect('manage_bikes.php');
    } catch (\PDOException $e) {
        $error = "Error saving bike: " . $e->getMessage();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Add Bike - ReOwn Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="admin.css?v=<?php echo time(); ?>">
</head>
<body>

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

<div id="main-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Add New Bike</h2>
        <a href="manage_bikes.php" class="btn btn-outline-light"><i class="fas fa-arrow-left me-2"></i> Back</a>
    </div>

    <div class="glass-card p-4">
        <form method="POST" enctype="multipart/form-data">
            <div class="row g-3">
                <?php if(isset($error)): ?>
                <div class="col-12"><div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div></div>
                <?php endif; ?>
                <div class="col-md-6">
                    <label>Model Name</label>
                    <select name="model_name" class="form-select bg-dark text-white border-secondary" required>
                        <option value="" disabled selected>-- Select Royal Enfield Model --</option>
                        <?php foreach ($bike_categories as $category => $models): ?>
                            <optgroup label="<?php echo htmlspecialchars($category); ?>">
                                <?php foreach ($models as $model): ?>
                                    <option value="<?php echo htmlspecialchars($model); ?>">
                                        <?php echo htmlspecialchars($model); ?>
                                    </option>
                                <?php endforeach; ?>
                            </optgroup>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-6">
                    <label>Price</label>
                    <input type="number" name="price" class="form-control bg-dark text-white border-secondary" required>
                </div>
                <div class="col-md-4">
                    <label>Year</label>
                    <input type="number" name="year" class="form-control bg-dark text-white border-secondary" required>
                </div>
                <div class="col-md-4">
                    <label>KM Driven</label>
                    <input type="number" name="km_driven" class="form-control bg-dark text-white border-secondary" required>
                </div>
                <div class="col-md-4">
                    <label>Location</label>
                    <input type="text" name="location" class="form-control bg-dark text-white border-secondary" required>
                </div>
                <div class="col-12 mt-2">
                    <label>Google Maps URL (for this vehicle location)</label>
                    <input type="url" name="map_url" class="form-control bg-dark text-white border-secondary" placeholder="e.g. https://www.google.com/maps/...">
                </div>

                <hr class="border-secondary my-4">
                <h5 class="text-re-red mb-3">Bike Specifications</h5>
                <div class="col-md-3">
                    <label>Color</label>
                    <input type="text" name="colour" class="form-control bg-dark text-white border-secondary">
                </div>
                <div class="col-md-3">
                    <label>Engine Capacity (e.g. 350)</label>
                    <input type="text" name="engine_capacity" class="form-control bg-dark text-white border-secondary">
                </div>
                <div class="col-md-3">
                    <label>Insurance Status</label>
                    <input type="text" name="insurance_status" class="form-control bg-dark text-white border-secondary" placeholder="e.g. Active till 2027">
                </div>
                <div class="col-md-3">
                    <label>Ownership</label>
                    <select name="ownership" class="form-select bg-dark text-white border-secondary">
                        <option value="1st">1st Owner</option>
                        <option value="2nd">2nd Owner</option>
                        <option value="3rd">3rd Owner</option>
                        <option value="4th+">4th+ Owner</option>
                </div>
                <div class="col-md-3">
                    <label>Fuel Tank Capacity</label>
                    <input type="text" name="fuel_tank_capacity" class="form-control bg-dark text-white border-secondary" placeholder="e.g. 13.5 L">
                </div>
                <div class="col-md-3">
                    <label>Insurance Valid Upto</label>
                    <input type="date" name="insurance_valid_upto" class="form-control bg-dark text-white border-secondary">
                </div>
                <div class="col-md-3">
                    <label>RC Status</label>
                    <select name="rc_status" class="form-select bg-dark text-white border-secondary">
                        <option value="Available">Available</option>
                        <option value="Not Available">Not Available</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label>RTO Code</label>
                    <input type="text" name="rto_code" class="form-control bg-dark text-white border-secondary" placeholder="e.g. KA02">
                </div>

                <hr class="border-secondary my-4">
                <h5 class="text-re-red mb-3">150-Point Inspection Report</h5>
                <div class="col-md-4">
                    <label>Engine Condition</label>
                    <select name="engine_condition" class="form-select bg-dark text-white border-secondary">
                        <option value="Excellent">Excellent</option>
                        <option value="Good" selected>Good</option>
                        <option value="Average">Average</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <label>Tyres Condition</label>
                    <select name="tyres_condition" class="form-select bg-dark text-white border-secondary">
                        <option value="Excellent">Excellent</option>
                        <option value="Good" selected>Good</option>
                        <option value="Average">Average</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <label>Brakes Condition</label>
                    <select name="brakes_condition" class="form-select bg-dark text-white border-secondary">
                        <option value="Excellent">Excellent</option>
                        <option value="Good" selected>Good</option>
                        <option value="Average">Average</option>
                    </select>
                </div>
                <div class="col-md-6">
                    <label>Battery Condition</label>
                    <select name="battery_condition" class="form-select bg-dark text-white border-secondary">
                        <option value="Excellent">Excellent</option>
                        <option value="Good" selected>Good</option>
                        <option value="Average">Average</option>
                    </select>
                </div>
                <div class="col-md-6">
                    <label>Suspension Condition</label>
                    <select name="suspension_condition" class="form-select bg-dark text-white border-secondary">
                        <option value="Excellent">Excellent</option>
                        <option value="Good" selected>Good</option>
                        <option value="Average">Average</option>
                    </select>
                </div>

                <hr class="border-secondary my-4">
                <h5 class="text-re-red mb-3">Media & Files</h5>
                <div class="col-12 mt-3">
                    <label>Main Image</label>
                    <input type="file" name="main_image" class="form-control bg-dark text-white border-secondary" accept="image/*" required>
                </div>
                <div class="col-12 mt-3">
                    <label>Gallery Images (Multiple)</label>
                    <input type="file" name="gallery_images[]" class="form-control bg-dark text-white border-secondary" accept="image/*" multiple>
                </div>
                <div class="col-12 mt-3">
                    <label>Bike Video</label>
                    <input type="file" name="video" class="form-control bg-dark text-white border-secondary" accept="video/*">
                </div>
                <div class="col-12 mt-3">
                    <label>360° Images (ZIP File - 36 or 72 images)</label>
                    <input type="file" name="zip_360" class="form-control bg-dark text-white border-secondary" accept=".zip">
                </div>
                <div class="col-12 mt-4">
                    <button type="submit" class="btn btn-danger">Save Bike</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
</html>
Back to Directory �������}�!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������� ������w�!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������ ��?��_��+��?��(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(�����