@extends('layouts.app')
@section('title', 'Vendors')

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item active">Vendors</li>
@endsection

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">Vendors</h1>
        <p class="page-subtitle">Manage your supplier and vendor list</p>
    </div>
    @if(auth()->user()->hasPermission('ap','create'))
    <a href="{{ route('masters.vendors.create') }}" class="btn btn-primary">
        <i class="bi bi-plus-circle me-1"></i><span class="d-none d-sm-inline">Add Vendor</span>
    </a>
    @endif
</div>

{{-- Stats --}}
<div class="row g-3 mb-4">
    @foreach([['Total','total','#EBF3FB','#1B4F8A'],['Active','active','#d1e7dd','#0a3622'],['Inactive','inactive','#f8d7da','#842029']] as [$label,$key,$bg,$color])
    <div class="col-4">
        <div class="p-3 rounded-3 text-center" style="background:{{ $bg }}">
            <div style="font-size:11px;color:{{ $color }};font-weight:700;text-transform:uppercase;letter-spacing:.5px">{{ $label }}</div>
            <div style="font-size:24px;font-weight:700;color:{{ $color }}">{{ $stats[$key] }}</div>
        </div>
    </div>
    @endforeach
</div>

<div class="card mb-3">
    <div class="card-body py-3">
        <form method="GET" action="{{ route('masters.vendors.index') }}">
            <div class="row g-2 align-items-end">
                <div class="col-12 col-sm-5 col-md-6">
                    <input type="text" name="search" class="form-control"
                           placeholder="Search by name, code, email or phone…"
                           value="{{ request('search') }}">
                </div>
                <div class="col-6 col-sm-3 col-md-2">
                    <select name="status" class="form-select">
                        <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>
                <div class="col-3 col-sm-2 col-md-2">
                    <button type="submit" class="btn btn-primary w-100"><i class="bi bi-search"></i></button>
                </div>
                <div class="col-3 col-sm-2 col-md-2">
                    <a href="{{ route('masters.vendors.index') }}" class="btn btn-outline-secondary w-100"><i class="bi bi-x-lg"></i></a>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0" style="min-width:450px">
                <thead class="table-light">
                    <tr>
                        <th>Vendor</th>
                        <th class="d-none d-md-table-cell">Contact</th>
                        <th class="d-none d-lg-table-cell">Currency</th>
                        <th class="d-none d-lg-table-cell">Terms</th>
                        <th>Status</th>
                        <th class="text-end pe-3" style="width:90px">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($vendors as $vendor)
                    <tr>
                        <td>
                            <div class="fw-semibold" style="font-size:13.5px">{{ $vendor->name }}</div>
                            @if($vendor->code)<code style="font-size:11.5px">{{ $vendor->code }}</code>@endif
                            @if($vendor->phone)
                            <div class="d-md-none text-muted" style="font-size:12px">
                                <i class="bi bi-telephone me-1"></i>{{ $vendor->phone }}
                            </div>
                            @endif
                        </td>
                        <td class="d-none d-md-table-cell">
                            @if($vendor->email)<div style="font-size:13px">{{ $vendor->email }}</div>@endif
                            @if($vendor->phone)<div class="text-muted" style="font-size:12px">{{ $vendor->phone }}</div>@endif
                            @if(!$vendor->email && !$vendor->phone)<span class="text-muted">—</span>@endif
                        </td>
                        <td class="d-none d-lg-table-cell" style="font-size:13px">{{ $vendor->currency?->code ?? '—' }}</td>
                        <td class="d-none d-lg-table-cell" style="font-size:13px">{{ $vendor->payment_terms_days }} days</td>
                        <td>
                            <span class="badge" style="background:{{ $vendor->is_active ? '#d1e7dd':'#f8d7da' }};color:{{ $vendor->is_active ? '#0a3622':'#842029' }};font-size:11px">
                                {{ $vendor->is_active ? 'Active' : 'Inactive' }}
                            </span>
                        </td>
                        <td class="text-end pe-2">
                            <div class="d-flex justify-content-end gap-1">
                                @if(auth()->user()->hasPermission('ap','edit'))
                                <a href="{{ route('masters.vendors.edit', $vendor) }}" class="btn btn-outline-primary btn-sm" title="Edit">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <form method="POST" action="{{ route('masters.vendors.toggle', $vendor) }}" id="tv-{{ $vendor->id }}">
                                    @csrf @method('PATCH')
                                    <button type="button" class="btn btn-sm {{ $vendor->is_active ? 'btn-outline-warning':'btn-outline-success' }}"
                                        data-confirm data-form="#tv-{{ $vendor->id }}"
                                        data-confirm-title="{{ $vendor->is_active ? 'Deactivate?':'Activate?' }}"
                                        data-confirm-text="{{ $vendor->name }}" data-confirm-icon="question"
                                        data-confirm-color="{{ $vendor->is_active ? '#E67E22':'#27AE60' }}"
                                        data-confirm-btn="{{ $vendor->is_active ? 'Deactivate':'Activate' }}"
                                        title="{{ $vendor->is_active ? 'Deactivate':'Activate' }}">
                                        <i class="bi bi-{{ $vendor->is_active ? 'pause-circle':'play-circle' }}"></i>
                                    </button>
                                </form>
                                <form method="POST" action="{{ route('masters.vendors.destroy', $vendor) }}" id="dv-{{ $vendor->id }}">
                                    @csrf @method('DELETE')
                                    <button type="button" class="btn btn-outline-danger btn-sm"
                                        data-confirm data-form="#dv-{{ $vendor->id }}"
                                        data-confirm-title="Delete permanently?"
                                        data-confirm-text="{{ $vendor->name }} can only be deleted if it has never been used in transactions. Used vendors should be deactivated instead."
                                        data-confirm-icon="warning"
                                        data-confirm-color="#DC2626"
                                        data-confirm-btn="Delete permanently"
                                        title="Delete permanently">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                                @endif
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" class="text-center py-5 text-muted">
                            <i class="bi bi-building display-5 d-block mb-2"></i>
                            <p class="mb-3">No vendors found.</p>
                            <a href="{{ route('masters.vendors.create') }}" class="btn btn-primary btn-sm">
                                <i class="bi bi-plus-circle me-1"></i>Add First Vendor
                            </a>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($vendors->hasPages())
        <div class="px-4 py-3 border-top">{{ $vendors->links() }}</div>
        @endif
    </div>
</div>
@endsection
