<?php
/***************************************************************************************************
PROGRAM DESCRIPTION :  PROGRAM TO DISPLAY THE EXISTING TEXTS OF A SELECTED PROGRAM(PROGRAM.PHP)
                       SO THAT ADMIN CAN MANUALLY CHECK IT AND APPROVE/REJECT.
VARIABLES          : pgmstatus       = AD STATUS(APPROVE OR REJECT)
                     linkid,id       = AD ID
                     status          = AD status
***************************************************************************************************/

require_once("includes/dbconnect.php"); // Make sure this includes your $conn (mysqli)

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

$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';

// Approve or Reject Ad
if ($pgmstatus === 'Approve') {
    $stmt = $conn->prepare("UPDATE partners_text SET text_status = 'active' WHERE text_id = ?");
    $stmt->bind_param("i", $linkid);
    $stmt->execute();
    $stmt->close();
} elseif ($pgmstatus === 'Reject') {
    $stmt = $conn->prepare("UPDATE partners_text SET text_status = 'inactive' WHERE text_id = ?");
    $stmt->bind_param("i", $linkid);
    $stmt->execute();
    $stmt->close();
}

// Fetch Ads based on Status
switch ($adstatus) {
    case 'active':
        $stmt = $conn->prepare("SELECT * FROM partners_text WHERE text_programid = ? AND text_status = 'active' ORDER BY text_id DESC");
        break;
    case 'inactive':
        $stmt = $conn->prepare("SELECT * FROM partners_text WHERE text_programid = ? AND text_status = 'inactive' ORDER BY text_id DESC");
        break;
    case 'all':
    default:
        $stmt = $conn->prepare("SELECT * FROM partners_text WHERE text_programid = ? ORDER BY text_id DESC");
        break;
}

$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%" class="tablebdr">
      <tr>
        <td width="100%" height="18" colspan="3" class="tdhead">Existing Texts</td>
      </tr>

<?php
if ($res->num_rows > 0) {
?>
      <tr><td colspan="3">&nbsp;</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->text_status));
        $id = (int) $row->text_id;
        $text_url = trim($row->text_url);
        $text_text = $row->text_text;
        $text_image = $row->text_image;
        $text_description = $row->text_description;

        // Ensure URL starts with http://
        if (stripos($text_url, 'http://') !== 0 && stripos($text_url, 'https://') !== 0) {
            $text_url = "http://" . $text_url;
        }
?>
          <table cellspacing="0" cellpadding="5" width='622' class="tablebdr">
            <tbody>
              <tr>
                <td width='300' class="grid1">&nbsp;</td>
                <td width='302' class="grid1" align="right">
<?php
        if ($userobj->GetAdminUserLink('Approve/Reject Links', $adminUserId, 3)) {
            if ($status === "active") {
                echo "<a href=\"index.php?Act=add_text&pgmstatus=Reject&programs=$id&programid=$pgmid&adstatus=$adstatus\">Reject</a>";
            } else {
                echo "<a href=\"index.php?Act=add_text&pgmstatus=Approve&programs=$id&programid=$pgmid&adstatus=$adstatus\">Approve</a>";
            }
        }
?>
                </td>
              </tr>
              <tr>
                <td align="center" colspan="2">
                  <div align="center" style="overflow:auto; border:none; width:500px; height:75px">
                    <table align="center" width="500" height="75" border="1" style="border-color:#3399FF;" cellpadding="0" cellspacing="0">
                      <tr valign="top">
                        <td valign="top" width="100%" height="100%">
                          <table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
                            <tr bgcolor="#3399FF" height="20">
                              <td align="left" <?php if (!empty($text_image)) echo 'colspan="2"'; ?>><font color="#FFFFFF"><b>SPONSORED LISTINGS</b></font></td>
                            </tr>
                            <tr>
                              <td align="left"><a href="<?= htmlspecialchars($text_url) ?>" target="_blank"><?= htmlspecialchars($text_text) ?></a></td>
<?php if (!empty($text_image)) { ?>
                              <td rowspan="3" align="right"><img src="../thumbnail.php?image=<?= urlencode($text_image) ?>&height=50" alt="0" border="0" /></td>
<?php } ?>
                            </tr>
                            <tr>
                              <td align="left"><?= htmlspecialchars(stripslashes($text_description)) ?></td>
                            </tr>
                            <tr>
                              <td align="left"><?= htmlspecialchars($text_url) ?></td>
                            </tr>
                          </table>
                        </td>
                      </tr>
                    </table>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
<?php
    }
?>
        </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 records found.</td>
      </tr>
<?php
}
$stmt->close();
?>
    </table>
  </center>
</div>
