<?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
***************************************************************************************************/

// Include DB connection and session (assumes these are handled globally)
require_once("db_connect.php"); // Replace with your actual DB connection include if needed

// GETTING AD STATUS AND LINKID
$pgmstatus = trim($_GET['pgmstatus'] ?? '');
$linkid = intval(trim($_GET['programs'] ?? 0));
$pgmid = intval(trim($_GET['programid'] ?? 0));
$adstatus = trim($_GET['adstatus'] ?? '');

if (empty($pgmid)) {
    $pgmid = $_SESSION['PGMID'] ?? 0;
}
if (empty($adstatus)) {
    $adstatus = 'all';
}

// APPROVE/REJECT ADS
if ($pgmstatus === 'Approve') {
    $stmt = $mysqli->prepare("UPDATE partners_text_old SET text_status='active' WHERE text_id=?");
    $stmt->bind_param("i", $linkid);
    $stmt->execute();
    $stmt->close();
} elseif ($pgmstatus === 'Reject') {
    $stmt = $mysqli->prepare("UPDATE partners_text_old SET text_status='inactive' WHERE text_id=?");
    $stmt->bind_param("i", $linkid);
    $stmt->execute();
    $stmt->close();
}

// SELECT ADS TO DISPLAY
switch ($adstatus) {
    case 'active':
        $sql3 = "SELECT * FROM partners_text_old WHERE text_programid=? AND text_status='active' ORDER BY text_id DESC";
        break;
    case 'inactive':
        $sql3 = "SELECT * FROM partners_text_old WHERE text_programid=? AND text_status='inactive' ORDER BY text_id DESC";
        break;
    case 'all':
    default:
        $sql3 = "SELECT * FROM partners_text_old WHERE text_programid=? ORDER BY text_id DESC";
        break;
}

$stmt = $mysqli->prepare($sql3);
$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 Texts</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->text_status));
        $id = stripslashes(trim($row->text_id));
        $text_url = htmlspecialchars($row->text_url);
        $text_text = htmlspecialchars(stripslashes($row->text_text));
        $text_description = htmlspecialchars(stripslashes($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 height="1" width="300" bgcolor="#FFFFFF" class="grid1">
                  <span class="text">
                    <b>Type:</b> Text<br/>
                    <b>URL:</b> <a href="<?= $text_url ?>" target="new"><?= $text_text ?></a>
                  </span>
                </td>
                <td height="1" width="302" bgcolor="#FFFFFF" class="grid1" align="right">
<?php
        if ($status === "active") {
?>
                  <a href="index.php?Act=add_text1&amp;pgmstatus=Reject&amp;programs=<?= $id ?>&amp;programid=<?= $pgmid ?>&amp;adstatus=<?= $adstatus ?>">Reject</a>
<?php
        } else {
?>
                  <a href="index.php?Act=add_text1&amp;pgmstatus=Approve&amp;programs=<?= $id ?>&amp;programid=<?= $pgmid ?>&amp;adstatus=<?= $adstatus ?>">Approve</a>
<?php
        }
?>
                </td>
              </tr>
              <tr>
                <td colspan="2" height="69" width="608">
                  <textarea rows="4" name="S1" cols="55"><?= $text_description ?></textarea>
                </td>
              </tr>
            </tbody>
          </table>
<?php
    } // End while
?>
        </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>
