@extends('layouts.app')

@section('title', 'Staff')
@section('page-title', 'Staff Management')
@section('page-subtitle', 'Tenant users, base roles, and customizable role profiles')

@section('content')
@php($showBranchField = $showBranchField ?? $branches->count() > 1)
<div class="row g-4">
    <div class="col-lg-4">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <h2 class="h5">Add staff</h2>
                <form method="POST" action="{{ route('tenant.staff.store') }}" class="vstack gap-2">
                    @csrf
                    <input name="name" class="form-control" placeholder="Full name" required>
                    <input name="email" type="email" class="form-control" placeholder="Email" required>
                    <input name="phone" class="form-control" placeholder="Phone">
                    <select name="role" class="form-select" required>
                        @foreach($roles as $value => $label)
                            <option value="{{ $value }}">{{ $label }}</option>
                        @endforeach
                    </select>
                    <select name="role_profile_id" class="form-select">
                        <option value="">Use base role permissions</option>
                        @foreach($roleProfiles as $profile)
                            <option value="{{ $profile->id }}">{{ $profile->name }} - {{ $profile->permissionCount() }} permissions</option>
                        @endforeach
                    </select>
                    @if($showBranchField)
                        <select name="branch_id" class="form-select" required>
                            @foreach($branches as $branch)
                                <option value="{{ $branch->id }}" @selected((string) ($defaultBranchId ?? '') === (string) $branch->id)>{{ $branch->name }}</option>
                            @endforeach
                        </select>
                    @else
                        <input type="hidden" name="branch_id" value="{{ $defaultBranchId }}">
                    @endif
                    <input name="password" type="password" class="form-control" placeholder="Temporary password" required>
                    <select name="status" class="form-select"><option value="active">Active</option><option value="inactive">Inactive</option></select>
                    <button class="btn btn-primary">Add staff</button>
                </form>
                <div class="small text-muted mt-3">
                    Create detailed access profiles from Settings > Role Profiles, then assign them here.
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-8">
        <div class="card border-0 shadow-sm">
            <div class="table-responsive">
                <table class="table mb-0"><thead><tr><th>Name</th><th>Email</th><th>Role</th><th>Profile</th>@if($showBranchField)<th>Branch</th>@endif<th>Status</th><th></th></tr></thead><tbody>
                @foreach($staff as $member)
                    <tr>
                        <form method="POST" action="{{ route('tenant.staff.update', $member) }}">
                            @csrf @method('PATCH')
                            <td><input name="name" value="{{ $member->name }}" class="form-control form-control-sm"></td>
                            <td>{{ $member->email }}</td>
                            <td><select name="role" class="form-select form-select-sm">@foreach($roles as $value => $label)<option value="{{ $value }}" @selected($member->role===$value)>{{ $label }}</option>@endforeach</select></td>
                            <td>
                                <select name="role_profile_id" class="form-select form-select-sm">
                                    <option value="">Base role</option>
                                    @foreach($roleProfiles as $profile)
                                        <option value="{{ $profile->id }}" @selected($member->role_profile_id === $profile->id)>{{ $profile->name }}</option>
                                    @endforeach
                                </select>
                            </td>
                            @if($showBranchField)
                                <td><select name="branch_id" class="form-select form-select-sm" required>@foreach($branches as $branch)<option value="{{ $branch->id }}" @selected((string) ($member->branch_id ?? $defaultBranchId) === (string) $branch->id)>{{ $branch->name }}</option>@endforeach</select></td>
                            @else
                                <input type="hidden" name="branch_id" value="{{ $member->branch_id ?: $defaultBranchId }}">
                            @endif
                            <td><select name="status" class="form-select form-select-sm"><option @selected($member->status==='active') value="active">Active</option><option @selected($member->status==='inactive') value="inactive">Inactive</option></select><input type="hidden" name="phone" value="{{ $member->phone }}"></td>
                            <td class="text-end"><button class="btn btn-sm btn-outline-primary">Save</button>
                        </form>
                            @if($member->id !== auth()->id())
                                <form method="POST" action="{{ route('tenant.staff.destroy', $member) }}" class="d-inline">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger">Deactivate</button></form>
                            @endif
                            </td>
                    </tr>
                @endforeach
                </tbody></table>
            </div>
            <div class="card-body">{{ $staff->links() }}</div>
        </div>
    </div>
</div>
@endsection
