<?php
define('CODE_SECRET', 'iaZvd8b2Uv0RBA8');
define('RATE_WCOIN', 1);
define('BONUS_START_TIME', '2022-09-20 01:00:00'); // year-month-day hour:minute:second
define('BONUS_END_TIME', '2022-09-21 23:00:00');
define('BONUS_RATE', 0); // 50%

include('conn.php');

$tableUser = 'MEMB_INFO';
$tableCash = 'CashShopData';
$tableLogMoney = 'Payment_history';

function calBonus($money)
{
    $now = date('Y-m-d H:i:s');
    if($now >= BONUS_START_TIME && $now <= BONUS_END_TIME) {
        $bonus = floor($money * BONUS_RATE);
        return $bonus;
    }
    return 0;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    $money = isset($_POST['so_tien']) ? $_POST['so_tien'] : NULL;
    $bankName = isset($_POST['ten_bank']) ? $_POST['ten_bank'] : NULL;
    $accountId = isset($_POST['id_khach']) ? $_POST['id_khach'] : NULL;
    $fullName = isset($_POST['ten_khach']) ? $_POST['ten_khach'] : NULL;
    $phone = isset($_POST['sdt_khach']) ? $_POST['sdt_khach'] : NULL;
    $transactionCode = isset($_POST['ma_gd']) ? $_POST['ma_gd'] : NULL;
    $content = isset($_POST['noi_dung']) ? $_POST['noi_dung'] : NULL;
    $transactionId = isset($_POST['trans_id']) ? $_POST['trans_id'] : NULL;
    $remainingMoney = isset($_POST['soDu_bank']) ? $_POST['soDu_bank'] : NULL;
    $bankTime = isset($_POST['thoi_gian']) ? $_POST['thoi_gian'] : NULL;
    $secretCode = isset($_POST['ma_baoMat']) ? $_POST['ma_baoMat'] : NULL;

    if($secretCode !== CODE_SECRET) {
        echo json_encode([
            'status' => 0, 
            'message' => 'Mã bảo mật không đúng', 
            'result' => ''
        ], JSON_UNESCAPED_UNICODE);
        exit;
    }

    $sqlCash = 'select * from '.$tableCash.' where AccountID = :AccountID';
    $stmt = $pdoSqlsrv->prepare($sqlCash);
    $stmt->execute([':AccountID' => $accountId]);
    $cashData = $stmt->fetch(PDO::FETCH_ASSOC);

    $sqlUser = 'select * from '.$tableUser.' where memb___id = :AccountID';
    $stmt = $pdoSqlsrv->prepare($sqlUser);
    $stmt->execute([':AccountID' => $accountId]);
    $userGame = $stmt->fetch(PDO::FETCH_ASSOC);
    if( ! isset($cashData['AccountID']) || ! isset($userGame['memb___id'])) {
        echo json_encode([
            'status' => 0, 
            'message' => 'Tài khoản không tồn tại', 
            'result' => ''
        ], JSON_UNESCAPED_UNICODE);
        exit;
    }
    
	$paymentId = $pdoSqlsrv->lastInsertId();

    $plusMut = floor($money*RATE_WCOIN);
    $moneyPrev = $cashData['WCoinC'];
    $moneyPlus = $cashData['WCoinC'] + $plusMut;

    $bonus = calBonus($plusMut);
    if($bonus > 0) {
        $sqlBonus = 'update '.$tableCash.' set WCoinC = WCoinC + :bonus where AccountID = :AccountID';
        $stmtBonus = $pdoSqlsrv->prepare($sqlBonus);
        $stmtBonus->execute([
            ':bonus' => $bonus,
            ':AccountID' => $accountId
        ]);
    }

    $sqlPayment = 'update '.$tableCash.' set WCoinC = WCoinC + :plusMut where AccountID = :AccountID';
    $stmt = $pdoSqlsrv->prepare($sqlPayment);
    $stmt->execute([
        ':plusMut' => $plusMut,
        ':AccountID' => $accountId
    ]);

	$sqlPayment1 = 'update CustomVIP12 set CASH_VIP12 = CASH_VIP12 + :plusMut where AccountID = :AccountID';
    $stmt = $pdoSqlsrv->prepare($sqlPayment1);
    $stmt->execute([
        ':plusMut' => $plusMut,
        ':AccountID' => $accountId
    ]);
	
	$sqlMoney = 'insert into '.$tableLogMoney.'(accountId, money_prev, money, money_after,TongNapNgay,TongNapTuan,TongNapThang,TongNap,bank_name,created_at) values(
					:accountId, :money_prev, :money, :money_after, :TongNapNgay, :TongNapTuan, :TongNapThang, :TongNap, :bank_name, :created_at)';
    $stmt = $pdoSqlsrv->prepare($sqlMoney);
    $stmt->execute([
       ':accountId' => $accountId,
       ':money_prev' => $moneyPrev,
       ':money' => $plusMut,
       ':money_after' => $moneyPlus,
	   ':TongNapNgay' => $plusMut,
	   ':TongNapTuan' => $plusMut,
	   ':TongNapThang' => $plusMut,
	   ':TongNap' => $plusMut,
	   ':bank_name' => $bankName,
       ':created_at' => time()
   ]);
	
    echo json_encode([
        'status' => 1, 
        'message' => 'Thành công', 
        'result' => ''
    ], JSON_UNESCAPED_UNICODE);
    exit;
}

?>
