@extends('layouts.app')

@section('content')
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0 fw-semibold">Edit Loan</h5>
        </div>

        <div class="card-body">
            <form id="loanForm">
                @csrf
                @method('PUT')

                <div class="row g-2">
                    <div class="col-md-4">
                        <label class="form-label">Branch <span class="text-danger">*</span></label>
                        <select name="branch_id" class="form-control">
                            @foreach($branches as $branch)
                                <option value="{{ $branch->id }}"
                                    {{ $loan->branch_id == $branch->id ? 'selected' : '' }}>
                                    {{ $branch->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Code <span class="text-danger">*</span></label>
                        <input type="text" name="code" class="form-control"
                            value="{{ $loan->code }}" required>
                    </div>

                    <div class="col-md-5">
                        <label class="form-label">Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control"
                            value="{{ $loan->name }}" required>
                    </div>

                    <div class="col-md-4">
                        <label class="form-label">Description</label>
                        <textarea name="description" class="form-control">{{ $loan->description }}</textarea>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Min Amount <span class="text-danger">*</span></label>
                        <input type="number" step="0.01" name="min_amount"
                            class="form-control" value="{{ $loan->min_amount }}" required>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Max Amount <span class="text-danger">*</span></label>
                        <input type="number" step="0.01" name="max_amount"
                            class="form-control" value="{{ $loan->max_amount }}" required>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Min Installments <span class="text-danger">*</span></label>
                        <input type="number" name="min_installments"
                            class="form-control" value="{{ $loan->min_installments }}" required>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Max Installments <span class="text-danger">*</span></label>
                        <input type="number" name="max_installments"
                            class="form-control" value="{{ $loan->max_installments }}" required>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Interest Type</label>
                        <select name="interest_type" class="form-control">
                            @foreach(['NONE','FIXED','REDUCING'] as $t)
                                <option value="{{ $t }}" @selected($loan->interest_type === $t)>
                                    {{ $t }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Interest Rate (%) <span class="text-danger">*</span> </label>
                        <input type="number" step="0.01" name="interest_rate"
                            class="form-control" value="{{ $loan->interest_rate }}" required>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Recovery Start</label>
                        <select name="recovery_start" class="form-control">
                            @foreach(['IMMEDIATE','NEXT_MONTH'] as $t)
                                <option value="{{ $t }}" @selected($loan->recovery_start === $t)>
                                    {{ $t }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Recovery Frequency</label>
                        <select name="recovery_frequency" class="form-control">
                            @foreach(['MONTHLY','BIWEEKLY'] as $t)
                                <option value="{{ $t }}" @selected($loan->recovery_frequency === $t)>
                                    {{ $t }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label">Deduction Code</label>
                        <input type="text" name="deduction_code"
                            class="form-control" value="{{ $loan->deduction_code }}">
                    </div>

                    <div class="col-md-2">
                        <label><input type="checkbox" name="allow_partial_recovery"
                            @checked($loan->allow_partial_recovery)> Partial Recovery</label>
                    </div>

                    <div class="col-md-2">
                        <label><input type="checkbox" name="is_salary_advance"
                            @checked($loan->is_salary_advance)> Salary Advance</label>
                    </div>

                    <div class="col-md-2">
                        <label>
                            <input type="checkbox" name="requires_approval" id="requiresApproval"
                                @checked($loan->requires_approval)>
                            Requires Approval
                        </label>
                    </div>

                    <div class="col-md-2">
                        <label class="form-label">Approval Levels</label>
                        <input type="number" name="approval_levels" id="approvalLevels"
                            class="form-control" min="1"
                            value="{{ $loan->approval_levels }}"
                            @disabled(! $loan->requires_approval)>
                    </div>

                    <div class="col-md-2">
                        <label class="form-label">Sort Order</label>
                        <input type="number" name="sort_order"
                            class="form-control" value="{{ $loan->sort_order }}">
                    </div>

                    <div class="col-md-2">
                        <label class="form-label">Effective From</label>
                        <input type="date" name="effective_from"
                            class="form-control"
                            value="{{ $loan->effective_from?->format('Y-m-d') }}">
                    </div>

                    <div class="col-md-2">
                        <label class="form-label">Effective To</label>
                        <input type="date" name="effective_to"
                            class="form-control"
                            value="{{ $loan->effective_to?->format('Y-m-d') }}">
                    </div>

                    <div class="col-md-2 d-flex align-items-end">
                        <label>
                            <input type="checkbox" name="status" @checked($loan->status)> Active
                        </label>
                    </div>
                </div>

                <button type="submit" class="btn btn-primary">Update</button>
                <a href="{{ route('payroll.loans.index') }}" class="btn btn-secondary">Back</a>            
            </form>
        </div>
    </div>
@endsection

@section('scripts')
<script>
$('#requiresApproval').on('change', function () {
    $('#approvalLevels').prop('disabled', !this.checked)
        .val(this.checked ? ($('#approvalLevels').val() || 1) : '');
});

$('#loanForm').on('submit', function(e){
    e.preventDefault();

    $.ajax({
        url: "{{ route('payroll.loans.update', $loan) }}",
        method: 'POST',
        data: $(this).serialize(),
        success(res){
            Swal.fire('Success', res.message, 'success')
            .then(() => window.location.href = "{{ route('payroll.loans.index') }}");
        },
        error(err){
            if (err.status === 422) {
                Swal.fire('Validation Error', err.responseJSON.errors, 'error');
            } else {
                 Swal.fire('Something went wrong','error');
            }
        }
    });
});
</script>
@endsection
