@extends('layouts.app')
@section('title',$account->exists?'Edit Financial Account':'New Financial Account')
@section('content')
<x-ui.form-shell :title="$account->exists?'Edit Financial Account':'New Financial Account'" description="Create and maintain a real branch financial account. Opening balances are posted to the accounting ledger and cannot be edited later." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Financial Accounts','url'=>route('accounts.index')],['label'=>$account->exists?'Edit':'New']]">
<form method="POST" action="{{ $account->exists?route('accounts.update',$account):route('accounts.store') }}" class="mf-form">@csrf @if($account->exists)@method('PUT')@endif
<x-ui.form-section title="Account Identity" description="Identify the account and the branch that owns it."><x-ui.form-grid>
@if(!$account->exists)<div class="mf-col-4"><label class="mf-field-label">Branch</label><select class="mf-input" name="branch_id" required><option value="">Select branch</option>@foreach($branches as $branch)<option value="{{ $branch->id }}" @selected((int)old('branch_id',$account->branch_id)===$branch->id)>{{ $branch->name }}</option>@endforeach</select>@error('branch_id')<p class="mf-field-error">{{ $message }}</p>@enderror</div>@else<div class="mf-col-4"><label class="mf-field-label">Branch</label><input class="mf-input" value="{{ $account->branch->name }}" readonly></div>@endif
<div class="mf-col-4"><label class="mf-field-label">Account name</label><input class="mf-input" name="name" value="{{ old('name',$account->name) }}" required placeholder="e.g. Main Cash Till">@error('name')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
<div class="mf-col-4"><label class="mf-field-label">Account type</label><select class="mf-input" name="account_type" required>@foreach(\App\Models\FinancialAccount::TYPES as $type)<option value="{{ $type }}" @selected(old('account_type',$account->account_type)===$type)>{{ str($type)->replace('_',' ')->title() }}</option>@endforeach</select></div>
</x-ui.form-grid></x-ui.form-section>
<x-ui.form-section title="Balance & Status" description="Opening balance is established once when the account is created. Later changes must come from posted business transactions."><x-ui.form-grid>
@if(!$account->exists)<div class="mf-col-4"><label class="mf-field-label">Opening balance</label><input type="number" min="0" step="0.01" class="mf-input" name="opening_balance" value="{{ old('opening_balance',$account->opening_balance ?? '0.00') }}" required>@error('opening_balance')<p class="mf-field-error">{{ $message }}</p>@enderror</div>@else<div class="mf-col-3"><label class="mf-field-label">Opening balance</label><input class="mf-input" value="{{ number_format((float)$account->opening_balance,2) }}" readonly></div><div class="mf-col-3"><label class="mf-field-label">Current balance</label><input class="mf-input" value="{{ number_format((float)$account->current_balance,2) }}" readonly></div>@endif
<div class="mf-col-3"><label class="mf-field-label">Status</label><select class="mf-input" name="status" required>@foreach(\App\Models\FinancialAccount::STATUSES as $status)<option value="{{ $status }}" @selected(old('status',$account->status)===$status)>{{ ucfirst($status) }}</option>@endforeach</select></div>
<div class="mf-col-{{ $account->exists?'6':'5' }}"><label class="mf-field-label">Account / reference details</label><input class="mf-input" name="reference_details" value="{{ old('reference_details',$account->reference_details) }}" placeholder="Bank account, mobile wallet number, till identifier…"></div>
</x-ui.form-grid></x-ui.form-section>
<x-ui.form-actions><a class="mf-btn" href="{{ $account->exists?route('accounts.show',$account):route('accounts.index') }}">Cancel</a><button class="mf-btn mf-btn-primary">{{ $account->exists?'Save Account':'Create Account' }}</button></x-ui.form-actions>
</form></x-ui.form-shell>
@endsection
