@extends('layouts.app')
@section('title', 'Business Settings')
@section('content')
<x-ui.form-shell
    title="Business Settings"
    description="Manage company identity, contact information, financial defaults and official branding from one structured configuration workspace."
    :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Settings'],['label'=>'Business Settings']]"
>
    <form method="POST" action="{{ route('admin.business-settings.update') }}" enctype="multipart/form-data" class="mf-form">
        @csrf
        @method('PUT')

        <x-ui.form-section title="Legal identity" description="Official business identification used on contracts, statements and regulatory documents.">
            <x-ui.form-grid>
                <div class="mf-col-5"><label class="mf-field-label">Business name</label><input class="mf-input" name="name" value="{{ old('name', $business->name) }}" required>@error('name')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
                <div class="mf-col-3"><label class="mf-field-label">Registration number</label><input class="mf-input" name="registration_number" value="{{ old('registration_number', $business->registration_number) }}">@error('registration_number')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
                <div class="mf-col-2"><label class="mf-field-label">TIN</label><input class="mf-input" name="tin" value="{{ old('tin', $business->tin) }}">@error('tin')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
                <div class="mf-col-2"><label class="mf-field-label">Country</label><input class="mf-input" name="country" value="{{ old('country', $business->country) }}" required>@error('country')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
                <div class="mf-col-12"><label class="mf-field-label">Business licence details</label><textarea class="mf-input" name="licence_details">{{ old('licence_details',$business->licence_details) }}</textarea></div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-section title="Contact & address" description="Public contact channels and registered business location.">
            <x-ui.form-grid>
                <div class="mf-col-5"><label class="mf-field-label">Physical address</label><input class="mf-input" name="address" value="{{ old('address', $business->address) }}"></div>
                <div class="mf-col-3"><label class="mf-field-label">Postal address</label><input class="mf-input" name="postal_address" value="{{ old('postal_address', $business->postal_address) }}"></div>
                <div class="mf-col-2"><label class="mf-field-label">Region</label><input class="mf-input" name="region" value="{{ old('region', $business->region) }}"></div>
                <div class="mf-col-2"><label class="mf-field-label">District</label><input class="mf-input" name="district" value="{{ old('district', $business->district) }}"></div>
                <div class="mf-col-3"><label class="mf-field-label">Primary phone</label><input class="mf-input" name="phone" value="{{ old('phone', $business->phone) }}"></div>
                <div class="mf-col-3"><label class="mf-field-label">Alternative phone</label><input class="mf-input" name="phone_alt" value="{{ old('phone_alt', $business->phone_alt) }}"></div>
                <div class="mf-col-3"><label class="mf-field-label">Email</label><input type="email" class="mf-input" name="email" value="{{ old('email', $business->email) }}"></div>
                <div class="mf-col-3"><label class="mf-field-label">Website</label><input class="mf-input" name="website" value="{{ old('website', $business->website) }}"></div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-section title="Financial defaults" description="Organization-wide defaults used when presenting money, periods and initial loan configuration.">
            <x-ui.form-grid>
                <div class="mf-col-2"><label class="mf-field-label">Currency code</label><input class="mf-input" name="currency" value="{{ old('currency', $business->currency) }}" required></div>
                <div class="mf-col-2"><label class="mf-field-label">Currency symbol</label><input class="mf-input" name="currency_symbol" value="{{ old('currency_symbol', $business->currency_symbol) }}" required></div>
                <div class="mf-col-2"><label class="mf-field-label">Number format</label><select class="mf-input" name="number_format">@foreach(['1,234.56','1.234,56','1234.56'] as $v)<option value="{{ $v }}" @selected(old('number_format',$business->number_format)===$v)>{{ $v }}</option>@endforeach</select></div>
                <div class="mf-col-3"><label class="mf-field-label">Financial year starts</label><select class="mf-input" name="financial_year_start_month">@for($m=1;$m<=12;$m++)<option value="{{ $m }}" @selected((int)old('financial_year_start_month',$business->financial_year_start_month)===$m)>{{ DateTime::createFromFormat('!m',$m)->format('F') }}</option>@endfor</select></div>
                <div class="mf-col-3"><label class="mf-field-label">Accounting period</label><select class="mf-input" name="accounting_period">@foreach(['monthly','quarterly','yearly'] as $v)<option value="{{ $v }}" @selected(old('accounting_period',$business->accounting_period)===$v)>{{ ucfirst($v) }}</option>@endforeach</select></div>
                <div class="mf-col-4"><label class="mf-field-label">Default interest method</label><select class="mf-input" name="default_interest_method">@foreach(['flat'=>'Flat interest','reducing_balance'=>'Reducing balance','fixed_amount'=>'Fixed interest amount'] as $v=>$label)<option value="{{ $v }}" @selected(old('default_interest_method',$business->default_interest_method)===$v)>{{ $label }}</option>@endforeach</select></div>
                <div class="mf-col-3"><label class="mf-field-label">Default penalty type</label><select class="mf-input" name="default_penalty_settings[type]"><option value="fixed" @selected(data_get($business->default_penalty_settings,'type')==='fixed')>Fixed amount</option><option value="percentage" @selected(data_get($business->default_penalty_settings,'type')==='percentage')>Percentage</option></select></div>
                <div class="mf-col-2"><label class="mf-field-label">Penalty value</label><input type="number" step="0.01" min="0" class="mf-input" name="default_penalty_settings[value]" value="{{ old('default_penalty_settings.value', data_get($business->default_penalty_settings,'value')) }}"></div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-section title="Scheduled payment deadline penalties" description="Optional special rules for products that require each installment to be paid before a specific clock time on its due date.">
            <div class="mb-4">
                <input type="hidden" name="payment_deadline_penalties_enabled" value="0">
                <label class="mf-option">
                    <input type="checkbox" name="payment_deadline_penalties_enabled" value="1" @checked(old('payment_deadline_penalties_enabled',$business->payment_deadline_penalties_enabled))>
                    <span><strong>Enable payment-time deadline penalties</strong><small class="block text-muted-foreground">This is the master switch. Only loan products that also have a payment deadline enabled will use these rules.</small></span>
                </label>
            </div>
            <x-ui.form-grid>
                <div class="mf-col-3"><label class="mf-field-label">Late same-day penalty</label><select class="mf-input" name="deadline_late_penalty_type"><option value="fixed" @selected(old('deadline_late_penalty_type',$business->deadline_late_penalty_type ?: 'fixed')==='fixed')>Fixed amount</option><option value="percentage" @selected(old('deadline_late_penalty_type',$business->deadline_late_penalty_type)==='percentage')>Percentage of unpaid scheduled amount</option></select></div>
                <div class="mf-col-2"><label class="mf-field-label">Late penalty value</label><input type="number" step="0.000001" min="0" class="mf-input" name="deadline_late_penalty_value" value="{{ old('deadline_late_penalty_value',$business->deadline_late_penalty_value ?? 0) }}">@error('deadline_late_penalty_value')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
                <div class="mf-col-3"><label class="mf-field-label">Late penalty collection</label><select class="mf-input" name="deadline_late_penalty_settlement"><option value="add_to_loan" @selected(old('deadline_late_penalty_settlement',$business->deadline_late_penalty_settlement ?: 'add_to_loan')==='add_to_loan')>Add to loan balance</option><option value="next_payment" @selected(old('deadline_late_penalty_settlement',$business->deadline_late_penalty_settlement)==='next_payment')>Collect when customer pays</option></select></div>
                <div class="mf-col-4"><div class="rounded-md border p-3 text-xs text-muted-foreground">Example: deadline 17:00. A payment recorded at 18:00 triggers this penalty once for that installment, unless an approved excuse is recorded.</div></div>

                <div class="mf-col-3"><label class="mf-field-label">Missed-day penalty</label><select class="mf-input" name="missed_schedule_penalty_type"><option value="fixed" @selected(old('missed_schedule_penalty_type',$business->missed_schedule_penalty_type ?: 'fixed')==='fixed')>Fixed amount</option><option value="percentage" @selected(old('missed_schedule_penalty_type',$business->missed_schedule_penalty_type)==='percentage')>Percentage of unpaid scheduled amount</option></select></div>
                <div class="mf-col-2"><label class="mf-field-label">Missed-day value</label><input type="number" step="0.000001" min="0" class="mf-input" name="missed_schedule_penalty_value" value="{{ old('missed_schedule_penalty_value',$business->missed_schedule_penalty_value ?? 0) }}">@error('missed_schedule_penalty_value')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
                <div class="mf-col-3"><label class="mf-field-label">Missed-day collection</label><select class="mf-input" name="missed_schedule_penalty_settlement"><option value="add_to_loan" @selected(old('missed_schedule_penalty_settlement',$business->missed_schedule_penalty_settlement ?: 'add_to_loan')==='add_to_loan')>Add to loan balance</option><option value="next_payment" @selected(old('missed_schedule_penalty_settlement',$business->missed_schedule_penalty_settlement)==='next_payment')>Collect when customer pays</option></select></div>
                <div class="mf-col-4"><div class="rounded-md border p-3 text-xs text-muted-foreground">If any scheduled amount remains unpaid after the due date ends, this penalty is charged once for that installment. A percentage is calculated from the unpaid scheduled amount at that point.</div></div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-section title="Loan approval controls" description="Configure who may approve applications and when a second reviewer or manager is required.">
            <x-ui.form-grid>
                <div class="mf-col-4">
                    <label class="mf-field-label">Approval mode</label>
                    <select class="mf-input" name="loan_approval_mode" required>
                        <option value="approval_required" @selected(old('loan_approval_mode',$business->loan_approval_mode ?? 'approval_required')==='approval_required')>Approval required</option>
                        <option value="auto_authorized" @selected(old('loan_approval_mode',$business->loan_approval_mode)==='auto_authorized')>Auto approval for authorized users</option>
                    </select>
                    <p class="mt-1 text-xs text-muted-foreground">Auto mode only applies to staff explicitly granted the auto-approval permission.</p>
                </div>
                <div class="mf-col-4">
                    <label class="mf-field-label">Manager approval threshold</label>
                    <input type="number" step="0.01" min="0" class="mf-input" name="manager_approval_threshold" value="{{ old('manager_approval_threshold',$business->manager_approval_threshold) }}" placeholder="Leave empty for no threshold">
                    <p class="mt-1 text-xs text-muted-foreground">Loans above this principal require a manager-authorized approver.</p>
                </div>
                <div class="mf-col-4 flex items-end">
                    <label class="mf-option w-full">
                        <input type="checkbox" name="maker_checker_enabled" value="1" @checked(old('maker_checker_enabled',$business->maker_checker_enabled))>
                        <span><strong>Enable maker-checker</strong><small class="block text-muted-foreground">The employee who created a loan cannot approve the same application.</small></span>
                    </label>
                </div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-section title="Loan top-up eligibility" description="Set the minimum percentage of the original scheduled obligation that a customer must repay before requesting a top-up.">
            <x-ui.form-grid>
                <div class="mf-col-4">
                    <label class="mf-field-label">Minimum repaid percentage</label>
                    <input type="number" list="top-up-thresholds" step="0.01" min="1" max="100" class="mf-input" name="top_up_eligibility_percentage" value="{{ old('top_up_eligibility_percentage',$business->top_up_eligibility_percentage ?? '70.00') }}" required>
                    <datalist id="top-up-thresholds"><option value="50"><option value="70"><option value="80"></datalist>
                    @error('top_up_eligibility_percentage')<p class="mf-field-error">{{ $message }}</p>@enderror
                    <p class="mt-1 text-xs text-muted-foreground">Common policies are 50%, 70% or 80%. Any custom percentage from 1–100 is allowed.</p>
                </div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-section title="Loan contract control" description="Control whether a signed customer contract will be mandatory before a future disbursement can be posted.">
            <label class="mf-option">
                <input type="checkbox" name="require_signed_contract_before_disbursement" value="1" @checked(old('require_signed_contract_before_disbursement',$business->require_signed_contract_before_disbursement))>
                <span><strong>Require signed contract before disbursement</strong><small class="block text-muted-foreground">When enabled, the disbursement phase must reject any loan without an uploaded signed contract.</small></span>
            </label>
        </x-ui.form-section>

        <x-ui.form-section title="Branding assets" description="Official visual assets used across printed and digital documents.">
            <x-ui.form-grid>
                <div class="mf-col-6"><label class="mf-field-label">Business logo</label><input type="file" name="logo" accept="image/png,image/jpeg,image/webp" class="mf-input"></div>
                <div class="mf-col-6"><label class="mf-field-label">Company stamp</label><input type="file" name="stamp" accept="image/png,image/jpeg,image/webp" class="mf-input"></div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-actions><button class="mf-btn mf-btn-primary" type="submit">Save settings</button></x-ui.form-actions>
    </form>
</x-ui.form-shell>
@endsection