<?php
include_once '../includes/constants.php';
include_once '../includes/functions.php';
include_once '../includes/session.php';
include_once '../includes/allstripslashes.php';

$partners = new partners();
$partners->connection($host, $user, $pass, $db);

// Connect using mysqli
$mysqli = new mysqli($host, $user, $pass, $db);
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}

// Populate list
$event_result = $mysqli->query("SELECT event_name FROM partners_event");

// Get count of active mails
$active_result = $mysqli->query("
    SELECT COUNT(*) AS active 
    FROM partners_adminmail A
    JOIN partners_event E ON A.adminmail_eventname = E.event_name
    WHERE E.event_status = 'yes'
");
$activerow = $active_result->fetch_object();

// Retrieve values from submitted form
if (!empty($_SESSION['BODY'])) {
    $from   = $_GET['from'] ?? '';
    $sub    = stripslashes($_GET['sub'] ?? '');
    $event  = $_GET['event'] ?? '';
    $status = $_REQUEST['status'] ?? '';
    $to     = $_GET['to'] ?? '';

    $body   = stripslashes($_SESSION['BODY']);
    $header = stripslashes($_SESSION['HEADER']);
    $footer = stripslashes($_SESSION['FOOTER']);
} else {
    $msg1 = $email_no_msg ?? '';
}

$msg = $_GET['msg'] ?? '';

// Radio button selection
$active = ($status == "yes") ? "checked='checked'" : '';
$inactive = ($status != "yes") ? "checked='checked'" : '';
?>

<script language="javascript" type="text/javascript">
    function select1_onchange() {
        if (document.f1.select1.value != "ChooseEvent") {
            document.f1.flag.value = "0";
            document.f1.submit();
        } else {
            document.f1.fromtxt.value = "";
            document.f1.subjecttxt.value = "";
            document.f1.bodytxt.value = "";
            document.f1.headertxt.value = "";
            document.f1.footertxt.value = "";
            return false;
        }
    }

    function msent_onclick() {
        if (document.f1.select1.value != "ChooseEvent") {
            document.f1.flag.value = "1";
            document.f1.submit();
        } else {
            return false;
        }
    }

    function btn_onclick() {
        if (document.f1.select1.value != "ChooseEvent") {
            document.f1.flag.value = "2";
            document.f1.submit();
        } else {
            return false;
        }
    }

    function btn1_onclick() {
        if (document.f1.select1.value != "ChooseEvent") {
            document.f1.flag.value = "1";
            document.f1.submit();
        } else {
            return false;
        }
    }
</script>

<form method="post" action="emailvalidate.php" id="f1" name="f1">
    <table cellpadding="0" cellspacing="0" class="tablebdr" align="center" width="100%">
        <tr><td colspan="3" height="10">&nbsp;</td></tr>
        <tr>
            <td width="60%" align="left">
                <table border="0" cellpadding="0" class="tablebdr" width="100%" id="AutoNumber1" cellspacing="0">
                    <tr><th colspan="4" class="tdhead">Email Messages</th></tr>
                    <tr><td colspan="4" align="center" class="textred"><?= htmlspecialchars($msg) ?></td></tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>Active Messages</td>
                        <td colspan="2" class="textred" align="left">: <?= $activerow->active ?></td>
                    </tr>
                    <tr>
                        <td class="body">&nbsp;</td>
                        <td>Choose Action</td>
                        <td colspan="2" align="left">
                            <select size="1" name="eventcompo" id="select1" onchange="return select1_onchange()">
                                <option selected="selected">ChooseEvent</option>
                                <?php
                                if ($event_result) {
                                    while ($row = $event_result->fetch_object()) {
                                        $selected = (trim($event) == trim($row->event_name)) ? "selected='selected'" : '';
                                        echo "<option $selected>" . htmlspecialchars($row->event_name) . "</option>";
                                    }
                                }
                                ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td class="body">&nbsp;</td>
                        <td>Current Status</td>
                        <td colspan="2" align="left">
                            Active <input type="radio" value="active" <?= $active ?> name="statusradio" />
                            &nbsp;&nbsp;Inactive <input type="radio" name="statusradio" value="inactive" <?= $inactive ?> />
                        </td>
                    </tr>
                    <tr>
                        <td class="body">&nbsp;</td>
                        <td>From</td>
                        <td colspan="2" align="left"><input type="text" name="fromtxt" size="27" value="<?= htmlspecialchars($from) ?>" /></td>
                    </tr>
                    <tr>
                        <td class="body">&nbsp;</td>
                        <td>Subject</td>
                        <td colspan="2" align="left"><input type="text" name="subjecttxt" size="27" value="<?= htmlspecialchars($sub) ?>" /></td>
                    </tr>
                    <tr>
                        <th colspan="4" class="tdhead">Please Paste HTML code on Header Body and Footer Fields</th>
                    </tr>
                    <tr><td class="body" colspan="4">&nbsp;</td></tr>
                    <tr>
                        <td class="body">&nbsp;</td>
                        <td>Header</td>
                        <td colspan="2"><textarea rows="2" name="headertxt" cols="41"><?= htmlspecialchars($header) ?></textarea></td>
                    </tr>
                    <tr>
                        <td class="body">&nbsp;</td>
                        <td>Body</td>
                        <td colspan="2"><textarea rows="8" name="bodytxt" cols="41" class="bodytxt"><?= htmlspecialchars($body) ?></textarea></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>Footer</td>
                        <td colspan="2"><textarea rows="2" name="footertxt" cols="41"><?= htmlspecialchars($footer) ?></textarea></td>
                    </tr>
                    <tr><td colspan="4">&nbsp;</td></tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td></td>
                        <td><input type="submit" value="Update" name="B1" /></td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="tdhead"><input type="hidden" value="0" id="flag" name="flag" /></td>
                        <td class="tdhead">Test</td>
                        <td class="tdhead" colspan="2">
                            <input type="text" name="to" size="28" value="<?= htmlspecialchars($to) ?>" />
                            &nbsp;&nbsp;&nbsp;<input type="button" value="TestSend" name="B2" onclick="return btn1_onclick()" />
                        </td>
                    </tr>
                </table>
            </td>

            <td width="2%">&nbsp;</td>

            <td width="38%" align="right" valign="top">
                <?php include 'email_variable_legend.php'; ?>
            </td>
        </tr>
        <tr><td colspan="3" height="10">&nbsp;</td></tr>
    </table>
</form>
