@extends('layouts.app')

@section('page-title', 'Quotations')

@section('content')
<div class="container-fluid">
    <div class="row mb-3">
        <div class="col-md-6">
            <h3>Quotation List</h3>
        </div>
        <div class="col-md-6 text-end">
            <a href="{{ route('quotations.create') }}" class="btn btn-primary">Add New Quotation</a>
            <a href="{{ route('quotations.sales') }}" class="btn btn-success">Sales Proposals</a>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif

    <div class="card">
        <div class="card-body">
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Date</th>
                        <th>Client</th>
                        <th>Company</th>
                        <th>Total</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($quotations as $quotation)
                        <tr>
                            <td>{{ $quotation->quotation_number }}</td>
                            <td>{{ $quotation->date ? \Illuminate\Support\Carbon::parse($quotation->date)->format('Y-m-d') : '' }}</td>
                            <td>{{ $quotation->bill_to }}</td>
                            <td>{{ $quotation->company_name }}</td>
                            <td>{{ number_format($quotation->total,2) }}</td>
                            <td>{{ ucfirst($quotation->status) }}</td>
                            <td>
                                <a href="{{ route('quotations.show', $quotation) }}" class="btn btn-sm btn-secondary" target="_blank">Print</a>
                                <a href="{{ route('quotations.edit', $quotation) }}" class="btn btn-sm btn-info">Edit</a>
                                <form action="{{ route('quotations.destroy', $quotation) }}" method="POST" style="display:inline-block;" onsubmit="return confirm('Delete this quotation?');">
                                    @csrf
                                    @method('DELETE')
                                    <button class="btn btn-sm btn-danger">Delete</button>
                                </form>

                                @if($quotation->status !== 'sale')
                                    <form action="{{ route('quotations.updateStatus', $quotation) }}" method="POST" style="display:inline-block;">
                                        @csrf
                                        @method('PATCH')
                                        <input type="hidden" name="status" value="sale">
                                        <button class="btn btn-sm btn-success">Sale</button>
                                    </form>
                                @endif
                                @if($quotation->status !== 'not_sale')
                                    <form action="{{ route('quotations.updateStatus', $quotation) }}" method="POST" style="display:inline-block;">
                                        @csrf
                                        @method('PATCH')
                                        <input type="hidden" name="status" value="not_sale">
                                        <button class="btn btn-sm btn-warning">Not Sale</button>
                                    </form>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center">No quotations found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
