@extends('layouts.app')
@section('title', 'Fiscal Years')
@section('crumbs', '<b>Fiscal Years</b> · period control')
@section('content')
<div class="card">
  <div class="card-h"><b>📅 Fiscal Years</b>
    <span class="muted small">Closing a year blocks journals, invoices, payments, payroll & bank entries dated inside it.</span>
    <span style="margin-left:auto;display:flex;gap:8px">
      @if(perm('settings.edit'))
      <a class="btn btn-sm" href="{{ crud_url('fiscal-years','create') }}">＋ New Year</a>
      @endif
    </span>
  </div>
  <div class="tbl-wrap"><table class="tbl">
    <thead><tr><th>Year</th><th>Start</th><th>End</th><th>Current</th><th>Status</th><th style="width:230px"></th></tr></thead>
    <tbody>
    @foreach($years as $y)
      <tr>
        <td><b>{{ $y->name }}</b></td>
        <td>{{ substr($y->start_date,0,10) }}</td>
        <td>{{ substr($y->end_date,0,10) }}</td>
        <td>{!! $y->is_current ? '<span class="bdg b-info">Current</span>' : '<span class="muted">—</span>' !!}</td>
        <td>{!! $y->is_closed ? '<span class="bdg b-err">🔒 Closed</span>' : '<span class="bdg b-ok">Open</span>' !!}</td>
        <td style="white-space:nowrap;text-align:right">
          @if(perm('settings.edit'))
            <a class="btn btn-sm btn-ghost" href="{{ crud_url('fiscal-years','edit',$y->id) }}">Edit dates</a>
            @if($y->is_closed)
              <form method="POST" action="{{ route('fiscal-years.reopen',$y->id) }}" data-confirm="Reopen {{ $y->name }}? Postings will be allowed again." style="display:inline">@csrf<button class="btn btn-sm btn-ok">🔓 Reopen</button></form>
            @else
              <form method="POST" action="{{ route('fiscal-years.close',$y->id) }}" data-confirm="Close {{ $y->name }}? All postings dated inside it will be blocked." style="display:inline">@csrf<button class="btn btn-sm btn-danger">🔒 Close</button></form>
            @endif
          @endif
        </td>
      </tr>
    @endforeach
    </tbody>
  </table></div>
</div>
@endsection
