@extends('layouts.app')

@section('title', 'Purchase Invoices List')

@section('content')
<div class="container mt-5">
    <h2 class="mb-4 text-center">Purchase Invoices</h2>
    <a href="{{ route('purchase-invoice.create') }}" class="btn btn-success mb-3">+ New Invoice</a>

    <table class="table table-bordered table-striped">
        <thead class="table-dark">
            <tr>
                <th>ID</th>
                <th>Make</th>
                <th>Model</th>
                <th>Varient</th>
                <th>Registration No</th>
                <th>Purchase Date</th>
                <th>Purchase Price</th>
                <th>Vendor Name</th>
                <th> File upload</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @forelse($invoices as $invoice)
            <tr>
                <td>{{ $invoice->id }}</td>
                <td>{{ $invoice->make }}</td>
                <td>{{ $invoice->model }}</td>
                <td>{{ $invoice->varient }}</td>
                <td>{{ $invoice->registration_no }}</td>
                <td>{{ $invoice->purchase_date }}</td>
                <td>${{ number_format($invoice->purchase_price) }}</td>
                <td>{{ $invoice->vendor_name }}</td>
                <td>
                    @if($invoice->invoice_file_path)
                    <a href="{{ asset('storage/' . $invoice->invoice_file_path) }}" class="btn btn-sm btn-dark" target="_blank">View File</a>

                    @endif
                </td>
                <td>

                @if(auth()->user()->is_admin == 0)

                @if($invoice->invoice_file_path)
                    @else N/A @endif
                    <a href="{{ route('purchase-invoice.edit', $invoice) }}" class="btn btn-sm btn-primary">Edit</a>
                    <a href="{{ route('cars.create', ['invoice' => $invoice->id]) }}" class="btn btn-sm btn-secondary">Report</a>
                    <a href="{{ route('purchase-invoice.print', $invoice->id) }}" target="_blank" class="btn btn-sm btn-warning">Print</a>
                @else
                <a href="{{ route('purchase-invoice.edit', $invoice) }}" class="btn btn-sm btn-primary">Edit</a>
                    <a href="{{ route('cars.create', ['invoice' => $invoice->id]) }}" class="btn btn-sm btn-secondary">Report</a>
                    <a href="{{ route('purchase-invoice.print', $invoice->id) }}" target="_blank" class="btn btn-sm btn-warning">Print</a>
                
                    <!-- <a href="{{ route('purchase-invoice.report', $invoice->id) }}" class="btn btn-sm btn-secondary">Report</a> -->
                    <form action="{{ route('purchase-invoice.destroy', $invoice) }}" method="POST" style="display:inline;">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-sm btn-danger"
                            onclick="return confirm('Are you sure you want to delete this invoice?')">Delete</button>
                    </form>

                @endif
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="9" class="text-center">No invoices found.</td>
            </tr>
            @endforelse
        </tbody>
    </table>
</div>
@endsection
