@extends('layouts.app')

@section('title', 'Edit Director Appointment - ' . $company->company_name)

@section('content')
<div class="row mb-4">
    <div class="col">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                <li class="breadcrumb-item"><a href="{{ route('companies.index') }}">Companies</a></li>
                <li class="breadcrumb-item"><a href="{{ route('companies.show', $company) }}">{{ $company->company_name }}</a></li>
                <li class="breadcrumb-item"><a href="{{ route('companies.appointments.index', $company) }}">Appointments</a></li>
                <li class="breadcrumb-item active">Edit Appointment</li>
            </ol>
        </nav>
        <h1 class="h3">
            <i class="bi bi-pencil"></i> Edit Director Appointment
        </h1>
    </div>
</div>

<div class="card mb-4">
    <div class="card-header bg-light">
        <h5 class="mb-0">
            <i class="bi bi-building"></i> {{ $company->company_name }}
            <small class="text-muted">(CI: {{ $company->ci_number }})</small>
        </h5>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-body">
                <form action="{{ route('companies.appointments.update', [$company, $appointment]) }}" method="POST">
                    @csrf
                    @method('PUT')
                    
                    <div class="mb-3">
                        <label for="director_id" class="form-label">
                            Director / 董事 <span class="text-danger">*</span>
                        </label>
                        <select class="form-select @error('director_id') is-invalid @enderror" 
                                id="director_id" 
                                name="director_id" 
                                required>
                            <option value="">-- Select Director / 選擇董事 --</option>
                            @foreach($directors as $director)
                                <option value="{{ $director->id }}" {{ old('director_id', $appointment->director_id) == $director->id ? 'selected' : '' }}>
                                    {{ $director->full_name }}
                                    @if($director->hkid_or_ci_number)
                                        ({{ $director->hkid_or_ci_number }})
                                    @endif
                                </option>
                            @endforeach
                        </select>
                        @error('director_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="date_of_appointment" class="form-label">
                                    Date of Appointment / 委任日期 <span class="text-danger">*</span>
                                </label>
                                <input type="date" 
                                       class="form-control @error('date_of_appointment') is-invalid @enderror" 
                                       id="date_of_appointment" 
                                       name="date_of_appointment" 
                                       value="{{ old('date_of_appointment', $appointment->date_of_appointment->format('Y-m-d')) }}"
                                       required>
                                @error('date_of_appointment')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="date_of_ceasing" class="form-label">
                                    Date of Ceasing / 離職日期
                                </label>
                                <input type="date" 
                                       class="form-control @error('date_of_ceasing') is-invalid @enderror" 
                                       id="date_of_ceasing" 
                                       name="date_of_ceasing" 
                                       value="{{ old('date_of_ceasing', $appointment->date_of_ceasing?->format('Y-m-d')) }}">
                                @error('date_of_ceasing')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                <small class="text-muted">Leave empty if director is still active</small>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="remarks" class="form-label">
                            Remarks / 備註
                        </label>
                        <textarea class="form-control @error('remarks') is-invalid @enderror" 
                                  id="remarks" 
                                  name="remarks" 
                                  rows="3">{{ old('remarks', $appointment->remarks) }}</textarea>
                        @error('remarks')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-lg"></i> Update Appointment
                        </button>
                        <a href="{{ route('companies.appointments.index', $company) }}" class="btn btn-secondary">
                            <i class="bi bi-x-lg"></i> Cancel
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card border-danger">
            <div class="card-header bg-danger text-white">
                <h6 class="mb-0"><i class="bi bi-exclamation-triangle"></i> Delete Appointment</h6>
            </div>
            <div class="card-body">
                <p class="small text-muted">
                    Permanently delete this director appointment record.
                </p>
                <form action="{{ route('companies.appointments.destroy', [$company, $appointment]) }}" method="POST" onsubmit="return confirm('Are you sure you want to delete this appointment?');">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger btn-sm">
                        <i class="bi bi-trash"></i> Delete Appointment
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection