@extends('layouts.app')
@section('title',$product->exists?'Edit Loan Product':'New Loan Product')
@section('content')
<x-ui.form-shell
    :title="$product->exists?'Edit Loan Product':'Create Loan Product'"
    description="Define product eligibility, pricing, repayment behavior and control requirements. All financial calculations remain authoritative on the backend."
    :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan Products','url'=>route('loan-products.index')],['label'=>$product->exists?'Edit Loan Product':'Create Loan Product']]"
>
    <form method="POST" action="{{ $product->exists?route('loan-products.update',$product):route('loan-products.store') }}" class="mf-form">
        @csrf
        @if($product->exists)@method('PUT')@endif

        <x-ui.form-section title="Product definition" description="Core product identity, amount limits, term and availability.">
            <x-ui.form-grid>
                <div class="mf-col-5"><label class="mf-field-label">Product name</label><input class="mf-input" name="name" value="{{ old('name',$product->name) }}" required></div>
                <div class="mf-col-2"><label class="mf-field-label">Minimum amount</label><input type="number" step="0.01" min="0.01" class="mf-input" name="minimum_amount" value="{{ old('minimum_amount',$product->minimum_amount) }}" required></div>
                <div class="mf-col-2"><label class="mf-field-label">Maximum amount</label><input type="number" step="0.01" min="0.01" class="mf-input" name="maximum_amount" value="{{ old('maximum_amount',$product->maximum_amount) }}" required></div>
                <div class="mf-col-3"><label class="mf-field-label">Status</label><select name="status" class="mf-input"><option value="active" @selected(old('status',$product->status ?: 'active')==='active')>Active</option><option value="inactive" @selected(old('status',$product->status)==='inactive')>Inactive</option></select></div>
                <div class="mf-col-2"><label class="mf-field-label">Duration</label><input type="number" min="1" class="mf-input" name="duration" value="{{ old('duration',$product->duration) }}" required></div>
                <div class="mf-col-2"><label class="mf-field-label">Duration unit</label><select class="mf-input" name="duration_unit">@foreach(['days','weeks','months'] as $v)<option value="{{ $v }}" @selected(old('duration_unit',$product->duration_unit)===$v)>{{ ucfirst($v) }}</option>@endforeach</select></div>
                <div class="mf-col-3"><label class="mf-field-label">Repayment frequency</label><select class="mf-input" name="repayment_frequency">@foreach(['daily'=>'Daily','weekly'=>'Weekly','biweekly'=>'Every two weeks','monthly'=>'Monthly','lump_sum'=>'Lump sum','custom'=>'Custom dates'] as $v=>$l)<option value="{{ $v }}" @selected(old('repayment_frequency',$product->repayment_frequency)===$v)>{{ $l }}</option>@endforeach</select></div>
                <div class="mf-col-2"><label class="mf-field-label">Grace period</label><input type="number" min="0" class="mf-input" name="grace_period_days" value="{{ old('grace_period_days',$product->grace_period_days) }}"><p class="mf-field-help">Days</p></div>
                <div class="mf-col-3"><label class="mf-field-label">Requirements</label><div class="flex h-10 items-center gap-4"><label class="inline-flex items-center gap-2 text-sm"><input type="checkbox" name="collateral_required" value="1" @checked(old('collateral_required',$product->collateral_required))> Collateral</label><label class="inline-flex items-center gap-2 text-sm"><input type="checkbox" name="guarantor_required" value="1" @checked(old('guarantor_required',$product->guarantor_required))> Guarantor</label></div></div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-section title="Scheduled payment deadline" description="Use this only for products where every installment must be paid before a specific time on its due date.">
            <x-ui.form-grid>
                <div class="mf-col-7 flex items-end">
                    <label class="mf-option w-full">
                        <input type="checkbox" name="payment_deadline_enabled" value="1" @checked(old('payment_deadline_enabled',$product->payment_deadline_enabled))>
                        <span><strong>Apply a payment-time deadline to this product</strong><small class="block text-muted-foreground">The company-wide deadline penalty feature must also be enabled in Business Settings. Approved excuses prevent both deadline and missed-day penalties for the affected installment.</small></span>
                    </label>
                </div>
                <div class="mf-col-3"><label class="mf-field-label">Daily deadline time</label><input type="time" class="mf-input" name="payment_deadline_time" value="{{ old('payment_deadline_time',$product->payment_deadline_time ? substr((string)$product->payment_deadline_time,0,5) : '17:00') }}">@error('payment_deadline_time')<p class="mf-field-error">{{ $message }}</p>@enderror<p class="mf-field-help">Example: 17:00 means payment after 5:00 PM is late.</p></div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-section title="Interest configuration" description="Select one calculation method and define the corresponding pricing value.">
            <x-ui.form-grid>
                <div class="mf-col-4"><label class="mf-field-label">Calculation method</label><select class="mf-input" name="interest_calculation_method">@foreach(['flat'=>'Flat interest','reducing_balance'=>'Reducing balance','fixed_amount'=>'Fixed interest amount'] as $v=>$l)<option value="{{ $v }}" @selected(old('interest_calculation_method',$product->interest_calculation_method)===$v)>{{ $l }}</option>@endforeach</select></div>
                <div class="mf-col-2"><label class="mf-field-label">Interest rate (%)</label><input type="number" step="0.000001" min="0" max="1000" class="mf-input" name="interest_rate" value="{{ old('interest_rate',$product->interest_rate) }}"></div>
                <div class="mf-col-3"><label class="mf-field-label">Fixed interest amount</label><input type="number" step="0.01" min="0" class="mf-input" name="fixed_interest_amount" value="{{ old('fixed_interest_amount',$product->fixed_interest_amount) }}"><p class="mf-field-help">Used only when Fixed Interest Amount is selected.</p></div>
                <div class="mf-col-3"><div class="rounded-md border border-border bg-muted/30 p-3 text-xs leading-5 text-muted-foreground">The schedule engine calculates and reconciles principal, interest and fees using fixed-precision arithmetic.</div></div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-section title="Fees & late-payment policy" description="Configure charges separately so product pricing remains transparent and auditable.">
            <x-ui.form-grid>
                <div class="mf-col-2"><label class="mf-field-label">Processing fee type</label><select class="mf-input" name="processing_fee_type"><option value="fixed" @selected(old('processing_fee_type',$product->processing_fee_type)==='fixed')>Fixed</option><option value="percentage" @selected(old('processing_fee_type',$product->processing_fee_type)==='percentage')>Percentage</option></select></div>
                <div class="mf-col-2"><label class="mf-field-label">Processing fee value</label><input type="number" step="0.000001" min="0" class="mf-input" name="processing_fee_value" value="{{ old('processing_fee_value',$product->processing_fee_value) }}"></div>
                <div class="mf-col-4"><label class="mf-field-label">How processing fee is paid</label><select class="mf-input" name="processing_fee_payment_mode" required>@foreach(\App\Models\LoanProduct::PROCESSING_FEE_PAYMENT_MODES as $value=>$label)<option value="{{ $value }}" @selected(old('processing_fee_payment_mode',$product->processing_fee_payment_mode ?: 'financed')===$value)>{{ $label }}</option>@endforeach</select><p class="mf-field-help">Cash: customer pays before release. Deducted: customer receives principal minus fee. Included: fee is spread across loan repayments.</p>@error('processing_fee_payment_mode')<p class="mf-field-error">{{ $message }}</p>@enderror</div>
                <div class="mf-col-2"><label class="mf-field-label">Insurance fee type</label><select class="mf-input" name="insurance_fee_type"><option value="fixed" @selected(old('insurance_fee_type',$product->insurance_fee_type)==='fixed')>Fixed</option><option value="percentage" @selected(old('insurance_fee_type',$product->insurance_fee_type)==='percentage')>Percentage</option></select></div>
                <div class="mf-col-2"><label class="mf-field-label">Insurance fee value</label><input type="number" step="0.000001" min="0" class="mf-input" name="insurance_fee_value" value="{{ old('insurance_fee_value',$product->insurance_fee_value) }}"></div>
                <div class="mf-col-2"><label class="mf-field-label">Other charges</label><input type="number" step="0.01" min="0" class="mf-input" name="other_charges" value="{{ old('other_charges',$product->other_charges) }}"></div>
                <div class="mf-col-4"><label class="mf-field-label">Late penalty type</label><select class="mf-input" name="late_payment_penalty_type">@foreach(['none'=>'None','fixed'=>'Fixed','percentage'=>'Percentage','daily_percentage'=>'Daily percentage','weekly_percentage'=>'Weekly percentage'] as $v=>$l)<option value="{{ $v }}" @selected(old('late_payment_penalty_type',$product->late_payment_penalty_type)===$v)>{{ $l }}</option>@endforeach</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="late_payment_penalty_value" value="{{ old('late_payment_penalty_value',$product->late_payment_penalty_value) }}"></div>
            </x-ui.form-grid>
        </x-ui.form-section>

        <x-ui.form-actions>
            <a class="mf-btn" href="{{ route('loan-products.index') }}">Cancel</a>
            <button class="mf-btn mf-btn-primary">Save product</button>
        </x-ui.form-actions>
    </form>
</x-ui.form-shell>
@endsection
