@extends('layouts.app')

@section('title', 'Dashboard')

@section('content')

<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">

<div class="table-responsive">

    {{-- Flash Messages --}}

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        <script>
            showDriftAnimation();
        </script>
    @endif

    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif


        <!-- <div class="text-center mb-1">
            <h1 class="display-8 fw-bold text-uppercase text-danger" style="letter-spacing: 2px;">
                Khan Motor's 
            </h1>
        </div> -->

    <div class="container py-0">
        <h2 class="mb-1 text-center">Available Stock</h2>
            <div class="mb-3 d-flex justify-content-end">
                <div class="input-group" style="width: 30%;">
                    <span class="input-group-text bg-white">
                        <i class="bi bi-search"></i>
                    </span>
                    <input type="text" id="tableSearch" class="form-control" placeholder="Search in table...">
                </div>
            </div>
        <div class="card">
            <div class="card-body" >
                <table class="table table-hover table-bordered">
                    <thead class="table-dark">
                        <tr>
                            <th>#</th>
                            <th>Report No</th>
                            <th>Make</th>
                            <th>Model</th>
                            <th>Varient</th>
                            <th>Color</th>
                            <th>Registered City</th>
                            <th>Transmission Type</th>
                            <th>Registration No</th>
                            <th>Engine</th>
                            <th>Mileage</th>
                            <th>Fuel Type</th>
                            <th>Inspection Date</th>
                            <th>Chassis No</th>
                            <th>Engine No</th>

                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($cars as $car)
                            <tr data-car-id="{{ $car->id }}">
                                <td>{{ $loop->iteration }}</td> <!-- Serial number -->
                                <td>KM-{{ \Carbon\Carbon::parse($car->inspection_date)->format('Y') }}-{{ $car->id }}</td>
                                <td>{{ $car->make }}</td>
                                <td>{{ $car->model }}</td>
                                <td>{{ $car->varient }}</td>
                                <td>{{ $car->color }}</td>
                                <td>{{ $car->registered_city }}</td>
                                <td>{{ $car->transmission }}</td>
                                <td>{{ $car->registration_no }}</td>
                                <td>{{ $car->engine_capacity }}</td>
                                <td>{{ $car->mileage }}</td>
                                <td>{{ $car->fuel_type }}</td>
                                <td>{{ \Carbon\Carbon::parse($car->inspection_date)->format('d/m/Y') }}</td>
                                <td>{{ $car->chassis_no }}</td>
                                <td>{{ $car->engine_no }}</td>
                                <td class="action-buttons">
                                    <a href="{{ route('cars.print', $car->id) }}" class="btn btn-info" target="_blank"><i class="bi bi-printer"></i></a>
                                    <a href="{{ route('cars.show', $car->id) }}" class="btn btn-warning"><i class="bi bi-eye"></i></a>

                                    <a href="{{ route('cars.edit', $car->id) }}" class="btn btn btn-success"><i class="bi bi-pencil"></i></a><br>
                                    @if(auth()->user()->is_admin == 0)

                                    @else
                                    <form action="{{ route('cars.destroy', $car->id) }}" method="POST" style="display:inline;">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn btn-danger" onclick="return confirm('Are you sure you want to delete this car?')"><i class="bi bi-Trash"></i></button>
                                    </form>
                                    @endif

                                    <a href="{{ route('cars.sell', $car->id) }}" class="btn btn-primary btn-sm">Sell</a>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>

                </table>
            </div>
        </div>
    </div>
</div>
<style>
    .action-buttons {
        white-space: nowrap;
        line-height: 35px;
    }
    thead th {
        vertical-align: top !important;
    }
    
</style>

<script>
function printForm(carId) {
    window.open(`/cars/${carId}/print`, '_blank');
}
</script>
@endsection
<script>
function printForm(carId) {
    window.open(`/cars/${carId}/print`, '_blank');
}

document.addEventListener('DOMContentLoaded', function () {
    const searchInput = document.getElementById('tableSearch');
    const tableRows = document.querySelectorAll('table tbody tr');

    searchInput.addEventListener('keyup', function () {
        const query = this.value.toLowerCase();

        tableRows.forEach(function (row) {
            const rowText = row.textContent.toLowerCase();
            row.style.display = rowText.includes(query) ? '' : 'none';
        });
    });
});
</script>









<style>
    .drift-car {
        position: fixed;
        bottom: 220px;
        left: -200px;
        width: 500px;
        height: auto;
        z-index: 9999;
        animation: drift 4s ease-out forwards;
        pointer-events: none;
    }

    @keyframes drift {
        0% { left: -200px; transform: rotate(0deg) scale(1); opacity: 0.5; }
        50% { transform: rotate(0deg) scale(1.2); }
        100% { left: 120%; transform: rotate(0deg) scale(0.8); opacity: 1; }
    }
</style>

<script>
    function showDriftAnimation() {
        const carImage = document.createElement('img');
        carImage.src = "{{ url('drift2.png') }}"; // full absolute URL
        carImage.classList.add('drift-car');
        document.body.appendChild(carImage);

        setTimeout(() => {
            carImage.remove();
        }, 3000); // remove after animation
    }
</script>




