<?php
$page_title = 'إعدادات الربط للعملاء';
$comms_tab = 'connect';
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/settings_store.php';

require_login();
if(function_exists('require_perm')) require_perm('can_settings');

$UA_FORCE_DESIGN_V2 = true;
$uaAndroidCustomerConnect = ((string)($_GET['ua_android'] ?? '') === '1')
  || stripos((string)($_SERVER['HTTP_USER_AGENT'] ?? ''), 'NetworkCustomersAndroid/') !== false;
$UA_PAGE_CSS = ['assets/css/pages/customer_connect_admin.css'];
if ($uaAndroidCustomerConnect) $UA_PAGE_CSS[] = 'assets/css/android_customer_connect.css';
include __DIR__ . '/header.php';
include __DIR__ . '/sidebar.php';

$msg=''; $err='';

$fields = [
  'pppoe_service_name' => 'اسم خدمة PPPoE (اختياري)',
  'primary_dns'        => 'DNS الأساسي',
  'secondary_dns'      => 'DNS الثانوي',
  'support_phone'      => 'رقم الدعم',
  'notes'              => 'ملاحظات للعميل',
];

if(isset($_POST['save'])){
  try{
    foreach($fields as $k=>$lbl){
      $v = trim($_POST[$k] ?? '');
      setting_set($conn, $k, $v);
    }
    $msg = 'تم حفظ الإعدادات ✅';
  }catch(Exception $e){ $err = $e->getMessage(); }
}

$val = [];
foreach($fields as $k=>$lbl){
  $val[$k] = (string)setting_get($conn, $k, '');
}
if ($uaAndroidCustomerConnect) {
  require __DIR__ . '/android_customer_connect.php';
  require_once __DIR__ . '/output_devices_lib.php';
  if ($conn instanceof mysqli) ua_output_print_bootstrap($conn, 'subscriber_connection');
  include __DIR__ . '/footer.php';
  exit;
}
?>

<div class="content-wrapper ua-extracted-page ua-customer-connect-page">
  <section class="content-header">
    <div class="container-fluid d-flex justify-content-between align-items-center">
      <h1 class="mb-0">إعدادات الربط للعملاء (PPPoE)</h1>
      <button class="btn btn-primary no-print" onclick="window.print();"><i class="fa fa-print"></i> طباعة</button>
    </div>
  </section>

  <section class="content">
    <div class="container-fluid">
      <header class="ua-customer-connect-hero">
        <div>
          <span><i class="fa fa-link"></i> PPPoE Customer Access</span>
          <h1>إعدادات الربط للعملاء</h1>
          <p>بيانات خدمة الاتصال وDNS والدعم التي تظهر ضمن تعليمات تسليم العميل.</p>
        </div>
        <button class="btn btn-primary no-print" type="button" onclick="window.print();"><i class="fa fa-print"></i> طباعة التعليمات</button>
      </header>
      <?php if($msg){ ?><div class="alert alert-success no-print"><?= h($msg) ?></div><?php } ?>
      <?php if($err){ ?><div class="alert alert-danger no-print"><?= h($err) ?></div><?php } ?>

      <div class="card compact no-print">
        <div class="card-body">
          <form method="post" class="form-compact labels-left">
            <div class="row">
              <div class="col-md-6">
                <div class="form-group">
                  <label><?= h($fields['pppoe_service_name']) ?></label>
                  <input class="form-control" name="pppoe_service_name" value="<?= h($val['pppoe_service_name']) ?>">
                </div>
              </div>
              <div class="col-md-3">
                <div class="form-group">
                  <label><?= h($fields['primary_dns']) ?></label>
                  <input class="form-control" name="primary_dns" value="<?= h($val['primary_dns']) ?>" placeholder="1.1.1.1">
                </div>
              </div>
              <div class="col-md-3">
                <div class="form-group">
                  <label><?= h($fields['secondary_dns']) ?></label>
                  <input class="form-control" name="secondary_dns" value="<?= h($val['secondary_dns']) ?>" placeholder="8.8.8.8">
                </div>
              </div>
            </div>

            <div class="row">
              <div class="col-md-4">
                <div class="form-group">
                  <label><?= h($fields['support_phone']) ?></label>
                  <input class="form-control" name="support_phone" value="<?= h($val['support_phone']) ?>">
                </div>
              </div>
              <div class="col-md-8">
                <div class="form-group">
                  <label><?= h($fields['notes']) ?></label>
                  <textarea class="form-control" name="notes" rows="2"><?= h($val['notes']) ?></textarea>
                </div>
              </div>
            </div>

            <button class="btn btn-success" name="save" value="1"><i class="fa fa-save"></i> حفظ</button>
          </form>
        </div>
      </div>

      <div class="card compact">
        <div class="card-body">
          <h4 class="ua-u-fw-900">تعليمات الربط (للعميل)</h4>
          <ol>
            <li>افتح إعدادات الراوتر/الجهاز وابحث عن PPPoE.</li>
            <li>أدخل اسم المستخدم وكلمة المرور المرسلة لك.</li>
            <?php if($val['pppoe_service_name']!==''){ ?>
              <li>Service Name: <b><?= h($val['pppoe_service_name']) ?></b></li>
            <?php } ?>
            <?php if($val['primary_dns'] || $val['secondary_dns']){ ?>
              <li>DNS: <b><?= h($val['primary_dns'] ?: '-') ?></b> / <b><?= h($val['secondary_dns'] ?: '-') ?></b></li>
            <?php } ?>
          </ol>
          <?php if($val['support_phone']){ ?>
            <div><b>الدعم:</b> <?= h($val['support_phone']) ?></div>
          <?php } ?>
          <?php if($val['notes']){ ?>
            <div class="mt-2"><b>ملاحظات:</b> <?= nl2br(h($val['notes'])) ?></div>
          <?php } ?>
        </div>
      </div>
    </div>
  </section>
</div>

<?php require_once __DIR__ . '/output_devices_lib.php'; if ($conn instanceof mysqli) ua_output_print_bootstrap($conn, 'subscriber_connection'); ?>
<?php include __DIR__ . '/footer.php'; ?>
