<?php

    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json");
    header("Accept: application/json");

    require '../config/conn.php';

    $uId = $_POST['uId'];
    $method = $_POST['method'];
    $methodId = $_POST['methodId'];
    $icon = $_POST['icon'];
    $coins = $_POST['coins'];
    $amount = $_POST['amount'];
    $activity = $_POST['activity'];


    //Decrementing User coins
    $updateUserCoins = "UPDATE `users` SET `coins` = `coins`-'$coins' WHERE `uId` = '$uId'";
    $updateUser = mysqli_query($conn,$updateUserCoins);

    if($updateUser)
    {

        //Creating Playing History
        $createPlayingHistory = "INSERT INTO `playinghistory` (`id`, `uId`, `icon`, `activity`, `coins`, `paid`, `status`, `type`, `date`) VALUES (NULL, '$uId', '$icon', '$activity', '$coins', '1', '1', 'WITHDRAW', current_timestamp());";

        $createHistory = mysqli_query($conn,$createPlayingHistory);

        if($createHistory)
        {
            //Creating WIthdraw Request
            $createWithdrawRequest="INSERT INTO `withdrawrequests` (`id`, `uId`, `method`, `methodId`, `amount`, `coins`, `status`,`requestTime`) VALUES (NULL, '$uId', '$method', '$methodId', '$amount', '$coins', '0',current_timestamp());";
            $createRequest = mysqli_query($conn,$createWithdrawRequest);

            if($createRequest)
            {
                $response['statusCode'] = 0;
                $response['message'] = "Successful";
            }
            else
            {
                $response['statusCode'] = 4;
                $response['message'] = "Error in creating withdraw request";
            }

        }
        else
        {
            $response['statusCode'] = 4;
            $response['message'] = "Error in creating history";    
        }
    }
    else
    {
        $response['statusCode'] = 4;
        $response['message'] = "Error in updating user coins"; 
    }

    echo json_encode($response);


?>