<?php
session_start();
include_once('../../config/#Config.php');
if(!$_SESSION['UserName']){
die('<script>window.location.href=\''.$config['site']['url'].'index.php\';</script>');
} else {
$get_AD = mysqli_query($conn, "Select * From config_admin WHERE Account = '".$_SESSION['UserName']."'");
if(!mysqli_num_rows($get_AD)){
	die('Không đủ quyền.');
}
$id = $_POST['id'];
$account = $_POST['account'];
$amount = $_POST['amount'];
$amountedit = $_POST['amountedit'];
$status = $_POST['status'];
if($amountedit){
if(!is_numeric($amountedit)){
	die('Số không phải chữ.');
}
}
$SoTienNap = $amount;
if($amountedit){
	if($amount){
		die('Hãy tắt chọn Mệnh Giá nếu chọn Tùy Nhập Số tiền.');
	}
	$SoTienNap = $amountedit;
}
$gettt = mysqli_query($conn, "Select * From logscardlt WHERE ID = '$id'");
if(mysqli_num_rows($gettt)){
	$gettt_OK = mysqli_fetch_array($gettt, MYSQLI_ASSOC);
mysqli_query($conn, "UPDATE logscardlt SET Money = '$SoTienNap', Active='$status' WHERE ID='$id'");
$KhuyenMaiBank = 5;

	//new khánh đẹp trai cu to
	if($status == 1){
		$LinhThach_Ref = $SoTienNap / 100 * 20;
	$get_tilenap = mysqli_fetch_assoc(mysqli_query($conn, "Select * From config_card WHERE ID = '1'"));
			if($gettt_OK['Hash'] == 'VCB' or $gettt_OK['Hash'] == 'Bank' or $gettt_OK['Hash'] == 'MoMo'){
			$KhuyenMaiBank = 5;
		}
	if($get_tilenap['khuyenmai'] > 1 and $get_tilenap['ketthuc'] >= strtotime(date('d-m-Y'))){
	$SoTienNap = $SoTienNap * $get_tilenap['khuyenmai'] * $KhuyenMaiBank;
	} else {
	$SoTienNap = $SoTienNap * $KhuyenMaiBank;	
	}
 		 /* Kiểm Tra */
		 $Check_Row = mysqli_query($conn, "Select * From account_web_money WHERE Account = '".$account."' and ServerID = '".$gettt_OK['Server']."'");
		 if(!mysqli_num_rows($Check_Row)){
			  mysqli_query($conn, "INSERT INTO account_web_money (ServerID,Account) VALUES ('".$gettt_OK['Server']."','".$account."')");
		 }				
		 
	$INFOLinhThach = mysqli_fetch_array(mysqli_query($conn, "Select LinhThach From account_web_money WHERE Account = '".$account."' and ServerID = '".$gettt_OK['Server']."'"),MYSQLI_ASSOC);
	$SoDuSau = $SoTienNap + $INFOLinhThach['LinhThach'];
	if($status == 2){
	$SoDuSau = 0;
	$SoTienNap = 0;
}
	mysqli_query($conn, "UPDATE logscardlt Set SoDuTruoc = ".$INFOLinhThach['LinhThach'].", SoDuSau = $SoDuSau WHERE ID='$id'");
	// chia rad2deg
	//Update Money for account
	mysqli_query($conn, "UPDATE account_web_money Set LinhThach = LinhThach + $SoTienNap WHERE Account = '$account' and ServerID = '".$gettt_OK['Server']."'");
	mysqli_query($conn, "UPDATE account_ref Set ChuaNhan = ChuaNhan + $LinhThach_Ref WHERE Ref = '$account'");
	}
	die('0');
} else {
	die('Máy chủ này không tồn tại.');
}
}
?>