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

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

$mysqli = $partners->mysqli;

// Sanitize GET parameters
$mode = isset($_GET['mode']) ? $_GET['mode'] : '';
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$msg = isset($_GET['msg']) ? htmlspecialchars($_GET['msg']) : '';

// Fetch all languages
$sql = "SELECT * FROM `partners_languages`";
$res = $mysqli->query($sql);
if (!$res) {
    die("Database Error: " . $mysqli->error);
}
$num = $res->num_rows;

$language = '';
$status = '';
if ($mode === "edit" && $id > 0) {
    $stmt = $mysqli->prepare("SELECT * FROM `partners_languages` WHERE `languages_id` = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result1 = $stmt->get_result();
    if ($row1 = $result1->fetch_object()) {
        $language = htmlspecialchars(trim($row1->languages_name));
        $status = htmlspecialchars(trim($row1->languages_status));
        $id = (int)$row1->languages_id;
    }
    $stmt->close();
}
?>

<br/>
<div align="center" class="textred"><h5><?= $msg ?></h5></div>

<form name="form1" method="post" action="languages_validate.php?mode=<?= $mode ?>&amp;id=<?= $id ?>">
    <table width="70%" border="0" class="tablebdr" align="center">
        <tr>
            <td colspan="4" class="tdhead">Add or Edit languages</td>
            <td width="43%" rowspan="3" valign="top" class="tdhead">
                <table width="100%" border="0" class="tablewbdr">
                    <tr>
                        <td colspan="3" class="tdhead">Existing languages</td>
                    </tr>
                    <?php while ($row = $res->fetch_object()):
                        $languages_name = htmlspecialchars(trim($row->languages_name));
                        $languages_status = htmlspecialchars(trim($row->languages_status));
                        $languages_id = (int)$row->languages_id;
                    ?>
                        <tr class="grid1">
                            <td width="69%">
                                <div align="left" <?= ($languages_status == "inactive" ? "class='textred'" : "") ?>>
                                    <?= $languages_name ?>
                                </div>
                            </td>
                            <td width="17%">
                                <div align="center">
                                    <a href="index.php?Act=languages&amp;mode=edit&amp;id=<?= $languages_id ?>">Edit</a>
                                </div>
                            </td>
                            <td width="14%">
                                <div align="center">
                                    <a href="languages_validate.php?mode=delete&amp;id=<?= $languages_id ?>" onclick="return confirm('Are you sure you want to delete this language?');">Delete</a>
                                </div>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </table>
            </td>
        </tr>
        <tr>
            <td width="1%">&nbsp;</td>
            <td width="26%">Language Name</td>
            <td colspan="2">
                <input name="language" type="text" size="30" value="<?= $language ?>" required />
            </td>
        </tr>
        <tr class="gtrid2">
            <td>&nbsp;</td>
            <td>Status</td>
            <td width="15%">Active &nbsp;
                <input name="status" type="radio" value="active"
                    <?= ($status === "active" || $mode === "add") ? "checked='checked'" : "" ?> />
            </td>
            <td width="15%">Inactive&nbsp;
                <input name="status" type="radio" value="inactive"
                    <?= ($status === "inactive") ? "checked='checked'" : "" ?> />
            </td>
        </tr>
        <tr class="gtrid2">
            <td>&nbsp;</td>
            <td colspan="3">
                <div align="center">
                    <input type="submit" name="Submit" value="Submit" />
                </div>
            </td>
            <td valign="top" class="tdhead">&nbsp;</td>
        </tr>
    </table>
</form>

<br/>

<table class="tablebdr" border="0" width="90%">
    <tr>
        <td class="textred">
            <p><b>NOTE :</b></p>
        </td>
    </tr>
    <tr>
        <td>
            <p>If you have added or activated a new Language to this system,
                Please copy the language file to the <code>lang</code> folders.</p>
        </td>
    </tr>
</table>
<br />
