<?php 
include("../../operainfo/getq2.php");

function getpost($key, $default = '') {
    return safetext2(
        isset($_POST[$key]) ? $_POST[$key] :
        (isset($_GET[$key]) ? $_GET[$key] : $default)
    );
}


$contactid     = getpost('contactid');
$companyid     = getpost('companyid');
$stkref        = getpost('stkref');
$hostcompanyid = getpost('hostcompanyid');
$typeofstatus  = getpost('typeofstatus', 0);
$statusid      = getpost('statusid', 0);

$operadesc = ''; // default value to avoid undefined notice

// Use pdoquery with prepared statements
$sql = "SELECT * FROM stocklist_tarriffs WHERE hostcompany_id = ? AND Stock_ref = ?";
$stmt = pdoquery($sql, array($hostcompanyid, $stkref));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $operadesc = isset($row['OperaDescription']) ? $row['OperaDescription'] : '';
}

$statusdesc = [];
$sql = "SELECT id, description, type FROM viamedquotecontactstockstatuslist ORDER BY id ASC";
$stmt = pdoquery($sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $statusdesc[$row['id']] = $row['description'];
}



// Feedback
if ($typeofstatus == 0) {
    $memo = "$stkref $operadesc Customer Feedback Requested ";
    
    $sql = "INSERT INTO viamedquotecontactstockstatus
    (stock_ref, contactid, companyid, addedby, addedon, statusid, hostcompanyid, typedata)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
    pdoquery($sql, array($stkref, $contactid, $companyid, $vui, $nowis, '8', $hostcompanyid, 'Feedback'));

    $sql = "INSERT INTO viamedquotestatus
    (rasiedby, rep_id, timestampraised, status, memo, company_id, customer_id)
    VALUES (?, ?, ?, ?, ?, ?, ?)";
    pdoquery($sql, array($localname, $vui, $nowis, 'FeedBack Request', $memo, $companyid, $contactid));
}

// Lost Customers
if ($typeofstatus == 1) {
    $memo = "$stkref Lost Customer Update Status $statusdesc[$statusid] ";
    
    $sql = "INSERT INTO viamedquotecontactstockstatus
    (stock_ref, contactid, companyid, addedby, addedon, statusid, hostcompanyid, typedata)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
    pdoquery($sql, array($stkref, $contactid, $companyid, $vui, $nowis, $statusid, $hostcompanyid, 'LostCustomer'));

    $sql = "INSERT INTO viamedquotestatus
    (rasiedby, rep_id, timestampraised, status, memo, company_id, customer_id)
    VALUES (?, ?, ?, ?, ?, ?, ?)";
    pdoquery($sql, array($localname, $vui, $nowis, 'Lost Customer Update', $memo, $companyid, $contactid));
}

// Lost Customers
if ($typeofstatus == 2) {  
 $memo = "$stkref Lost Customer Action $statusdesc[$statusid] ";
    $sql = "INSERT INTO viamedquotecontactstockstatus
    (stock_ref, contactid, companyid, addedby, addedon, statusid, hostcompanyid, typedata)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
    pdoquery($sql, array($stkref, $contactid, $companyid, $vui, $nowis, $statusid, $hostcompanyid, 'LostCustomerAction'));
    
        $sql = "INSERT INTO viamedquotestatus
    (rasiedby, rep_id, timestampraised, status, memo, company_id, customer_id)
    VALUES (?, ?, ?, ?, ?, ?, ?)";
    pdoquery($sql, array($localname, $vui, $nowis, 'Lost Customer Action', $memo, $companyid, $contactid));
}


/*
Code confirmed compatible with PHP 5.6 and PHP 8.x
Upgraded to pdoquery() with prepared statements for security and compatibility
Ensured all $_GET variables use isset() fallback for PHP 5.6 support
*/
?>
Logged
