<?php
if (Auth('admin')->User()->dashboard_style == 'light') {
    $text = 'dark';
} else {
    $text = 'light';
}
?>
@extends('layouts.app')
@section('content')
    @include('admin.topmenu')
    @include('admin.sidebar')
    <div class="main-panel">
        <div class="content">
            <div class="page-inner">
                <div class="page-header">
                    <h4 class="page-title text-{{ $text }}">Car Management</h4>
                    <ul class="breadcrumbs">
                        <li class="nav-home"><a href="{{ route('admin.dashboard') }}"><i class="flaticon-home"></i></a></li>
                        <li class="separator"><i class="flaticon-right-arrow"></i></li>
                        <li class="nav-item"><span class="text-{{ $text }}">Cars</span></li>
                    </ul>
                </div>

                @if(session('success'))
                    <div class="alert alert-success alert-dismissible fade show">
                        {{ session('success') }}
                        <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
                    </div>
                @endif

                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <div class="d-flex align-items-center">
                                    <h4 class="card-title text-{{ $text }}">All Cars</h4>
                                    <a href="{{ route('admin.cars.create') }}" class="btn btn-primary btn-round ml-auto">
                                        <i class="fa fa-plus"></i> Add New Car
                                    </a>
                                </div>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table id="ShipTable" class="display table table-striped table-hover">
                                        <thead>
                                            <tr>
                                                <th>Photo</th>
                                                <th>Name</th>
                                                <th>Make/Model/Year</th>
                                                <th>Price</th>
                                                <th>Qty</th>
                                                <th>Status</th>
                                                <th>Orders</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach($cars as $car)
                                            <tr>
                                                <td>
                                                    @if($car->photos->count() > 0)
                                                        <img src="{{ asset('dash/' . $car->primary_photo) }}" alt="{{ $car->name }}" style="width:60px;height:40px;object-fit:cover;border-radius:4px;">
                                                    @else
                                                        <span class="badge badge-secondary">No Photo</span>
                                                    @endif
                                                </td>
                                                <td>{{ $car->name }}</td>
                                                <td>{{ $car->make }} {{ $car->model }} {{ $car->year }}</td>
                                                <td>${{ number_format($car->price, 2) }}</td>
                                                <td>{{ $car->quantity }}</td>
                                                <td>
                                                    @if($car->status == 'available')
                                                        <span class="badge badge-success">Available</span>
                                                    @else
                                                        <span class="badge badge-danger">Unavailable</span>
                                                    @endif
                                                    @if($car->featured)
                                                        <span class="badge badge-warning">Featured</span>
                                                    @endif
                                                </td>
                                                <td>{{ $car->orders_count }}</td>
                                                <td>
                                                    <div class="btn-group" role="group">
                                                        <a href="{{ route('admin.cars.show', $car) }}" class="btn btn-sm btn-info" title="View">
                                                            <i class="fas fa-eye"></i>
                                                        </a>
                                                        <a href="{{ route('admin.cars.edit', $car) }}" class="btn btn-sm btn-warning" title="Edit">
                                                            <i class="fas fa-edit"></i>
                                                        </a>
                                                        <form action="{{ route('admin.cars.toggle-status', $car) }}" method="POST" class="d-inline">
                                                            @csrf
                                                            <button type="submit" class="btn btn-sm btn-{{ $car->status == 'available' ? 'secondary' : 'success' }}" title="Toggle Status">
                                                                <i class="fas fa-exchange-alt"></i>
                                                            </button>
                                                        </form>
                                                        <form action="{{ route('admin.cars.destroy', $car) }}" method="POST" class="d-inline" onsubmit="return confirm('Delete this car?')">
                                                            @csrf @method('DELETE')
                                                            <button type="submit" class="btn btn-sm btn-danger" title="Delete">
                                                                <i class="fas fa-trash"></i>
                                                            </button>
                                                        </form>
                                                    </div>
                                                </td>
                                            </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
