<?php
require_once __DIR__ . '/../includes/config.php';
if (file_exists(__DIR__ . '/../includes/auth.php')) require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/functions.php';
$id = (int)($_GET['id'] ?? 0);
$issues = infl_schema_issues();
$record = !$issues ? infl_find_influencer($id) : null;
if (!$record && !$issues) die('المؤثر غير موجود');
$form = $record ? array_merge(infl_default_form(), $record) : infl_default_form();
$form['status'] = (int)($form['status'] ?? 1);
$error = ''; $isEdit = true;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !$issues) {
    $form = array_merge($form, $_POST);
    $result = infl_update_influencer($id, $_POST);
    if ($result['success']) infl_redirect('view.php?id='.$id.'&msg='.urlencode($result['message']));
    $error = $result['message'];
}
include __DIR__ . '/../includes/header.php';
if (file_exists(__DIR__ . '/../includes/sidebar.php')) include __DIR__ . '/../includes/sidebar.php';
?>
<div class="content-wrapper" style="padding:20px;">
  <div style="margin-bottom:18px;"><h1 style="margin:0 0 8px;">تعديل المؤثر</h1><p style="margin:0;color:#6b7280;">تعديل بيانات الدخول، الحالة، وكود الخصم وعمولته.</p></div>
  <?php if ($error): ?><div style="margin-bottom:16px;padding:12px 14px;border-radius:12px;background:#fef2f2;border:1px solid #fecaca;color:#991b1b;"><?php echo infl_h($error); ?></div><?php endif; ?>
  <form method="post"><?php include __DIR__ . '/_form.php'; ?></form>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
