<?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';

admin_require_post_action('promo_status', admin_url('promo_codes/list.php'));

$id = (int)($_POST['id'] ?? 0);
$value = (int)($_POST['value'] ?? 0);
$back = (string)($_POST['back'] ?? admin_url('promo_codes/list.php'));
if (!str_starts_with($back, '/app/admin/promo_codes/')) {
    $back = admin_url('promo_codes/list.php');
}

$result = promo_set_status($id, $value);
admin_log_activity('promo_code.set_status', 'تغيير حالة كود خصم', 'تم تغيير حالة كود الخصم.', 'promo_code', $id, ['status' => $value], true);
$separator = (strpos($back, '?') !== false) ? '&' : '?';
promo_redirect($back . $separator . 'msg=' . urlencode($result['message']));
