@extends('layouts.app')

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

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4>Customers</h4>
                    <div>
                        <a href="{{ route('customers.print') }}" class="btn btn-secondary btn-sm" target="_blank">Print List</a>
                        <a href="{{ route('customers.create') }}" class="btn btn-primary btn-sm">Add Customer</a>
                    </div>
                </div>
                <div class="card-body">
                    @if($customers->count() > 0)
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Email</th>
                                    <th>Phone</th>
                                    <th>Address</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($customers as $customer)
                                    <tr>
                                        <td>{{ $customer->name }}</td>
                                        <td>{{ $customer->email ?? '-' }}</td>
                                        <td>{{ $customer->phone ?? '-' }}</td>
                                        <td>{{ substr($customer->address ?? '', 0, 50) }}{{ strlen($customer->address ?? '') > 50 ? '...' : '' }}</td>
                                        <td>
                                            <a href="{{ route('customers.printSingle', ['customer' => $customer, 'autoprint' => 1]) }}" class="btn btn-secondary btn-sm" target="_blank">Print</a>
                                            <a href="{{ route('customers.edit', $customer) }}" class="btn btn-warning btn-sm">Edit</a>
                                            <form action="{{ route('customers.destroy', $customer) }}" method="POST" style="display:inline;" onsubmit="return confirm('Are you sure?');">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                                            </form>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                        {{ $customers->links() }}
                    @else
                        <p class="text-center">No customers found. <a href="{{ route('customers.create') }}">Create one now</a></p>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
