@extends('layouts.app')

@section('title', 'Suppliers Management - Harshana Systems')
@section('page-title', 'Suppliers Management')

@section('content')
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h1 class="h3 mb-0 page-title-theme">Suppliers Management</h1>
                    </div>
                    <div class="card-body">
                        @if(auth()->user() && auth()->user()->hasAccessTo('suppliers.create'))
                            <a href="{{ route('suppliers.create') }}" class="btn btn-primary mb-3">
                                <i class="fas fa-plus me-2"></i>Add New Supplier
                            </a>
                        @endif

                        <!-- Search Form -->
                        <form method="GET" action="{{ route('suppliers.index') }}" class="mb-3">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="input-group">
                                        <input type="text" name="search" class="form-control" placeholder="Search by supplier name..." value="{{ $search ?? '' }}">
                                        <button class="btn btn-outline-secondary" type="submit">
                                            <i class="fas fa-search"></i>
                                        </button>
                                        @if($search)
                                            <a href="{{ route('suppliers.index') }}" class="btn btn-outline-danger">
                                                <i class="fas fa-times"></i>
                                            </a>
                                        @endif
                                    </div>
                                </div>
                            </div>
                        </form>

                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead class="table-dark">
                                    <tr>
                                        <th>ID</th>
                                        <th>Supplier Name</th>
                                        <th>Contact Number</th>
                                        <th>Status</th>
                                        <th>Created By</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($suppliers as $supplier)
                                        <tr>
                                            <td>{{ $supplier->id }}</td>
                                            <td>{{ $supplier->supplier_name }}</td>
                                            <td>{{ $supplier->contact_number ?? 'N/A' }}</td>
                                            <td>
                                                @if($supplier->is_active)
                                                    <span class="badge bg-success">Active</span>
                                                @else
                                                    <span class="badge bg-danger">Inactive</span>
                                                @endif
                                            </td>
                                            <td>{{ $supplier->creator->name ?? 'N/A' }}</td>
                                            <td>
                                                @if(auth()->user() && auth()->user()->hasAccessTo('suppliers.view'))
                                                    <a href="{{ route('suppliers.show', $supplier) }}" class="btn btn-sm btn-info me-1">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                @endif
                                                @if(auth()->user() && auth()->user()->hasAccessTo('suppliers.edit'))
                                                    <a href="{{ route('suppliers.edit', $supplier) }}" class="btn btn-sm btn-warning me-1">
                                                        <i class="fas fa-edit"></i>
                                                    </a>
                                                @endif
                                                @if(auth()->user() && auth()->user()->hasAccessTo('suppliers.delete'))
                                                    <form method="POST" action="{{ route('suppliers.destroy', $supplier) }}" style="display: inline;">
                                                        @csrf
                                                        @method('DELETE')
                                                        <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">
                                                            <i class="fas fa-trash"></i>
                                                        </button>
                                                    </form>
                                                @endif
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="6" class="text-center">No suppliers found.</td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>

                        {{ $suppliers->appends(request()->except('page'))->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection