@extends('layouts.app')
@section('title', 'Asset Categories')
@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item"><a href="#">Fixed Assets</a></li>
    <li class="breadcrumb-item active">Asset Categories</li>
@endsection

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title"><i class="bi bi-tags me-2 text-primary"></i>Asset Categories</h1>
        <p class="page-subtitle">{{ $stats['total'] }} total &nbsp;·&nbsp; <span class="text-success">{{ $stats['active'] }} active</span></p>
    </div>
    @if(auth()->user()->hasPermission('fixed_assets', 'edit'))
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#categoryAddModal">
        <i class="bi bi-plus-lg me-1"></i>New Category
    </button>
    @endif
</div>

@if(session('success'))
<div class="alert alert-success alert-dismissible d-flex gap-2 mb-3">
    <i class="bi bi-check-circle flex-shrink-0"></i><div>{{ session('success') }}</div>
    <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
</div>
@endif
@if($errors->any())
<div class="alert alert-danger alert-dismissible mb-3">
    @foreach($errors->all() as $e)<div>{{ $e }}</div>@endforeach
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
@endif

<div class="card mb-3">
    <div class="card-body py-2 px-3">
        <form method="GET">
            <div class="row g-2 align-items-center">
                <div class="col-12 col-sm-5">
                    <input type="text" name="search" class="form-control form-control-sm" placeholder="Search name…" value="{{ request('search') }}">
                </div>
                <div class="col-6 col-sm-2">
                    <select name="status" class="form-select form-select-sm">
                        <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-1">
                    <button type="submit" class="btn btn-primary btn-sm w-100"><i class="bi bi-search"></i></button>
                </div>
                <div class="col-3 col-sm-1">
                    <a href="{{ route('fixed-assets.categories.index') }}" class="btn btn-outline-secondary btn-sm 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">
                <thead class="table-light">
                    <tr>
                        <th class="ps-4">Name</th>
                        <th style="width:120px">Status</th>
                        <th class="text-end pe-4" style="width:140px">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($records as $record)
                    <tr class="{{ !$record->is_active ? 'table-secondary' : '' }}">
                        <td class="ps-4 fw-semibold" style="font-size:13.5px">{{ $record->name }}</td>
                        <td>
                            <span class="badge" style="font-size:11px;background:{{ $record->is_active ? '#d1e7dd' : '#e9ecef' }};color:{{ $record->is_active ? '#0a3622' : '#495057' }}">
                                {{ $record->is_active ? 'Active' : 'Inactive' }}
                            </span>
                        </td>
                        <td class="text-end pe-3">
                            @if(auth()->user()->hasPermission('fixed_assets', 'edit'))
                            <div class="d-flex justify-content-end gap-1">
                                <button type="button" class="btn btn-outline-secondary btn-sm btn-edit-category"
                                        data-name="{{ $record->name }}"
                                        data-active="{{ $record->is_active ? '1' : '0' }}"
                                        data-url="{{ route('fixed-assets.categories.update', $record) }}">
                                    <i class="bi bi-pencil"></i>
                                </button>
                                <form method="POST" action="{{ route('fixed-assets.categories.toggle', $record) }}">
                                    @csrf @method('PATCH')
                                    <button type="submit" class="btn btn-sm {{ $record->is_active ? 'btn-outline-warning' : 'btn-outline-success' }}"><i class="bi bi-{{ $record->is_active ? 'eye-slash' : 'eye' }}"></i></button>
                                </form>
                                <form method="POST" action="{{ route('fixed-assets.categories.destroy', $record) }}" onsubmit="return confirm('Delete {{ addslashes($record->name) }}?')">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="btn btn-outline-danger btn-sm"><i class="bi bi-trash"></i></button>
                                </form>
                            </div>
                            @endif
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="3" class="text-center py-5 text-muted">No categories found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($records->hasPages())
        <div class="px-4 py-3 border-top">{{ $records->links() }}</div>
        @endif
    </div>
</div>

@if(auth()->user()->hasPermission('fixed_assets', 'edit'))
<div class="modal fade" id="categoryAddModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header border-0 pb-0">
                <h5 class="modal-title fw-semibold">New Category</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="{{ route('fixed-assets.categories.store') }}">
                @csrf
                <div class="modal-body pt-2">
                    <label class="form-label fw-semibold">Name <span class="text-danger">*</span></label>
                    <input type="text" name="name" class="form-control" required maxlength="120" autofocus>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="categoryEditModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header border-0 pb-0">
                <h5 class="modal-title fw-semibold">Edit Category</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" id="categoryEditForm">
                @csrf @method('PUT')
                <div class="modal-body pt-2">
                    <label class="form-label fw-semibold">Name <span class="text-danger">*</span></label>
                    <input type="text" name="name" id="categoryEditName" class="form-control" required maxlength="120">
                    <div class="form-check form-switch mt-3">
                        <input class="form-check-input" type="checkbox" name="is_active" value="1" id="categoryEditActive">
                        <label class="form-check-label fw-semibold" for="categoryEditActive">Active</label>
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Save Changes</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endif
@endsection

@push('scripts')
<script>
document.querySelectorAll('.btn-edit-category').forEach(function (btn) {
    btn.addEventListener('click', function () {
        document.getElementById('categoryEditForm').action = this.dataset.url;
        document.getElementById('categoryEditName').value = this.dataset.name;
        document.getElementById('categoryEditActive').checked = this.dataset.active === '1';
        if (typeof bootstrap !== 'undefined') {
            bootstrap.Modal.getOrCreateInstance(document.getElementById('categoryEditModal')).show();
        }
    });
});
</script>
@endpush
