@extends('layouts.app')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="mb-0">Departments</h4>
        <a href="{{ route('users.index') }}" class="btn btn-outline-secondary btn-sm">Users</a>
    </div>
    <div class="row g-4">
        <div class="col-lg-4">
            <div class="card shadow-sm">
                <div class="card-header fw-bold">Add department</div>
                <div class="card-body">
                    <form method="POST" action="{{ route('departments.master.store') }}" class="row g-2">
                        @csrf
                        <div class="col-12">
                            <label class="form-label">Name <span class="text-danger">*</span></label>
                            <input name="name" class="form-control" required maxlength="120" value="{{ old('name') }}">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Slug</label>
                            <input name="slug" class="form-control" maxlength="120" placeholder="optional" value="{{ old('slug') }}">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Description</label>
                            <input name="description" class="form-control" maxlength="255" value="{{ old('description') }}">
                        </div>
                        <div class="col-12 form-check">
                            <input type="hidden" name="is_active" value="0">
                            <input class="form-check-input" type="checkbox" name="is_active" value="1" id="act" {{ old('is_active', true) ? 'checked' : '' }}>
                            <label class="form-check-label" for="act">Active</label>
                        </div>
                        <div class="col-12 d-grid">
                            <button class="btn btn-success">Save</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-8">
            <div class="card shadow-sm">
                <div class="card-header fw-bold">All departments</div>
                <div class="card-body p-0 table-responsive">
                    <table class="table table-sm mb-0">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Slug</th>
                            <th>Users</th>
                            <th>Active</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($departments as $d)
                            <tr>
                                <td>{{ $d->name }}</td>
                                <td><code>{{ $d->slug }}</code></td>
                                <td>{{ $d->users_count }}</td>
                                <td>{{ $d->is_active ? 'Yes' : 'No' }}</td>
                                <td class="text-end">
                                    <a href="{{ route('departments.master.edit', $d) }}" class="btn btn-sm btn-outline-secondary">Edit</a>
                                    <form method="POST" action="{{ route('departments.master.destroy', $d) }}" class="d-inline" onsubmit="return confirm('Delete this department?');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="text-center text-muted py-3">No departments.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection
