<?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";
if(empty($_POST['chargeUser']) || empty($_POST['typeCharge'])) 
{
	header("location:chargeRequest.php?res=2");
	exit;
}
include("validateLogin.php");
include("includeInformation.php");
$messageRequest = "";
$chargeUser = $_POST['chargeUser'];
$typeCharge = $_POST['typeCharge'];
if(!empty($_POST['messageRequest']))
	$messageRequest = (int)$_POST['messageRequest'];
if($typeCharge==2) {
	if(empty($messageRequest)) {
		header("location:chargeRequest.php?res=2");
		exit;
	}
	$querySelect="SELECT * FROM `charge_request` WHERE `id`='$chargeUser'";
	$resultSelect=mysqli_query($conn, $querySelect);
	$numSelect=mysqli_num_rows($resultSelect);
	if(!empty($numSelect)) {
		$rowSelect=mysqli_fetch_assoc($resultSelect);
		$id = $rowSelect['id'];
		$depart_name = $rowSelect['depart_name'];
		$depart_code = $rowSelect['depart_code'];
		$message_request = $rowSelect['message_request'];
		$date_add = $rowSelect['date_add'];
		$user_id = $rowSelect['user_id'];
		$user_name = $rowSelect['user_name'];
		$depart_admin_id = $rowSelect['depart_admin_id'];
		$charg_type = $rowSelect['charg_type'];
		$mysqlError="";
		if(mysqli_query($conn, "START TRANSACTION")) {
			$querySelectUser = "SELECT `account` FROM `user_login` WHERE `id`='$user_id'";
			$resultSelectUser = mysqli_query($conn, $querySelectUser);
			$rowSelectUser = mysqli_fetch_assoc($resultSelectUser);
			$countBefor=$rowSelectUser['account'];
			
			$query  =" update user_login set";
			$query .=" account=account+".$messageRequest.",";
			$query .=" accountOriginal=accountOriginal+".$messageRequest."";
			$query .=" where id='".$user_id."'";
			$result = mysqli_query($conn, $query);
			$mysqlError.= mysqli_error($conn);
			
			$querySelectUser1 = "SELECT `account` FROM `user_login` WHERE `id`='$user_id'";
			$resultSelectUser1 = mysqli_query($conn, $querySelectUser1);
			$rowSelectUser1 = mysqli_fetch_assoc($resultSelectUser1);
			$countAfter=$rowSelectUser1['account'];
			
			$queryInsert = "INSERT INTO `charge_archives` (`depart_name`,`charge_request_id`,`depart_code`,`message_request`,`message_charge`,`date_add`,`user_name`,`date_charge`,`user_id`,`depart_admin_id`,`depart_account_befor`,`depart_account_after`,`user_account_befor`,`user_account_after`,`power`)
			VALUES('$depart_name','$chargeUser','$depart_code','$message_request','$messageRequest','$date_add','$user_name','".time()."','$user_id','1','0','0','$countBefor','$countAfter','-')";
			$resultInsert=mysqli_query($conn, $queryInsert);
			$mysqlError.= mysqli_error($conn);
			$queryUpdate="UPDATE `charge_request` SET `charg_type`='2' WHERE `id`='$chargeUser'";
			$result=mysqli_query($conn, $queryUpdate);
			$mysqlError.= mysqli_error($conn);
			if(!empty($mysqlError)) {
				mysqli_query($conn, "ROLLBACK");
				header("location:chargeRequest.php?res=2");
				exit;
			} else {
				mysqli_query($conn, "COMMIT");
				header("location:chargeRequest.php?res=1");
				exit;
			}
		}
	} else {
		header("location:chargeRequest.php?res=2");
		exit;
	}
} elseif($typeCharge==3) {
	$queryUpdate="UPDATE `charge_request` SET `charg_type`='3' WHERE `id`='$chargeUser'";
	$result=mysqli_query($conn, $queryUpdate);
	if($result) {
		header("location:chargeRequest.php?res=1");
		exit;
	} else {
		header("location:chargeRequest.php?res=2");
		exit;
	}
} else {
	header("location: chargeRequest.php?res=2");
	exit;
}	
ob_end_flush();
?>
