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

require_once("includes/common.php");
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_popup SET popup_status='active' WHERE popup_id=?");
    $stmt->bind_param("i", $linkid);
    $stmt->execute();
    $stmt->close();
} elseif ($pgmstatus === 'Reject' && $linkid > 0) {
    $stmt = $mysqli->prepare("UPDATE partners_popup SET popup_status='inactive' WHERE popup_id=?");
    $stmt->bind_param("i", $linkid);
    $stmt->execute();
    $stmt->close();
}

// SELECT POPUPS BASED ON STATUS
switch ($adstatus) {
    case 'active':
        $sql = "SELECT * FROM partners_popup WHERE popup_programid=? AND popup_status='active' ORDER BY popup_id DESC";
        break;
    case 'inactive':
        $sql = "SELECT * FROM partners_popup WHERE popup_programid=? AND popup_status='inactive' ORDER BY popup_id DESC";
        break;
    default:
        $sql = "SELECT * FROM partners_popup WHERE popup_programid=? ORDER BY popup_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 Popups</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->popup_status));
                            $id     = (int) $row->popup_id;
                            $url    = stripslashes($row->popup_url);
                            $width  = (int) $row->popup_width;
                            $height = (int) $row->popup_height;
                        ?>
                        <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> POPUP<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_popup&amp;pgmstatus=Reject&amp;programs=<?= $id ?>&amp;programid=<?= $pgmid ?>&amp;adstatus=<?= $adstatus ?>">Reject</a>
                                            <?php else: ?>
                                                <a href="index.php?Act=add_popup&amp;pgmstatus=Approve&amp;programs=<?= $id ?>&amp;programid=<?= $pgmid ?>&amp;adstatus=<?= $adstatus ?>">Approve</a>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" height='69' width='608' align="center">
                                        <a href="#">
                                            <img src='../merchants/images/popup.gif' border='0' width="300" height="60" onclick="window.open('<?= htmlspecialchars($url) ?>','popup','width=<?= $width ?>,height=<?= $height ?>')" alt="" />
                                        </a>
                                    </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"></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 popup ads found.</td>
                </tr>
            <?php endif; ?>
        </table>
    </center>
</div>

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