@extends('layout.app')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm">
                <div class="card-header bg-white py-3">
                    <h4 class="card-title m-0 font-weight-bold text-primary">Edit New Line Request</h4>
                </div>
                <div class="card-body">
                    <form action="{{ route('new-line-request.update', $newLineRequest->id) }}" method="POST">
                        @csrf
                        @method('PUT')
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Full Name <span class="text-danger">*</span></label>
                                    <input type="text" name="name" class="form-control @error('name') is-invalid @enderror" value="{{ old('name', $newLineRequest->name) }}" required>
                                    @error('name')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Father's Name</label>
                                    <input type="text" name="father_name" class="form-control @error('father_name') is-invalid @enderror" value="{{ old('father_name', $newLineRequest->father_name) }}">
                                    @error('father_name')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Mother's Name</label>
                                    <input type="text" name="mother_name" class="form-control @error('mother_name') is-invalid @enderror" value="{{ old('mother_name', $newLineRequest->mother_name) }}">
                                    @error('mother_name')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Phone Number <span class="text-danger">*</span></label>
                                    <input type="text" name="phone" class="form-control @error('phone') is-invalid @enderror" value="{{ old('phone', $newLineRequest->phone) }}" required>
                                    @error('phone')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Alternative Phone Number</label>
                                    <input type="text" name="alt_phone" class="form-control @error('alt_phone') is-invalid @enderror" value="{{ old('alt_phone', $newLineRequest->alt_phone) }}">
                                    @error('alt_phone')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label>Billing Date <span class="text-danger">*</span></label>
                                    <select name="billing_date" class="form-control @error('billing_date') is-invalid @enderror" required>
                                        <option value="">Select Billing Date</option>
                                        @foreach (explode(',', $confday) as $conf)
                                            <option value="{{ $conf }}" {{ old('billing_date', $newLineRequest->billing_date) == $conf ? 'selected' : '' }}>{{ $conf }}</option>
                                        @endforeach
                                    </select>
                                    @error('billing_date')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label>NID Number</label>
                                    <input type="text" name="nid" class="form-control @error('nid') is-invalid @enderror" value="{{ old('nid', $newLineRequest->nid) }}">
                                    @error('nid')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>Full Address <span class="text-danger">*</span></label>
                                    <textarea name="address" class="form-control @error('address') is-invalid @enderror" rows="3" required>{{ old('address', $newLineRequest->address) }}</textarea>
                                    @error('address')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>POP <span class="text-danger">*</span></label>
                                    <select name="pop_id" id="pop_id" class="form-control select2 @error('pop_id') is-invalid @enderror" required>
                                        <option value="">Select POP</option>
                                        @foreach($pops as $pop)
                                            <option value="{{ $pop->id }}" {{ old('pop_id', $newLineRequest->pop_id) == $pop->id ? 'selected' : '' }}>
                                                {{ $pop->popname }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('pop_id')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Package <span class="text-danger">*</span></label>
                                    <select name="package_id" id="package" class="form-control select2 @error('package_id') is-invalid @enderror" required>
                                        <option value="">Select Package</option>
                                    </select>
                                    @error('package_id')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label>OTC Amount <span class="text-danger">*</span></label>
                                    <input type="number" name="otc" class="form-control @error('otc') is-invalid @enderror" value="{{ old('otc', $newLineRequest->otc) }}" required>
                                    @error('otc')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label>Fiber Meter <span class="text-danger">*</span></label>
                                    <input type="number" name="fiber_meter" class="form-control @error('fiber_meter') is-invalid @enderror" value="{{ old('fiber_meter', $newLineRequest->fiver_miter) }}" required>
                                    @error('fiber_meter')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label>Latitude</label>
                                    <input type="text" name="latitude" class="form-control @error('latitude') is-invalid @enderror" value="{{ old('latitude', $newLineRequest->lat) }}">
                                    @error('latitude')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label>Longitude</label>
                                    <input type="text" name="longitude" class="form-control @error('longitude') is-invalid @enderror" value="{{ old('longitude', $newLineRequest->long) }}">
                                    @error('longitude')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>

                            <div class="col-12">
                                <div class="form-group">
                                    <label>Additional Notes</label>
                                    <textarea name="notes" class="form-control @error('notes') is-invalid @enderror" rows="3">{{ old('notes', $newLineRequest->remarks) }}</textarea>
                                    @error('notes')
                                        <span class="invalid-feedback">{{ $message }}</span>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-12">
                                <button type="submit" class="btn btn-primary">Update Request</button>
                                <a href="{{ route('new-line-request.index') }}" class="btn btn-secondary">Cancel</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('js')
<script>
    $(document).ready(function() {
        function loadPackages(popId, selectedPackageId) {
            if (!popId) {
                $('#package').empty().append('<option value="">Select Package</option>');
                return;
            }
            $.ajax({
                url: "{{ route('getPackageForPop') }}",
                type: "GET",
                data: { id: popId },
                success: function(response) {
                    $('#package').empty().append(response);
                    if (selectedPackageId) {
                        $('#package').val(String(selectedPackageId));
                    }
                }
            });
        }

        const initialPopId = $('#pop_id').val();
        const selectedPackageId = '{{ old('package_id', $newLineRequest->package_id) }}';
        loadPackages(initialPopId, selectedPackageId);

        $('#pop_id').on('change', function() {
            loadPackages($(this).val(), null);
        });
    });
</script>
@endpush
