<?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.');

}	

	// check tồn tại hay không

	$get_check = mysqli_query($conn, "Select * From account_web WHERE Account = '".$_POST['user']."'");

	if(mysqli_num_rows($get_check)){

    /* ------------------------------------------- CONNECT CHAR SV ------------------------------- */

    $get_Server = mysqli_query($conn, "Select * From config_server WHERE ServerID = '" . $_POST['sv'] . "'");

    if (!mysqli_num_rows($get_Server)) {

		$return['type'] = 1;

		$return['text'] = "Lấy thông tin máy chủ thất bại";

		echo json_encode($return);

		exit();		

		}

    $get_Server = mysqli_fetch_array($get_Server, MYSQLI_ASSOC);

    $conn_char  = @mysqli_connect($get_Server['IPdata'], $get_Server['Userdata'], $get_Server['PassData'], $get_Server['Databases']);

    if ($conn_char->mysqli_connect_errno) {

		$return['type'] = 1;

		$return['text'] = "Lỗi dữ liệu databases";

		echo json_encode($return);

		exit();	

    }

	mysqli_query($conn_char, "SET NAMES 'utf8'"); 

    /* ------------------------------------------- CONNECT CHAR SV ------------------------------- */	

	    $get_nhannvat = mysqli_query($conn_char, "Select * From Player WHERE Account = '" . $_POST['user'] . "' and ID = '".$_POST['id']."'");

    if (mysqli_num_rows($get_nhannvat)) {

		$get_nhannvat = mysqli_fetch_array($get_nhannvat, MYSQLI_ASSOC);

		$get_tilenap = mysqli_fetch_assoc(mysqli_query($conn, "Select * From config_card WHERE ID = '1'"));

		$KhuyenMaiBank = 2;

		$sotiennap = $_REQUEST['menhgia'];

		if($_REQUEST['menhgiatunhap']){

			$sotiennap = $_REQUEST['menhgiatunhap'];

		}

		if($_REQUEST['goinap'] == 'VCB' or $_REQUEST['goinap'] == 'Bank' or $_REQUEST['goinap'] == 'MoMo'){

				$KhuyenMaiBank = 4;

			}

		if($get_tilenap['khuyenmai'] > 1 and $get_tilenap['ketthuc'] >= strtotime(date('d-m-Y'))){

		$amount = $sotiennap * $get_tilenap['khuyenmai'] * $KhuyenMaiBank;

		} else {

		$amount = $sotiennap * $KhuyenMaiBank;		

		}

			$TienThat_Ref = $amount / 100 * 20;			

                $IDChar   = $get_nhannvat['ID'];

                $pud      = time() - 1509999999;

				$chuoititle = json_encode(array('type' => "SendMail_ACB00099552233"));

                $url      = "http://" . $get_Server['IpJson'] . ":" . $get_Server['PortJson'] . "/efunsendreward?userId=1&roleId=$IDChar&serverCode=".$get_Server['ServerID']."&serialNo=$pud&title=$chuoititle&content=" . urlencode("Bạn nhận được Tiền Duyệt từ BQT, Nạp Thông Qua [".$_REQUEST['goinap']."] Mệnh Giá :" . number_format($sotiennap) . "VNĐ nhận được ".number_format($amount)." Xu");

                $ch       = curl_init();

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                curl_setopt($ch, CURLOPT_HEADER, 0);

                curl_setopt($ch, CURLOPT_URL, $url);

                $output = curl_exec($ch);

                $reset  = json_decode($output);

                if ($reset->code == 1000) {

                    // update card

                    // success		

					mysqli_query($conn, "INSERT INTO logscard (UserName,Server,PlayerID,TypeCard,Seri,PassCard,Money,MoneyDefault,Timer,Active,TimerS,Hash) VALUES ('" . $_POST['user'] . "', '" . $get_Server['ServerID'] . "', '" . $IDChar . "', '00', '0000', '0000','$sotiennap','$sotiennap','".strtotime(date('d-m-Y H:i:s'))."','1','".strtotime(date('d-m-Y'))."','Bank')");				

					mysqli_query($conn, "INSERT INTO logs_admin (UserName,Type,Messager,Timer) VALUES ('" . $_SESSION['UserName'] . "', '1', N'Chuyển ".number_format($sotiennap)." VNĐ S-".$_POST['sv']."-".$_POST['user']." -".$IDChar."', '".strtotime(date('d-m-Y H:i:s'))."')");		
 		 /* Kiểm Tra */
		 $Check_Row = mysqli_query($conn, "Select * From account_web_money WHERE Account = '".$_POST['user']."' and ServerID = '".$_POST['sv']."'");
		 if(!mysqli_num_rows($Check_Row)){
			  mysqli_query($conn, "INSERT INTO account_web_money (ServerID,Account) VALUES ('".$_POST['sv']."','".$_POST['user']."')");
		 }                      
					mysqli_query($conn, "UPDATE account_web_money Set TienThat = TienThat + " . $amount . " WHERE Account = '" . $_POST['user'] . "' and ServerID = '".$_POST['sv']."'");

		$return['type'] = 0;

		$return['text'] = 'Thành công.';

		echo json_encode($return);

		exit();	

                } else {

		$return['type'] = 1;

		$return['text'] = $reset->message;

		echo json_encode($return);

		exit();							

                }		

	} else {

		$return['type'] = 1;

		$return['text'] = 'Nhân vật này không tồn tại.';

		echo json_encode($return);

		exit();		

	}

		} else {

		$return['type'] = 1;

		$return['text'] = 'Tài khoản này không tồn tại.';

		echo json_encode($return);

		exit();

	}

}



?>