@extends('layouts.app')

@section('title', __('messages.financial_years'))

@section('content')
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="fw-bold mb-0">Financial Year & Period Lock Settings</h5>
        <span class="text-muted fs-xs">Define Accounting Fiscal Years, Default Active Period, and Backdate Lock Dates</span>
    </div>
    <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#createFyModal">
        <i class="bi bi-plus-lg me-1"></i> Add Financial Year
    </button>
</div>

<div class="card-erp mb-3">
    <div class="table-responsive">
        <table class="table table-dense table-hover mb-0">
            <thead>
                <tr>
                    <th>Year Name</th>
                    <th>Start Date</th>
                    <th>End Date</th>
                    <th>Status</th>
                    <th>Default Active</th>
                    <th>Accounting Lock Date</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($financialYears as $fy)
                    <tr>
                        <td class="fw-bold text-dark">{{ $fy->name }}</td>
                        <td>{{ $fy->start_date->format('Y-m-d') }}</td>
                        <td>{{ $fy->end_date->format('Y-m-d') }}</td>
                        <td>
                            @if($fy->status === 'open')
                                <span class="badge bg-success">Open</span>
                            @else
                                <span class="badge bg-danger">Closed</span>
                            @endif
                        </td>
                        <td>
                            @if($fy->is_default)
                                <span class="badge bg-primary"><i class="bi bi-star-fill me-1"></i> Default Active</span>
                            @else
                                <span class="text-muted fs-xs">No</span>
                            @endif
                        </td>
                        <td>
                            @if($fy->lock_date)
                                <span class="badge bg-warning text-dark"><i class="bi bi-lock me-1"></i> Lock Before: {{ $fy->lock_date->format('Y-m-d') }}</span>
                            @else
                                <span class="text-muted fs-xs">No Lock Date</span>
                            @endif
                        </td>
                        <td class="text-end">
                            <button class="btn btn-dense btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editFyModal{{ $fy->id }}">
                                <i class="bi bi-pencil"></i> Edit
                            </button>
                        </td>
                    </tr>

                    <!-- Edit Modal -->
                    <div class="modal fade" id="editFyModal{{ $fy->id }}" tabindex="-1">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form method="POST" action="{{ route('financial-years.update', $fy->id) }}">
                                    @csrf
                                    @method('PUT')
                                    <div class="modal-header py-2">
                                        <h6 class="modal-title fw-bold">Edit Financial Year: {{ $fy->name }}</h6>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                    </div>
                                    <div class="modal-body">
                                        <div class="mb-3">
                                            <label class="form-label small fw-semibold">Year Name</label>
                                            <input type="text" name="name" class="form-control form-control-sm" value="{{ $fy->name }}" required>
                                        </div>
                                        <div class="row g-2 mb-3">
                                            <div class="col-6">
                                                <label class="form-label small fw-semibold">Start Date</label>
                                                <input type="date" name="start_date" class="form-control form-control-sm" value="{{ $fy->start_date->format('Y-m-d') }}" required>
                                            </div>
                                            <div class="col-6">
                                                <label class="form-label small fw-semibold">End Date</label>
                                                <input type="date" name="end_date" class="form-control form-control-sm" value="{{ $fy->end_date->format('Y-m-d') }}" required>
                                            </div>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label small fw-semibold">Accounting Period Lock Date</label>
                                            <input type="date" name="lock_date" class="form-control form-control-sm" value="{{ $fy->lock_date ? $fy->lock_date->format('Y-m-d') : '' }}">
                                            <span class="text-muted fs-xs">Prevent transactions prior to this date unless user has special permission.</span>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label small fw-semibold">Status</label>
                                            <select name="status" class="form-select form-select-sm">
                                                <option value="open" {{ $fy->status === 'open' ? 'selected' : '' }}>Open</option>
                                                <option value="closed" {{ $fy->status === 'closed' ? 'selected' : '' }}>Closed</option>
                                            </select>
                                        </div>
                                        <div class="form-check">
                                            <input type="checkbox" name="is_default" value="1" class="form-check-input" id="def_{{ $fy->id }}" {{ $fy->is_default ? 'checked' : '' }}>
                                            <label class="form-check-label small fw-semibold" for="def_{{ $fy->id }}">Set as Default Active Financial Year</label>
                                        </div>
                                    </div>
                                    <div class="modal-footer py-2">
                                        <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button>
                                        <button type="submit" class="btn btn-sm btn-primary">Update Year</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                @empty
                    <tr><td colspan="7" class="text-center py-3 text-muted">No financial years configured.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

<!-- Create FY Modal -->
<div class="modal fade" id="createFyModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="POST" action="{{ route('financial-years.store') }}">
                @csrf
                <div class="modal-header py-2">
                    <h6 class="modal-title fw-bold">Create Financial Year</h6>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Year Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control form-control-sm" placeholder="e.g. FY 2026" required>
                    </div>
                    <div class="row g-2 mb-3">
                        <div class="col-6">
                            <label class="form-label small fw-semibold">Start Date <span class="text-danger">*</span></label>
                            <input type="date" name="start_date" class="form-control form-control-sm" value="2026-01-01" required>
                        </div>
                        <div class="col-6">
                            <label class="form-label small fw-semibold">End Date <span class="text-danger">*</span></label>
                            <input type="date" name="end_date" class="form-control form-control-sm" value="2026-12-31" required>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Accounting Period Lock Date</label>
                        <input type="date" name="lock_date" class="form-control form-control-sm">
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Status</label>
                        <select name="status" class="form-select form-select-sm">
                            <option value="open">Open</option>
                            <option value="closed">Closed</option>
                        </select>
                    </div>
                    <div class="form-check">
                        <input type="checkbox" name="is_default" value="1" class="form-check-input" id="defNew" checked>
                        <label class="form-check-label small fw-semibold" for="defNew">Set as Default Active Financial Year</label>
                    </div>
                </div>
                <div class="modal-footer py-2">
                    <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-sm btn-primary">Save Year</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
