@extends('layouts.app')

@section('title', 'Customers')

@section('content')
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">Customers</h5>
        <a href="{{ route('customers.create') }}" class="btn btn-primary">
            <i class="bi bi-plus-circle me-2"></i>Add Customer
        </a>
    </div>
    <div class="card-body">
        @if(session('success'))
            <div class="alert alert-success">
                {{ session('success') }}
            </div>
        @endif

        <div class="row mb-4">
            <div class="col-md-6">
                <form method="GET" action="{{ route('customers.index') }}" class="d-flex gap-2">
                    <div class="input-group">
                        <input type="text" 
                               name="search" 
                               class="form-control" 
                               placeholder="Search by email or phone..."
                               value="{{ request('search') }}">
                        <button class="btn btn-outline-secondary" type="submit">
                            <i class="bi bi-search"></i>
                        </button>
                        @if(request('search'))
                            <a href="{{ route('customers.index', ['is_owner' => request('is_owner')]) }}" 
                               class="btn btn-outline-secondary">
                                <i class="bi bi-x"></i>
                            </a>
                        @endif
                    </div>
                </form>
            </div>
            <div class="col-md-6">
                <form method="GET" action="{{ route('customers.index') }}" class="d-flex justify-content-end">
                    <div class="input-group" style="width: auto;">
                        <label for="is_owner" class="input-group-text">Filter by Owner:</label>
                        <select name="is_owner" id="is_owner" class="form-select" onchange="this.form.submit()">
                            <option value="">All</option>
                            <option value="1" {{ request('is_owner') == '1' ? 'selected' : '' }}>Owner</option>
                            <option value="0" {{ request('is_owner') == '0' ? 'selected' : '' }}>Not Owner</option>
                        </select>
                        @if(request('is_owner') !== null)
                            <a href="{{ route('customers.index', ['search' => request('search')]) }}" 
                               class="btn btn-outline-secondary">
                                <i class="bi bi-x"></i>
                            </a>
                        @endif
                    </div>
                </form>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>Type</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($customers as $user)
                        <tr>
                            <td>{{ $user->name }}</td>
                            <td>{{ $user->email }}</td>
                            <td>{{ $user->phone }}</td>
                            <td>
                                @if($user->is_owner)
                                    <span class="badge bg-primary">Owner</span>
                                @else
                                    <span class="badge bg-secondary">Customer</span>
                                @endif
                            </td>
                            <td>
                                @if($user->deactivated_at)
                                    <span class="badge bg-danger">Deactivated</span>
                                @else
                                    <span class="badge bg-success">Active</span>
                                @endif
                            </td>
                            <td>
                                <div class="btn-group" role="group">
                                    <a href="{{ route('customers.edit', $user) }}" 
                                       class="btn btn-sm btn-info text-white me-2"
                                       data-bs-toggle="tooltip"
                                       data-bs-placement="top"
                                       title="Edit Customer">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <form action="{{ route('customers.reset-password', $user) }}" 
                                          method="POST" 
                                          class="d-inline me-2">
                                        @csrf
                                        <button type="submit" 
                                                class="btn btn-sm btn-warning"
                                                data-bs-toggle="tooltip"
                                                data-bs-placement="top"
                                                title="Reset Password"
                                                onclick="return confirm('Are you sure you want to reset the password?')">
                                            <i class="bi bi-key"></i>
                                        </button>
                                    </form>
                                    <form action="{{ route('customers.toggle-status', $user) }}" 
                                          method="POST" 
                                          class="d-inline">
                                        @csrf
                                        <button type="submit" 
                                                class="btn btn-sm {{ $user->deactivated_at ? 'btn-success' : 'btn-danger' }}"
                                                data-bs-toggle="tooltip"
                                                data-bs-placement="top"
                                                title="{{ $user->deactivated_at ? 'Activate Customer' : 'Deactivate Customer' }}"
                                                onclick="return confirm('Are you sure you want to {{ $user->deactivated_at ? 'activate' : 'deactivate' }} this customer?')">
                                            <i class="bi {{ $user->deactivated_at ? 'bi-person-check' : 'bi-person-x' }}"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="text-center py-4">
                                @if(request('search'))
                                    No customers found matching your search criteria.
                                @else
                                    No customers found.
                                @endif
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

@push('scripts')
<script>
    // Initialize all tooltips
    var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
    var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl)
    })
</script>
@endpush
@endsection 