<?php
session_start();
if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit; }

// Konfigurasi Database
$host = 'localhost';
$user = 'majapahi_omnich';
$pass = 'A1b2c3d4.,@';
$db   = 'majapahi_omnich';
$conn = new mysqli($host, $user, $pass, $db);

// Otomatis tambahkan kolom closed_by jika di database belum ada (agar aman)
$conn->query("ALTER TABLE contacts ADD COLUMN IF NOT EXISTS closed_by INT(11) NULL");

if (isset($_GET['phone'])) {
    $phone = $conn->real_escape_string($_GET['phone']);
    $user_id = $_SESSION['user_id'];
    
    // Update status jadi closed, kosongkan PIC aktif, DAN simpan ID CS yang menutup tiket
    $sql = "UPDATE contacts SET status = 'closed', assigned_to = NULL, closed_by = '$user_id' WHERE phone_number = '$phone'";
    $conn->query($sql);
}

header("Location: dashboard.php?tab=closed");
exit;
?>