@extends('layouts.app')
@section('title', $department->exists ? 'Editar departamento' : 'Novo departamento')
@section('content')
    <div class="page-heading"><div><span class="eyebrow">ADMINISTRAÇÃO · DEPARTAMENTOS</span><h1>{{ $department->exists ? 'Editar '.$department->name : 'Criar departamento' }}</h1><p>A estrutura pode evoluir sem perder o histórico anterior.</p></div><a class="secondary-link" href="{{ route('admin.departments.index') }}">← Voltar</a></div>
    @if($errors->any())<div class="alert danger">{{ $errors->first() }}</div>@endif
    <form class="panel management-form narrow" method="POST" action="{{ $department->exists ? route('admin.departments.update', $department) : route('admin.departments.store') }}">@csrf @if($department->exists)@method('PUT')@endif
        <div class="form-grid"><label class="field"><span>Organização</span><select name="organization_id" required>@foreach($organizations as $organization)<option value="{{ $organization->id }}" @selected(old('organization_id', $department->organization_id ?: $organizations->first()?->id) == $organization->id)>{{ $organization->name }}</option>@endforeach</select></label><label class="field"><span>Código único</span><input name="code" value="{{ old('code', $department->code) }}" required></label><label class="field span-2"><span>Nome</span><input name="name" value="{{ old('name', $department->name) }}" required></label><label class="field span-2"><span>Descrição</span><textarea name="description" rows="5">{{ old('description', $department->description) }}</textarea></label></div>
        <div class="form-options"><label><input type="checkbox" name="is_active" value="1" @checked(old('is_active', $department->exists ? $department->is_active : true))> Departamento activo</label></div><div class="form-actions"><a class="secondary-link" href="{{ route('admin.departments.index') }}">Cancelar</a><button class="primary-button compact" type="submit">Guardar departamento</button></div>
    </form>
@endsection
