@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">New Line Request Information</h4>
                    </div>
                    <div class="card-body">
                        {{-- Multi Router Yes message at top (from company_information) --}}
                        @if (!empty(trim($multirouterYesMessage ?? '')))
                            <div class="alert alert-warning mb-3 d-none" id="multiRouterTopMessage" role="alert">
                                {!! $multirouterYesMessage !!}
                            </div>
                        @endif
                        {{-- @dd($new_line_request->id) --}}
                        <form action="{{ route('newline.newInformationStore', $new_line_request->id) }}" method="POST"
                            autocomplete="off" data-line-status="{{ $new_line_request->line_status ?? '' }}">
                            @csrf
                            <div class="row">
                                <input type="hidden" name="confirm_line" value="{{ $confirm_line }}">
                                <div class="col-12 mb-2">
                                    <h5 class="text-primary mb-1">Survey</h5>
                                    <small class="text-muted">
                                        Current: {{ $new_line_request->survey_status ?? 'Not Provided' }}
                                        @if (!empty($new_line_request->survey_remarks))
                                            ({{ $new_line_request->survey_remarks }})
                                        @endif
                                    </small>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>Survey Status</label>
                                        <select name="survey" id="surveySelect" class="form-control select2"
                                            autocomplete="off">
                                            <option value="">Select Survey Status</option>
                                            <option value="Coverage"
                                                {{ old('survey', $new_line_request->survey_status ?? '') == 'Coverage' ? 'selected' : '' }}>
                                                Coverage</option>
                                            <option value="Not Coverage"
                                                {{ old('survey', $new_line_request->survey_status ?? '') == 'Not Coverage' ? 'selected' : '' }}>
                                                Not Coverage</option>
                                        </select>
                                    </div>
                                </div>


                                <div class="col-md-9">
                                    <div class="form-group">
                                        <label>Survey Remarks</label>
                                        <textarea name="survey_remarks" id="surveyRemarks" class="form-control @error('survey_remarks') is-invalid @enderror"
                                            rows="2" placeholder="Enter survey remarks..." autocomplete="off">{{ old('survey_remarks', $new_line_request->survey_remarks ?? '') }}</textarea>
                                        @error('survey_remarks')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-12" id="surveyCancelReasonSection">
                                    <div class="form-group">
                                        <label class="d-block mb-2">Cancel Reason</label>
                                        <div class="border rounded bg-light p-3">
                                            <div class="row">
                                                @forelse ($cancelReasons as $index => $cancelReason)
                                                    <div class="col-md-6 col-12 mb-2">
                                                        <div class="form-check">
                                                            <input type="checkbox"
                                                                name="survey_cancel_reasons[{{ $cancelReason->id }}]"
                                                                value="Yes"
                                                                class="form-check-input survey-cancel-reason-cb"
                                                                id="survey-cancel-reason_{{ $cancelReason->id }}"
                                                                autocomplete="off"
                                                                data-survey-cancel-reason-id="{{ $cancelReason->id }}">
                                                            <label class="form-check-label"
                                                                for="survey-cancel-reason_{{ $cancelReason->id }}">
                                                                {{ $cancelReason->reason }}
                                                            </label>
                                                        </div>
                                                    </div>
                                                @empty
                                                    <div class="col-12">
                                                        <p class="text-muted mb-0">No cancel reasons defined.</p>
                                                    </div>
                                                @endforelse
                                            </div>
                                        </div>
                                        <small class="form-text text-muted mt-1">Check for Yes, leave unchecked for
                                            No.</small>

                                    </div>
                                </div>

                                <div class="col-12">
                                    <hr class="my-2">
                                </div>

                                <div class="col-12 mb-2">
                                    <h5 class="text-primary mb-0">Customer Information</h5>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>Full Name</label>
                                        <input type="text" name="name"
                                            class="form-control @error('name') is-invalid @enderror"
                                            value="{{ old('name', $new_line_request->name) }}" autocomplete="off">
                                        @error('name')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <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', $new_line_request->father_name) }}"
                                            autocomplete="off">
                                        @error('father_name')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <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', $new_line_request->mother_name) }}"
                                            autocomplete="off">
                                        @error('mother_name')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>Phone Number</label>
                                        <input type="text" name="phone"
                                            class="form-control @error('phone') is-invalid @enderror"
                                            value="{{ $new_line_request->phone }}" autocomplete="off">
                                        @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', $new_line_request->alt_phone) }}"
                                            autocomplete="off">
                                        @error('alt_phone')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>Billing Date</label>
                                        <select name="billing_date"
                                            class="form-control select2 @error('billing_date') is-invalid @enderror"
                                            autocomplete="off">
                                            <option value="">Select Billing Date</option>
                                            @foreach (explode(',', $confday) as $conf)
                                                <option value="{{ $conf }}"
                                                    {{ old('billing_date', $new_line_request->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', $new_line_request->nid) }}" autocomplete="off">
                                        @error('nid')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-12">
                                    <hr class="my-2">
                                </div>
                                <div class="col-12 mb-2">
                                    <h5 class="text-primary mb-0">Location</h5>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="">Division *</label>
                                        <select name="division" id="division" class="form-control select2"
                                            autocomplete="off" required>
                                            <option value="">Select Division</option>
                                            @foreach ($divisions as $division)
                                                <option value="{{ $division->id }}"
                                                    @if ($new_line_request->division_id == $division->id) selected @endif>
                                                    {{ $division->name }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="">District *</label>
                                        <select name="district" id="district" class="form-control select2"
                                            autocomplete="off" required>
                                            <option value="">Select District</option>
                                            @foreach ($districts as $district)
                                                <option value="{{ $district->id }}"
                                                    @if ($new_line_request->district_id == $district->id) selected @endif>
                                                    {{ $district->name }}</option>
                                            @endforeach

                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="">Upazilla *</label>
                                        <select name="upazilla" id="upazilla" class="form-control select2"
                                            autocomplete="off" required>
                                            <option value="">Select Upazilla</option>
                                            @foreach ($upazilas as $upazila)
                                                <option value="{{ $upazila->id }}"
                                                    {{ $new_line_request->upazila_id == $upazila->id ? 'selected' : '' }}>
                                                    {{ $upazila->name }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="">Thana *</label>
                                        <select name="thana_id" id="thana_id" class="form-control select2"
                                            autocomplete="off" required>
                                            <option value="">Select Thana</option>
                                            @forelse ($thanas as $thana)
                                                <option value="{{ $thana->id }}"
                                                    {{ $new_line_request->thana_id == $thana->id ? 'selected' : '' }}>
                                                    {{ $thana->name }}</option>
                                            @empty
                                                <option value="">No Thana found</option>
                                            @endforelse
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label>Full Address *</label>
                                        <textarea name="address" class="form-control @error('address') is-invalid @enderror" rows="3"
                                            autocomplete="off" required>{{ old('address', $new_line_request->address) }} </textarea>
                                        @error('address')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        {{-- @dd($pops) --}}
                                        <label>POP</label>
                                        <select name="pop_id" id="pop_id"
                                            class="form-control select2 @error('pop_id') is-invalid @enderror"
                                            autocomplete="off">
                                            <option value="">Select POP</option>
                                            @foreach ($pops as $pop)
                                                <option value="{{ $pop->id }}"
                                                    {{ old('pop_id', $new_line_request->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-3">
                                    <div class="form-group">
                                        <label>Package</label>
                                        <select name="package_id" id="package"
                                            class="form-control select2 @error('package_id') is-invalid @enderror"
                                            autocomplete="off">
                                            <option value="">Select Package</option>
                                            @forelse ($packages as $package)
                                                <option value="{{ $package->id }}"
                                                    {{ old('package_id', $old_package_id) == $package->id ? 'selected' : '' }}>
                                                    {{ $package->name }}</option>
                                            @empty
                                                <option value="">No Package found</option>
                                            @endforelse
                                        </select>
                                        @error('package_id')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>



                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>Email</label>
                                        <input type="email" name="email"
                                            class="form-control @error('email') is-invalid @enderror"
                                            value="{{ old('email', $new_line_request->email ?? '') }}"
                                            autocomplete="off">
                                        @error('email')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>Free Days</label>
                                        <input type="number" name="free_days"
                                            class="form-control @error('free_days') is-invalid @enderror"
                                            value="{{ old('free_days', $new_line_request->free_days ?? '') }}"
                                            max="{{ $max_free_day }}" autocomplete="off">
                                        @error('free_days')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>



                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="confirm_activation_date">Confirm Activation Date</label>
                                        <input
                                            type="date"
                                            id="confirm_activation_date"
                                            name="confirm_activation_date"
                                            class="form-control @error('confirm_activation_date') is-invalid @enderror"
                                            value="{{ isset($new_line_request->confirm_activation_date)
                                                ? \Carbon\Carbon::parse($new_line_request->confirm_activation_date)->format('Y-m-d')
                                                : ''
                                            }}"
                                            autocomplete="off"
                                        >
                                        @error('confirm_activation_date')
                                            <span class="invalid-feedback" role="alert">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="follow_up_date">Follow Up Date</label>
                                        <input
                                            type="date"
                                            id="follow_up_date"
                                            name="follow_up_date"
                                            class="form-control @error('follow_up_date') is-invalid @enderror"
                                            value="{{ isset($new_line_request->follow_up_date)
                                                ? \Carbon\Carbon::parse($new_line_request->follow_up_date)->format('Y-m-d')
                                                : ''
                                            }}"
                                            autocomplete="off"
                                        >
                                        @error('follow_up_date')
                                            <span class="invalid-feedback" role="alert">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>OTC Amount</label>
                                        <input type="number" name="otc"
                                            class="form-control @error('otc') is-invalid @enderror"
                                            value="{{ old('otc', $new_line_request->otc ?? '') }}" autocomplete="off">
                                        @error('otc')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>Fiber Meter</label>
                                        <input type="number" name="fiber_meter"
                                            class="form-control @error('fiber_meter') is-invalid @enderror"
                                            value="{{ old('fiber_meter', $new_line_request->fiver_miter ?? '') }}"
                                            autocomplete="off">
                                        @error('fiber_meter')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label>Latitude</label>
                                        <input type="text" name="latitude"
                                            class="form-control @error('latitude') is-invalid @enderror"
                                            value="{{ old('latitude', $new_line_request->lat ?? '') }}"
                                            autocomplete="off">
                                        @error('latitude')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label>Longitude</label>
                                        <input type="text" name="longitude"
                                            class="form-control @error('longitude') is-invalid @enderror"
                                            value="{{ old('longitude', $new_line_request->long ?? '') }}"
                                            autocomplete="off">
                                        @error('longitude')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label>Device Uses</label>
                                        <input type="number" name="device_uses"
                                            class="form-control @error('device_uses') is-invalid @enderror"
                                            value="{{ old('device_uses', $new_line_request->divice_uses ?? '') }}"
                                            autocomplete="off">
                                        @error('device_uses')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>

                                <div class="col-md-12 text-danger d-none" id="multiRouterNotes">
                                    {!! $multirouterYesMessage ?? 'Your select multi router yes.' !!}
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>Multi Router</label>
                                        <select name="multi_router" id="multi_router"
                                            class="form-control @error('multi_router') is-invalid @enderror"
                                            autocomplete="off">
                                            <option value="">Select Option</option>
                                            <option value="No(1)"
                                                {{ old('multi_router', $new_line_request->multi_router ?? '') == 'No(1)' ? 'selected' : '' }}>
                                                No(1)</option>
                                            <option value="Yes"
                                                {{ old('multi_router', $new_line_request->multi_router ?? '') == 'Yes' ? 'selected' : '' }}>
                                                Yes</option>
                                            <option value="Access Point"
                                                {{ old('multi_router', $new_line_request->multi_router ?? '') == 'Access Point' ? 'selected' : '' }}>
                                                Access Point
                                            </option>
                                        </select>
                                        @error('multi_router')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-md-12 {{ old('multi_router', $new_line_request->multi_router ?? '') != 'Yes' ? 'd-none' : '' }}"
                                    id="multiRouterRemarksWrap">
                                    <div class="form-group">
                                        <label>Multi Router Remarks</label>
                                        <textarea name="multi_router_remarks" class="form-control @error('multi_router_remarks') is-invalid @enderror"
                                            rows="3" autocomplete="off">{{ old('multi_router_remarks', $new_line_request->multi_router_remarks ?? '') }}</textarea>
                                        @error('multi_router_remarks')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                {{-- Support done info (read-only): show only when Multi Router is Yes AND support_done is 1 --}}
                                @php
                                    $showSupportDoneInfo =
                                        old('multi_router', $new_line_request->multi_router ?? '') == 'Yes' &&
                                        ($new_line_request->support_done ?? 0) == 1;
                                @endphp
                                <div class="col-md-12 {{ !$showSupportDoneInfo ? 'd-none' : '' }}"
                                    id="supportDoneInfoWrap"
                                    data-support-done="{{ $new_line_request->support_done ?? 0 }}">
                                    <div class="card card-outline card-info">
                                        <div class="card-header py-2">
                                            <span class="card-title">Support Done Information</span>
                                        </div>
                                        <div class="card-body">
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-group mb-0">
                                                        <label class="text-muted">Support Done By</label>
                                                        <p class="form-control-plaintext font-weight-bold">
                                                            {{ optional($new_line_request->supportDoneByUser)->name ?? '—' }}
                                                        </p>
                                                    </div>
                                                </div>
                                                <div class="col-md-12">
                                                    <div class="form-group mb-0">
                                                        <label class="text-muted">Support Remarks</label>
                                                        <p class="form-control-plaintext">
                                                            {{ $new_line_request->support_remarks ?? '—' }}</p>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-12">
                                    <div class="form-group">
                                        <label class="d-block mb-2">Smart Device</label>
                                        <div class="border rounded bg-light p-3">
                                            <div class="row">
                                                @forelse ($smartDevice as $index => $device)
                                                    @php
                                                        $oldStatus = old(
                                                            'smart_devices.' . $device->id,
                                                            $newLineRequestSmartDevices[$device->id] ?? 'No',
                                                        );
                                                        $isYes = $oldStatus === 'Yes' || $oldStatus === true;
                                                    @endphp
                                                    <div class="col-md-3 col-6 mb-2">
                                                        <div class="form-check">
                                                            <input type="checkbox"
                                                                name="smart_devices[{{ $device->id }}]" value="Yes"
                                                                class="form-check-input smart-device-cb"
                                                                id="device_{{ $device->id }}"
                                                                {{ $isYes ? 'checked' : '' }} autocomplete="off"
                                                                data-device-id="{{ $device->id }}">
                                                            <label class="form-check-label"
                                                                for="device_{{ $device->id }}">
                                                                {{ $device->name }}
                                                            </label>
                                                        </div>
                                                    </div>
                                                @empty
                                                    <div class="col-12">
                                                        <p class="text-muted mb-0">No smart devices defined.</p>
                                                    </div>
                                                @endforelse
                                            </div>
                                        </div>
                                        <small class="form-text text-muted mt-1">Check for Yes, leave unchecked for
                                            No.</small>
                                        @error('smart_devices')
                                            <span class="invalid-feedback d-block">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                                {{-- @dd($new_line_request->line_status) --}}
                                {{-- @if ($new_line_request->line_status == 'confirm_request')
                                @else
                                    <div class="col-12 " id="marketingStatus">
                                        <div class="form-group">
                                            <label>Marketing Status</label>
                                            <select name="marketing_status_id" id="marketing_status_id"
                                                class="form-control select2 @error('marketing_status_id') is-invalid @enderror"
                                                autocomplete="off">
                                                <option value="">Select Marketing Status</option>
                                                @foreach ($marketingStatus as $status)
                                                    <option value="{{ $status->id }}"
                                                        {{ old('marketing_status_id', $new_line_request->marketing_done ?? null) == $status->id ? 'selected' : '' }}>
                                                        {{ $status->name }}
                                                    </option>
                                                @endforeach
                                            </select>
                                            @error('marketing_status_id')
                                                <span class="invalid-feedback">{{ $message }}</span>
                                            @enderror
                                        </div>
                                    </div>
                                @endif --}}

                            @can('change-marketing-employee')
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label>Marketing  Employee  </label>
                                        <select name="marketing_employee" id="marketing_employee"
                                            class="form-control select2 @error('marketing_employee') is-invalid @enderror"
                                            autocomplete="off">
                                            <option value="">Select one</option>
                                            @foreach ($marketingEmployee as $employee)
                                                <option value="{{ $employee->id }}"
                                                    {{ old('marketing_employee', $new_line_request->marketing_user_id ?? '') == $employee->id ? 'selected' : '' }}> {{ $employee->name }}
                                                </option>
                                            @endforeach

                                        </select>
                                        @error('marketing_employee')
                                            <span class="invalid-feedback">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>
                            @endcan



                                <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"
                                            autocomplete="off">{{ old('notes', $last_note ?? '') }}</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">Submit 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() {
            // POP Package functionality
            $('#pop_id').on('change', function() {
                let pop_id = $(this).val();
                $.ajax({
                    url: "{{ route('getPackageForPop') }}",
                    type: "GET",
                    data: {
                        id: pop_id
                    },
                    success: function(response) {
                        $('#package').empty();
                        $('#package').append(response);
                    }
                });
            });

            // Multi Router functionality
            var supportDone = parseInt($('#supportDoneInfoWrap').data('support-done') || 0, 10);
            var lineStatus = $('form[data-line-status]').data('line-status') || '';

            function toggleMultiRouterMessage(show) {
                if (show) {
                    $('#multiRouterTopMessage').removeClass('d-none');
                    $('#multiRouterNotes').removeClass('d-none');
                    $('#multiRouterRemarksWrap').removeClass('d-none');
                    if (supportDone === 1) {
                        $('#supportDoneInfoWrap').removeClass('d-none');
                    }
                    $('#marketingStatus').addClass('d-none');
                } else {
                    $('#multiRouterTopMessage').addClass('d-none');
                    $('#multiRouterNotes').addClass('d-none');
                    $('#multiRouterRemarksWrap').addClass('d-none');
                    $('#supportDoneInfoWrap').addClass('d-none');
                    $('#marketingStatus').removeClass('d-none');
                }
            }

            $('#multi_router').on('change', function() {
                toggleMultiRouterMessage($(this).val() === 'Yes');
            });

            // Show message on load if Multi Router is already Yes
            if ($('#multi_router').val() === 'Yes') {
                toggleMultiRouterMessage(true);
            }

            // Survey Cancel Reason Section Toggle
            function toggleSurveyCancelReasonSection() {
                var surveyStatus = $('#surveySelect').val();

                if (surveyStatus === 'Not Coverage') {
                    $('#surveyCancelReasonSection').removeClass('d-none');
                } else {
                    $('#surveyCancelReasonSection').addClass('d-none');
                    // Clear all checkboxes when hiding
                    $('.survey-cancel-reason-cb').prop('checked', false);
                }
            }

            // Hide survey cancel reason section on page load
            $('#surveyCancelReasonSection').addClass('d-none');

            // Check if survey status is already "Not Coverage" on page load
            if ($('#surveySelect').val() === 'Not Coverage') {
                $('#surveyCancelReasonSection').removeClass('d-none');
            }

            // Toggle survey cancel reason section when survey status changes
            $('#surveySelect').on('change', function() {
                toggleSurveyCancelReasonSection();
            });

            // Form validation before submit
            $('form').on('submit', function(e) {
                var surveyStatus = $('#surveySelect').val();

                // If survey status is "Not Coverage", check if at least one cancel reason is selected
                if (surveyStatus === 'Not Coverage') {
                    var checkedReasons = $('.survey-cancel-reason-cb:checked').length;

                    if (checkedReasons === 0) {
                        e.preventDefault();
                        toastr.error(
                            'Please select at least one cancel reason when survey status is "Not Coverage".'
                        );

                        // Scroll to the cancel reason section
                        $('html, body').animate({
                            scrollTop: $('#surveyCancelReasonSection').offset().top - 100
                        }, 500);

                        return false;
                    }
                }
            });
        });
    </script>

    <script>
        $(document).ready(function() {
            $('#division').change(function() {
                var id = $('#division').val();
                $.ajax({
                    url: '{{ route('fetchDistrict') }}',
                    type: 'get',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    success: function(data) {
                        $('#district').html(data);
                    }
                });
            });

            $('#district').change(function() {
                var id = $('#district').val();
                $.ajax({
                    url: '{{ route('fetchUpazila') }}',
                    type: 'get',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    success: function(data) {
                        $('#upazilla').html(data);
                    }
                });
            });

            $('#upazilla').change(function() {
                var id = $('#upazilla').val();
                $.ajax({
                    url: '{{ route('fetchThana') }}',
                    type: 'get',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    success: function(data) {
                        $('#thana_id').html(data);
                    }
                });
            });
        });
    </script>
@endpush
