@extends('layouts.app')

@section('content')
<div class="container-fluid px-4 py-6">
    <div class="max-w-7xl mx-auto">
        <!-- Header -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 class="mb-1">Deployment Management</h2>
                <p class="text-muted mb-0">Track active and completed deployments</p>
            </div>
            <a href="{{ route('deployments.create') }}" class="btn btn-primary">
                <i class="bi bi-plus-lg"></i> Create Deployment
            </a>
        </div>

        <!-- Success Message -->
        @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                {{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif

        <!-- Deployments List -->
        @forelse($deployments as $deployment)
            <div class="card shadow-sm mb-4">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start mb-3">
                        <div>
                            <h4 class="mb-2">
                                {{ optional($deployment->ndrtOfficer)->name ?? (optional($deployment->ndrtOfficer)->first_name . ' ' . optional($deployment->ndrtOfficer)->surname) ?: 'NDRT Officer' }}
                            </h4>
                            <p class="text-muted mb-0">{{ $deployment->mission_description ? Str::limit($deployment->mission_description, 120) : 'No description' }}</p>
                        </div>
                        
                        @if (strtolower($deployment->status) === 'pending' && Auth::user()->isAdmin() || Auth::user()->isOperationsManager())
                            <!-- Approve Button, which submits a form or triggers an action -->
                            <form action="{{ route('deployments.approve', $deployment->id) }}" method="POST" style="display:inline;">
                                @csrf
                                @method('PATCH')
                                <button type="submit" class="btn btn-success btn-sm">Approve Deployment</button>
                            </form>
                        @else
                            <span class="badge {{ $deployment->status === 'completed' ? 'bg-secondary' : 'bg-success' }}">
                                {{ ucfirst($deployment->status ?? 'active') }}
                            </span>
                        @endif
                        </div>

                    <div class="row g-4 mb-3">
                        <div class="col-md-3">
                            <div class="d-flex gap-2">
                                <i class="bi bi-calendar3 text-muted"></i>
                                <div>
                                    <div class="text-muted small">Period</div>
                                    <div class="fw-semibold">
                                        {{ optional($deployment->start_date)->format('M d, Y') ?? '—' }} - {{ optional($deployment->end_date)->format('M d, Y') ?? '—' }}
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="d-flex gap-2">
                                <i class="bi bi-geo-alt text-muted"></i>
                                <div>
                                    <div class="text-muted small">Location</div>
                                    <div class="fw-semibold">{{ $deployment->mission_location ?? '—' }}</div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="d-flex gap-2">
                                <i class="bi bi-person-check text-muted"></i>
                                <div>
                                    <div class="text-muted small">Mentor</div>
                                    <div class="fw-semibold">
                                        {{ $deployment->has_mentor ? ($deployment->mentor_name ?: 'Yes') : 'No' }}
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="d-flex gap-2">
                                <i class="bi bi-arrow-repeat text-muted"></i>
                                <div>
                                    <div class="text-muted small">Extended</div>
                                    <div class="fw-semibold">
                                        {{ $deployment->is_extended ? 'Yes' : 'No' }}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Action Buttons -->
                    @if(Auth::user()->isAdmin() || Auth::user()->isOperationsManager())
                    <div class="d-flex gap-2 pt-2 border-top">
                        <a href="{{ route('deployments.show', $deployment) }}" class="btn btn-sm btn-outline-primary">
                            <i class="fas fa-eye"></i> View
                        </a>
                        <a href="{{ route('deployments.edit', $deployment) }}" class="btn btn-sm btn-outline-secondary">
                            <i class="fas fa-pencil-alt"></i> Edit/Extend Mission
                        </a>
                        @if(($deployment->status ?? 'active') !== 'completed')
                            <button type="button" class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#endMissionModal{{ $deployment->id }}">
                                <i class="fas fa-flag"></i> End Mission
                            </button>
                        @endif
                        <form action="{{ route('deployments.destroy', $deployment) }}" method="POST" class="d-inline" onsubmit="return confirm('Delete this deployment?');">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-sm btn-outline-danger">
                                <i class="fas fa-trash"></i> Delete
                            </button>
                        </form>
                    </div>
                    @endif
                </div>
            </div>

            <!-- End Mission Modal -->
            <div class="modal fade" id="endMissionModal{{ $deployment->id }}" tabindex="-1" aria-labelledby="endMissionLabel{{ $deployment->id }}" aria-hidden="true">
                <div class="modal-dialog modal-lg modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="endMissionLabel{{ $deployment->id }}">End Mission - {{ optional($deployment->ndrtOfficer)->name ?? (optional($deployment->ndrtOfficer)->first_name . ' ' . optional($deployment->ndrtOfficer)->surname) }}</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <form action="{{ route('deployments.end-mission', $deployment) }}" method="POST" enctype="multipart/form-data">
                            @csrf
                            @method('PATCH')
                            <div class="modal-body">
                                <div class="row g-3">
                                    <div class="col-md-4">
                                        <label for="mission_end_date_{{ $deployment->id }}" class="form-label fw-semibold">Mission Date Ended <span class="text-danger">*</span></label>
                                        <input type="date" id="mission_end_date_{{ $deployment->id }}" name="mission_end_date" class="form-control" value="{{ date('Y-m-d') }}" required>
                                    </div>
                                    <div class="col-md-8">
                                        <label class="form-label fw-semibold">Attach Report</label>
                                        <input type="file" class="form-control" name="report_file" accept=".pdf,.doc,.docx">
                                        <small class="text-muted">PDF or DOC up to 10MB</small>
                                    </div>
                                    <div class="col-12">
                                        <label for="branch_debrief_remarks_{{ $deployment->id }}" class="form-label fw-semibold">Remarks from Debrief at the Branch</label>
                                        <textarea id="branch_debrief_remarks_{{ $deployment->id }}" name="branch_debrief_remarks" class="form-control" rows="3" placeholder="Branch debrief remarks..."></textarea>
                                    </div>
                                    <div class="col-12">
                                        <label for="hq_debrief_remarks_{{ $deployment->id }}" class="form-label fw-semibold">Remarks from Debriefing at HQ</label>
                                        <textarea id="hq_debrief_remarks_{{ $deployment->id }}" name="hq_debrief_remarks" class="form-control" rows="3" placeholder="HQ debrief remarks..."></textarea>
                                    </div>
                                </div>

                                <hr class="my-4">

                                <!-- Previous Missions list (read-only info) -->
                                @php
                                    $previousMissions = \App\Models\Deployment::where('ndrt_officer_id', $deployment->ndrt_officer_id)
                                        ->where('id', '!=', $deployment->id)
                                        ->orderByDesc('start_date')
                                        ->take(5)
                                        ->get();
                                @endphp
                                <div>
                                    <h6 class="fw-semibold mb-2">Previous Missions (latest 5)</h6>
                                    @if($previousMissions->count() > 0)
                                        <div class="table-responsive">
                                            <table class="table table-sm table-hover align-middle">
                                                <thead>
                                                    <tr>
                                                        <th style="width: 15%;">Period</th>
                                                        <th style="width: 20%;">Location</th>
                                                        <th style="width: 35%;">Brief Description</th>
                                                        <th style="width: 30%;">Debrief Remarks</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    @foreach($previousMissions as $pm)
                                                        <tr>
                                                            <td class="text-nowrap">
                                                                {{ optional($pm->start_date)->format('M d, Y') }} - {{ optional($pm->end_date)->format('M d, Y') }}
                                                            </td>
                                                            <td>{{ $pm->mission_location }}</td>
                                                            <td>{{ Str::limit($pm->mission_description, 80) }}</td>
                                                            <td>
                                                                @php
                                                                    $remarks = trim(($pm->branch_debrief_remarks ? ('Branch: ' . $pm->branch_debrief_remarks) : '') . ' ' . ($pm->hq_debrief_remarks ? ('HQ: ' . $pm->hq_debrief_remarks) : ''));
                                                                @endphp
                                                                {{ $remarks ?: '—' }}
                                                            </td>
                                                        </tr>
                                                    @endforeach
                                                </tbody>
                                            </table>
                                        </div>
                                    @else
                                        <p class="text-muted small mb-0">No previous missions found.</p>
                                    @endif
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                                <button type="submit" class="btn btn-success">
                                    <i class="bi bi-check2-circle"></i> Mark as Completed
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        @empty
            <div class="card shadow-sm text-center py-5">
                <div class="card-body">
                    <i class="bi bi-clipboard2-data" style="font-size: 4rem; color: #ccc;"></i>
                    <h4 class="mt-3 mb-2">No deployments yet</h4>
                    <p class="text-muted mb-4">Create your first deployment record</p>
                    <a href="{{ route('deployments.create') }}" class="btn btn-primary">
                        <i class="bi bi-plus-lg"></i> Create Deployment
                    </a>
                </div>
            </div>
        @endforelse
    </div>
</div>
@endsection

