<?php
/**
 * Processing Forget password form data:
 * check data then generate new password and send it through SMPT Email
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 * @todo           Nothing
 */
ob_start();
session_start();
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
$idUser = (integer)$_POST["id"];
$maxNumMsg = (integer)$_POST["maxNumMsg"];
if(empty($idUser) || empty($maxNumMsg)) {
	echo $data="2";
}
include("validateLogin.php");
include("includeInformation.php");

$resultSelectBeforeCharge = mysqli_query($conn, "SELECT * FROM `user_login` WHERE id='".$idUser."'");
$rowSelectBeforeCharge = mysqli_fetch_assoc($resultSelectBeforeCharge);
$totalChargeBeforeCharge = $rowSelectBeforeCharge["accountOriginal"];
$accountBeforeCharge = $rowSelectBeforeCharge["account"];

$message_request = $maxNumMsg;
$date_add = time();
$user_id = $idUser;
$mysqlError="";
if(mysqli_query($conn, "START TRANSACTION")) {
	$querySelectUser = "SELECT `account`, `accountOriginal` FROM `user_login` WHERE `id`='$idUser'";
	$resultSelectUser = mysqli_query($conn, $querySelectUser);
	$rowSelectUser = mysqli_fetch_assoc($resultSelectUser);
	$countBefor = $rowSelectUser['account'];
	
	$query  =" update user_login set `account`=`account`+".$maxNumMsg.", `accountOriginal`=`accountOriginal`+".$maxNumMsg." WHERE `id`='".$idUser."'";
	$result = mysqli_query($conn, $query);
	$mysqlError.= mysqli_error($conn);
	
	$querySelectUser1 = "SELECT `account`, `accountOriginal` FROM `user_login` WHERE `id`='$idUser'";
	$resultSelectUser1 = mysqli_query($conn, $querySelectUser1);
	$rowSelectUser1 = mysqli_fetch_assoc($resultSelectUser1);
	$countAfter = $rowSelectUser1['account'];
	
	$queryInsert = "INSERT INTO `user_charge` (userId,date,charge,total_charge,chargeFrom,account,totalChargeBeforeCharge,accountBeforeCharge) VALUES ";
	$queryInsert .= "('".$user_id."','".$date_add."','".(integer) $maxNumMsg."','".(integer) $maxNumMsg."','".$_SESSION["userId"]."','".(integer) $countAfter."','".(integer) $totalChargeBeforeCharge."','".(integer) $accountBeforeCharge."')";
	$resultInsert = mysqli_query($conn,$queryInsert); 
	$mysqlError.= mysqli_error($conn);
	if(!empty($mysqlError)) {
		mysqli_query($conn, "ROLLBACK");
		echo $data="2#$#".$rowSelectUser['account']."#$#".$rowSelectUser['accountOriginal'];
	} else {
		mysqli_query($conn, "COMMIT");
		echo $data="1#$#".$rowSelectUser1['account']."#$#".$rowSelectUser1['accountOriginal'];
	}
}
ob_end_flush();
?>
