<?php
/***************************************************************************************************
  PROGRAM DESCRIPTION :  DISPLAY EXISTING TEXT LINKS OF A SELECTED PROGRAM (PROGRAM.PHP)
                         FOR ADMIN TO MANUALLY CHECK AND APPROVE/REJECT.
****************************************************************************************************/

require_once("includes/common.php"); // include DB connection and session start
require_once("classes/adminuser.php");

$userobj = new adminuser();
$adminUserId = $_SESSION['ADMINUSERID'] ?? 0;

// GET PARAMETERS
$pgmstatus  = isset($_GET['pgmstatus']) ? trim($_GET['pgmstatus']) : '';
$linkid     = isset($_GET['programs']) ? (int) $_GET['programs'] : 0;
$pgmid      = isset($_GET['programid']) ? (int) $_GET['programid'] : ($_SESSION['PGMID'] ?? 0);
$adstatus   = isset($_GET['adstatus']) ? trim($_GET['adstatus']) : 'all';

// PROCESS APPROVE / REJECT
if ($pgmstatus === 'Approve' && $linkid > 0) {
    $stmt = $mysqli->prepare("UPDATE partners_links SET link_status='active' WHERE link_id=?");
    $stmt->bind_param("i", $linkid);
    $stmt->execute();
    $stmt->close();
} elseif ($pgmstatus === 'Reject' && $linkid > 0) {
    $stmt = $mysqli->prepare("UPDATE partners_links SET link_status='inactive' WHERE link_id=?");
    $stmt->bind_param("i", $linkid);
    $stmt->execute();
    $stmt->close();
}

// SELECT TEXT LINKS BASED ON STATUS
switch ($adstatus) {
    case 'active':
        $sql = "SELECT * FROM partners_links WHERE link_programid=? AND link_status='active' ORDER BY link_id DESC";
        break;
    case 'inactive':
        $sql = "SELECT * FROM partners_links WHERE link_programid=? AND link_status='inactive' ORDER BY link_id DESC";
        break;
    default:
        $sql = "SELECT * FROM partners_links WHERE link_programid=? ORDER BY link_id DESC";
        break;
}

$stmt = $mysqli->prepare($sql);
$stmt->bind_param("i", $pgmid);
$stmt->execute();
$res = $stmt->get_result();
?>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<div align="center">
    <center>
        <table border='0' cellpadding="0" cellspacing="0" style="border-collapse: collapse; text-align: center" width="77%" id="AutoNumber4" class="tablebdr">
            <tr>
                <td width="100%" height="18" colspan="3" class="tdhead">Existing Text Links</td>
            </tr>
            <?php if ($res && $res->num_rows > 0): ?>
                <tr>
                    <td width="100%" height="18" colspan="3"></td>
                </tr>
                <tr>
                    <td width="2%" height="19">&nbsp;</td>
                    <td width="97%" height="19">
                        <?php while ($row = $res->fetch_object()):
                            $status = stripslashes(trim($row->link_status));
                            $id = (int) $row->link_id;
                            $name = stripslashes($row->link_text);
                            $url = stripslashes($row->link_url);
                        ?>
                        <table cellspacing="0" cellpadding="5" width='622' class="tablebdr">
                            <tbody>
                                <tr>
                                    <td height='1' width='300' bgcolor="#FFFFFF" class="grid1">
                                        <span class='text'><b>Type:</b> Text Link<br/>
                                        <b>Link ID: <?= $id ?></b><br/>
                                        <b>Text:</b> <?= htmlspecialchars($name) ?><br/>
                                        <b>URL:</b> <a href="<?= htmlspecialchars($url) ?>" target="_blank"><?= htmlspecialchars($url) ?></a></span>
                                    </td>
                                    <td height='1' width='302' bgcolor="#FFFFFF" class="grid1" align="right">
                                        <?php if ($userobj->GetAdminUserLink('Approve/Reject Links', $adminUserId, 3)): ?>
                                            <?php if ($status === "active"): ?>
                                                <a href="index.php?Act=add_link&amp;pgmstatus=Reject&amp;programs=<?= $id ?>&amp;programid=<?= $pgmid ?>&amp;adstatus=<?= $adstatus ?>">Reject</a>
                                            <?php else: ?>
                                                <a href="index.php?Act=add_link&amp;pgmstatus=Approve&amp;programs=<?= $id ?>&amp;programid=<?= $pgmid ?>&amp;adstatus=<?= $adstatus ?>">Approve</a>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <?php endwhile; ?>
                    </td>
                    <td width="1%" height="19">&nbsp;</td>
                </tr>
                <tr>
                    <td width="2%" height="19">&nbsp;</td>
                    <td width="97%" height="19">&nbsp;</td>
                    <td width="1%" height="19">&nbsp;</td>
                </tr>
                <tr>
                    <td width="100%" height="19" colspan="3" class="tdhead">&nbsp;</td>
                </tr>
            <?php else: ?>
                <tr>
                    <td width="100%" align="center" class="textred" colspan="3">No text link ads found.</td>
                </tr>
            <?php endif; ?>
        </table>
    </center>
</div>

<?php
$stmt->close();
?>