<?php if (isset($component)) { $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54 = $attributes; } ?>
<?php $component = App\View\Components\AppLayout::resolve([] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('app-layout'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\App\View\Components\AppLayout::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
     <?php $__env->slot('header', null, []); ?> 
        <div class="d-flex align-items-center justify-content-between">
            <div>
                <h2 class="h4 mb-0"><?php echo e(isset($customer) ? 'Edit Customer' : 'Add Customer'); ?></h2>
            </div>
            <a href="<?php echo e(route('admin.customers.index')); ?>" class="btn btn-secondary">Back to Customers</a>
        </div>
     <?php $__env->endSlot(); ?>

    <div class="card shadow-sm border-0">
        <div class="card-body">
            <form id="customerCreateForm" action="<?php echo e(isset($customer) ? route('admin.customers.update', $customer) : route('admin.customers.store')); ?>" method="POST">
                <?php echo csrf_field(); ?>
                <?php if(isset($customer)): ?> <?php echo method_field('PUT'); ?> <?php endif; ?>
                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label">Customer ID</label>
                        <input type="text" name="customer_code" id="customer_code" class="form-control" required value="<?php echo e(old('customer_code', $customer->customer_code ?? $defaultCustomerCode)); ?>">
                        <small class="text-muted">Unique customer code, e.g. DEV0001. You can change this value before saving.</small>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Mikrotik Server</label>
                        <select name="mikrotik_id" id="mikrotik_id" class="form-select" <?php echo e(isset($customer) ? 'disabled' : ''); ?>>
                            <option value="">None</option>
                            <?php $__currentLoopData = $mikrotiks; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $mikrotik): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($mikrotik->id); ?>" <?php echo e(old('mikrotik_id', $customer->mikrotik_id ?? null) == $mikrotik->id ? 'selected' : ''); ?>><?php echo e($mikrotik->name); ?> (<?php echo e($mikrotik->ip_address); ?>)</option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <?php if(isset($customer)): ?>
                            <input type="hidden" name="mikrotik_id" value="<?php echo e($customer->mikrotik_id); ?>">
                            <small class="text-muted">MikroTik cannot be changed after customer creation.</small>
                        <?php endif; ?>
                    </div>
                    <div class="col-md-6 mikrotik-credential-field d-none">
                        <label class="form-label">PPPoE Username</label>
                        <input type="text" name="ip_address" id="ip_address" class="form-control" maxlength="191" placeholder="e.g. ruman@pppoe" value="<?php echo e(old('ip_address', $customer->ip_address ?? '')); ?>" <?php echo e(isset($customer) ? 'readonly' : ''); ?>>
                        <small class="text-muted">Must be unique. Spaces are not allowed.</small>
                    </div>
                    <div class="col-md-6 mikrotik-credential-field d-none">
                        <label class="form-label">PPPoE Password</label>
                        <input type="text" name="queue_password" id="queue_password" class="form-control" value="<?php echo e(old('queue_password', '')); ?>" placeholder="<?php echo e(isset($customer) ? 'Leave blank to keep current password' : ''); ?>">
                        <small class="text-muted"><?php echo e(isset($customer) ? 'Enter a value only when changing the MikroTik password.' : 'MikroTik secret/customer connection password.'); ?></small>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Full Name</label>
                        <input type="text" name="name" id="name" class="form-control" required value="<?php echo e(old('name', $customer->name ?? '')); ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" id="email" class="form-control" value="<?php echo e(old('email', $customer->email ?? '')); ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Contact Phone</label>
                        <input type="text" name="contact_phone" id="contact_phone" class="form-control" required value="<?php echo e(old('contact_phone', $customer->contact_phone ?? '')); ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Secondary Phone</label>
                        <input type="text" name="secondary_phone" id="secondary_phone" class="form-control" value="<?php echo e(old('secondary_phone', $customer->secondary_phone ?? '')); ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Package</label>
                        <select name="package_id" id="package_id" class="form-select">
                            <option value="">Standard</option>
                            <?php $__currentLoopData = $packages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $package): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($package->id); ?>" data-price="<?php echo e($package->price); ?>" data-mikrotik="<?php echo e($package->mikrotik_server_id); ?>" <?php echo e(old('package_id', $customer->package_id ?? null) == $package->id ? 'selected' : ''); ?>><?php echo e($package->name); ?> (<?php echo e($package->net_speed); ?>)</option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Monthly Bill</label>
                        <input type="number" name="monthly_bill" id="monthly_bill" class="form-control" min="0" step="0.01" required value="<?php echo e(old('monthly_bill', $customer->monthly_bill ?? '')); ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Status</label>
                        <select name="status" id="status" class="form-select" required>
                            <?php $__currentLoopData = [1 => 'Active', 0 => 'Inactive', 2 => 'Disconnected', 3 => 'Free']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($value); ?>" <?php echo e((string) old('status', $customer->status ?? 1) === (string) $value ? 'selected' : ''); ?>><?php echo e($label); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Connection Type</label>
                        <input type="text" name="connection_type" id="connection_type" class="form-control" value="<?php echo e(old('connection_type', $customer->connection_type ?? '')); ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label" for="zone">Zone</label>
                        <?php $selectedZone = old('zone', $customer->zone ?? ''); ?>
                        <select name="zone" id="zone" class="form-select">
                            <option value="">Select Zone</option>
                            <?php $__currentLoopData = $zones; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $zone): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($zone->name); ?>" data-zone-id="<?php echo e($zone->id); ?>" <?php echo e($selectedZone === $zone->name ? 'selected' : ''); ?>><?php echo e($zone->name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php if($selectedZone && ! $zones->contains('name', $selectedZone)): ?>
                                <option value="<?php echo e($selectedZone); ?>" data-zone-id="saved" selected><?php echo e($selectedZone); ?> (Saved)</option>
                            <?php endif; ?>
                        </select>
                        <?php if($zones->isEmpty()): ?>
                            <small class="text-warning">No active zone found. Create one from the Zones menu first.</small>
                        <?php endif; ?>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label" for="sub_zone">Sub Zone</label>
                        <?php $selectedSubZone = old('sub_zone', $customer->sub_zone ?? ''); ?>
                        <select name="sub_zone" id="sub_zone" class="form-select" data-selected="<?php echo e($selectedSubZone); ?>">
                            <option value="">Select Sub Zone</option>
                            <?php $__currentLoopData = $zones; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $zone): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php $__currentLoopData = $zone->children; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $subZone): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($subZone->name); ?>" data-parent-id="<?php echo e($zone->id); ?>" <?php echo e($selectedSubZone === $subZone->name ? 'selected' : ''); ?>><?php echo e($subZone->name); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php if($selectedSubZone && ! $zones->flatMap->children->contains('name', $selectedSubZone)): ?>
                                <option value="<?php echo e($selectedSubZone); ?>" data-parent-id="saved" selected><?php echo e($selectedSubZone); ?> (Saved)</option>
                            <?php endif; ?>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Connection Date</label>
                        <input type="date" name="connection_date" id="connection_date" class="form-control" value="<?php echo e(old('connection_date', isset($customer) ? optional($customer->connection_date)->format('Y-m-d') : '')); ?>">
                    </div>
                    
                    <div class="col-md-6">
                        <label class="form-label">Disconnect Date</label>
                        <input type="date" name="mikrotik_disconnect" id="mikrotik_disconnect" class="form-control" value="<?php echo e(old('mikrotik_disconnect', isset($customer) ? optional($customer->mikrotik_disconnect)->format('Y-m-d') : '')); ?>">
                        <small class="text-muted">Leave empty for a lifetime connection without automatic disconnection.</small>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Office Address</label>
                        <textarea name="office_address" id="office_address" class="form-control" rows="2"><?php echo e(old('office_address', $customer->office_address ?? '')); ?></textarea>
                    </div>
                    <div class="col-12">
                        <label class="form-label">Remark</label>
                        <textarea name="remark" id="remark" class="form-control" rows="2"><?php echo e(old('remark', $customer->remark ?? '')); ?></textarea>
                    </div>
                    <div class="col-12 col-lg-6">
                        <div class="form-check form-switch app-toggle">
                            <input class="form-check-input" type="checkbox" name="generate_bill_this_month" value="1" id="generateBillThisMonth" <?php echo e(old('generate_bill_this_month') ? 'checked' : ''); ?>>
                            <label class="form-check-label" for="generateBillThisMonth">Generate bill from this month</label>
                        </div>
                    </div>
                    <div class="col-12 col-lg-6">
                        <div class="form-check form-switch app-toggle">
                            <input class="form-check-input" type="checkbox" name="send_sms" value="1" id="sendSms" <?php echo e(old('send_sms') ? 'checked' : ''); ?>>
                            <label class="form-check-label" for="sendSms">Send SMS notification to customer</label>
                        </div>
                    </div>
                </div>

                <div class="mt-4 text-end">
                    <button type="submit" class="btn btn-primary"><?php echo e(isset($customer) ? 'Update Customer' : 'Save Customer'); ?></button>
                    <a href="<?php echo e(route('admin.customers.index')); ?>" class="btn btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>

    <?php $__env->startPush('scripts'); ?>
        <script>
            document.addEventListener('DOMContentLoaded', function () {
                const form = document.getElementById('customerCreateForm');
                const mikrotikSelect = document.getElementById('mikrotik_id');
                const credentialFields = document.querySelectorAll('.mikrotik-credential-field');
                const packageSelect = document.getElementById('package_id');
                const monthlyBill = document.getElementById('monthly_bill');
                const zoneSelect = document.getElementById('zone');
                const subZoneSelect = document.getElementById('sub_zone');
                const editing = <?php echo json_encode(isset($customer), 15, 512) ?>;

                function toggleMikrotikCredentials() {
                    credentialFields.forEach(function (field) {
                        field.classList.toggle('d-none', !mikrotikSelect?.value);
                    });
                    document.getElementById('ip_address')?.toggleAttribute('required', !!mikrotikSelect?.value);
                    document.getElementById('queue_password')?.toggleAttribute('required', !!mikrotikSelect?.value && !editing);
                }

                packageSelect?.addEventListener('change', function () {
                    const option = packageSelect.options[packageSelect.selectedIndex];
                    if (option?.dataset.price !== undefined) monthlyBill.value = option.dataset.price;
                });

                if (mikrotikSelect) {
                    mikrotikSelect.addEventListener('change', toggleMikrotikCredentials);
                    toggleMikrotikCredentials();
                }

                function refreshSubZones(keepSelection = false) {
                    if (!zoneSelect || !subZoneSelect) return;
                    const zoneId = zoneSelect.options[zoneSelect.selectedIndex]?.dataset.zoneId || '';
                    const currentValue = keepSelection ? subZoneSelect.dataset.selected : subZoneSelect.value;
                    let currentVisible = false;

                    Array.from(subZoneSelect.options).forEach(function (option, index) {
                        if (index === 0) return;
                        const visible = !!zoneId && (option.dataset.parentId === zoneId || option.dataset.parentId === 'saved');
                        option.hidden = !visible;
                        option.disabled = !visible;
                        if (visible && option.value === currentValue) currentVisible = true;
                    });

                    subZoneSelect.value = currentVisible ? currentValue : '';
                    subZoneSelect.disabled = !zoneId;
                }

                zoneSelect?.addEventListener('change', function () {
                    subZoneSelect.dataset.selected = '';
                    refreshSubZones(false);
                });
                refreshSubZones(true);

                form.addEventListener('submit', function (event) {
                    event.preventDefault();

                    const formData = new FormData(form);
                    const data = Object.fromEntries(formData.entries());

                    if (!data.hasOwnProperty('send_sms')) {
                        data.send_sms = 0;
                    }

                    axios({ method: <?php echo json_encode(isset($customer) ? 'put' : 'post', 15, 512) ?>, url: form.action, data })
                        .then(function () {
                            window.location.href = '<?php echo e(route('admin.customers.index')); ?>';
                        })
                        .catch(function (error) {
                            if (error.response?.data?.errors) {
                                const errors = error.response.data.errors;
                                let message = Object.values(errors).flat().join('\n');
                                alert(message);
                            } else {
                                alert('Unable to save customer. Please try again.');
                                console.error(error);
                            }
                        });
                });
            });
        </script>
    <?php $__env->stopPush(); ?>
 <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $attributes = $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $component = $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php /**PATH D:\herd\billing\laravel-app\resources\views\admin\customers\create.blade.php ENDPATH**/ ?>