<?php
if(!defined('INDEX')) { header('Location: ../'); exit; }
$pagea = 'mail_liste';
$titre = 'Demandes et problèmes de la galaxie';

	$template->assign('linkpage', 'courrier');

if (!empty($_GET['w']) || !empty($_GET['x'])) {
	if (!empty($_GET['w'])) $id = $_GET['w'];
	else $id = $_GET['x'];
	$chapeau->connexion();
	$chapeau->escape($id);
	$req = $chapeau->unique_query("SELECT $table_user.pseudo, $table_ope_mail.statut, $table_ope_mail.time, $table_ope_mail.titre, $table_ope_mail.contenu, $table_ope_mail.id FROM $table_ope_mail INNER JOIN $table_user ON $table_user.id = $table_ope_mail.id_user WHERE $table_ope_mail.id = '$id';");
	if ($req['statut'] >= 6) $chapeau->query("UPDATE $table_ope_mail SET statut = '0' WHERE id = '$id';");
	else $chapeau->query("UPDATE $table_ope_mail SET statut = statut + 1 WHERE id = '$id';");
	$chapeau->deconnexion();

	if (isset($_GET['x'])) header('Location: admin.php?p=courrier&v='.$id);
	else header('Location: admin.php?p=courrier');
	exit;
}
elseif (!empty($_GET['v'])) {
	$id = $_GET['v'];
	$chapeau->connexion();
	$chapeau->escape($id);
	$req = $chapeau->unique_query("SELECT $table_user.pseudo, $table_ope_mail.statut, $table_ope_mail.time, $table_ope_mail.titre, $table_ope_mail.contenu, $table_ope_mail.id FROM $table_ope_mail INNER JOIN $table_user ON $table_user.id = $table_ope_mail.id_user WHERE $table_ope_mail.id = '$id';");
	$chapeau->deconnexion();

	$template->assign('req', $req);
	$template->assign('id', $id);
	$pagea = 'mail_view';
}
elseif (!empty($_GET['d']) && $sess->values['auth_level'] >= 5) {
	$id = $_GET['d'];
	$chapeau->connexion();
	$chapeau->escape($id);
	$chapeau->query("DELETE FROM $table_ope_mail WHERE id = '$id';");
	$chapeau->deconnexion();

	header('Location: admin.php?p=courrier');
	exit;
}
else {
	$chapeau->connexion();
	$req = $chapeau->query("SELECT $table_user.pseudo, $table_ope_mail.statut, $table_ope_mail.time, $table_ope_mail.titre, $table_ope_mail.contenu, $table_ope_mail.id FROM $table_ope_mail INNER JOIN $table_user ON $table_user.id = $table_ope_mail.id_user ORDER BY $table_ope_mail.time DESC;");
	$chapeau->connexion();
	$template->assign('mails', $req);
}
?>