<?php
require_once './includes/session.php';
require_once __DIR__ . '/../admin/db_config.php';

$session = SessionManager::getInstance();
if (!$session->isLoggedIn()) { header('Location: login.php'); exit; }

try {
    $pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET, DB_USER, DB_PASS);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $fields = [];
    $params = [];

    if (isset($_POST['mobile']) && $_POST['mobile'] !== '') {
        $fields[] = 'mobile = ?';
        $params[] = $_POST['mobile'];
    }
    if (isset($_POST['callmobile']) && $_POST['callmobile'] !== '') {
        $fields[] = 'callmobile = ?';
        $params[] = $_POST['callmobile'];
    }
    if (isset($_POST['preferred_sim_slot']) && $_POST['preferred_sim_slot'] !== '') {
        $fields[] = 'preferred_sim_slot = ?';
        $params[] = $_POST['preferred_sim_slot'];
    }

    if (!empty($fields)) {
        $fields[] = 'updatedate = NOW()';
        $sql = "UPDATE settings SET " . implode(', ', $fields) . " WHERE id = 1";
        $stmt = $pdo->prepare($sql);
        $stmt->execute($params);
    }
} catch (Exception $e) {
    // silent
}

header('Location: devices.php');
exit;
