@extends('layouts.app')

@section('title', 'Suppliers')

@section('actions')
    <a href="{{ route('suppliers.create') }}" class="btn btn-primary">
        <i class="fas fa-plus"></i> New Supplier
    </a>
@endsection

@section('content')
<div class="container-fluid">
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">Suppliers List</h5>
        </div>
        <div class="card-body">
            @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show">
                <i class="fas fa-check-circle"></i> {{ session('success') }}
                <button type="button" class="close" data-dismiss="alert">&times;</button>
            </div>
            @endif
            
            @if(session('error'))
            <div class="alert alert-danger alert-dismissible fade show">
                <i class="fas fa-exclamation-circle"></i> {{ session('error') }}
                <button type="button" class="close" data-dismiss="alert">&times;</button>
            </div>
            @endif
            
            <!-- Search Form -->
            <form method="GET" class="row mb-4">
                <div class="col-md-8">
                    <div class="input-group">
                        <input type="text" name="search" class="form-control" 
                               value="{{ request('search') }}" placeholder="Search suppliers...">
                        <div class="input-group-append">
                            <button class="btn btn-primary" type="submit">
                                <i class="fas fa-search"></i> Search
                            </button>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <select name="status" class="form-control" onchange="this.form.submit()">
                        <option value="">All Status</option>
                        <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>Active</option>
                        <option value="inactive" {{ request('status') == 'inactive' ? 'selected' : '' }}>Inactive</option>
                    </select>
                </div>
            </form>
            
            <!-- Suppliers Table -->
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Code</th>
                            <th>Name</th>
                            <th>Contact</th>
                            <th>Balance</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($suppliers as $supplier)
                        <tr>
                            <td>
                                <strong>{{ $supplier->supplier_code }}</strong>
                            </td>
                            <td>
                                <strong>{{ $supplier->name }}</strong>
                                @if($supplier->company_name)
                                <br>
                                <small class="text-muted">{{ $supplier->company_name }}</small>
                                @endif
                            </td>
                            <td>
                                <i class="fas fa-phone"></i> {{ $supplier->phone }}
                                @if($supplier->email)
                                <br>
                                <i class="fas fa-envelope"></i> {{ $supplier->email }}
                                @endif
                            </td>
                            <td>
                                <strong class="{{ $supplier->current_balance > 0 ? 'text-danger' : 'text-success' }}">
                                    ৳{{ number_format($supplier->current_balance, 2) }}
                                </strong>
                                <br>
                                <small class="text-muted">Limit: ৳{{ number_format($supplier->credit_limit, 2) }}</small>
                            </td>
                            <td>
                                <span class="badge badge-{{ $supplier->status == 'active' ? 'success' : 'secondary' }}">
                                    {{ ucfirst($supplier->status) }}
                                </span>
                            </td>
                            <td>
                                <div class="btn-group btn-group-sm">
                                    <a href="{{ route('suppliers.show', $supplier) }}" class="btn btn-info">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    <a href="{{ route('suppliers.edit', $supplier) }}" class="btn btn-warning">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <button type="button" class="btn btn-danger delete-btn" 
                                            data-id="{{ $supplier->id }}" data-name="{{ $supplier->name }}">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="text-center py-4">
                                <div class="text-muted">
                                    <i class="fas fa-users fa-3x mb-3"></i>
                                    <h5>No suppliers found</h5>
                                    <p>Start by adding your first supplier</p>
                                    <a href="{{ route('suppliers.create') }}" class="btn btn-primary">
                                        <i class="fas fa-plus"></i> Add Supplier
                                    </a>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            
            <!-- Pagination -->
            @if($suppliers->hasPages())
            <div class="mt-3">
                {{ $suppliers->links() }}
            </div>
            @endif
        </div>
    </div>
</div>

<!-- Delete Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Confirm Delete</h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to delete <strong id="deleteSupplierName"></strong>?</p>
                <p class="text-danger">This action cannot be undone!</p>
            </div>
            <div class="modal-footer">
                <form id="deleteForm" method="POST">
                    @csrf
                    @method('DELETE')
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
    $(document).ready(function() {
        // Handle delete button click
        $('.delete-btn').click(function() {
            var supplierId = $(this).data('id');
            var supplierName = $(this).data('name');
            
            $('#deleteSupplierName').text(supplierName);
            $('#deleteForm').attr('action', '/suppliers/' + supplierId);
            $('#deleteModal').modal('show');
        });
    });
</script>
@endpush