@extends('layouts.main')

@section('content')
@php
$today = now()->toDateString();
$hasTodayBalance = \App\Models\OpeningBalance::where('user_id', auth()->id())
    ->where('date', $today)
    ->exists();

if ($hasTodayBalance && !$openingBalance) {
    // Balance exists but not passed - redirect to sales
    header('Location: ' . route('pos.sales'));
    exit;
}
@endphp
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="bi bi-cash-stack me-2"></i>Opening Balance</h5>
                    @if(auth()->user()->hasPermission('opening-balance.view') || auth()->user()->isSuperAdmin() || auth()->user()->isAdmin())
                    <a href="{{ route('opening-balance.report') }}" class="btn btn-sm btn-light">
                        <i class="bi bi-file-earmark-text"></i> Report
                    </a>
                    @endif
                </div>
                <div class="card-body">
                    @if($openingBalance)
                    <div class="alert alert-info">
                        <h4>Today's Opening Balance: Kes {{ number_format($openingBalance->amount, 2) }}</h4>
                        <p class="mb-0">You can update your opening balance below if needed.</p>
                    </div>
                    @else
                    <div class="alert alert-warning">
                        <p class="mb-0">Please enter your opening balance before starting sales.</p>
                    </div>
                    @endif

                    <form method="POST" action="{{ route('opening-balance.store') }}">
                        @csrf
                        <div class="mb-3">
                            <label class="form-label">Opening Balance Amount (Kes)</label>
                            <input type="number" name="opening_balance" class="form-control form-control-lg" 
                                   step="0.01" min="0" 
                                   value="{{ $openingBalance->amount ?? '' }}" 
                                   placeholder="Enter amount" required>
                        </div>
                        <div class="d-grid">
                            <button type="submit" class="btn btn-success btn-lg">
                                <i class="bi bi-check-circle me-2"></i> Save & Start Sales
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
