<?php include('../config.php');

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $game_name = mysqli_real_escape_string($con,$_POST['game_name']);
    $date = mysqli_real_escape_string($con,$_POST['date']);
    $digit = mysqli_real_escape_string($con,$_POST['digit']);
  
    $check=mysqli_query($con, "select * from gali_desawar_result_chart where game_name= '$game_name' and date='$date' ");
    if(mysqli_num_rows($check)>0){
        $check_open=mysqli_query($con, "select * from gali_desawar_result_chart where game_name= '$game_name' and date='$date' and digit!=''");
        if(mysqli_num_rows($check_open)>0){
            echo '<div class="alert alert-danger">
                <strong>Failed!</strong> Result Already Declared.
              </div>';
            ?>
            <script>
                alert('Result Already Declared');
                window.location='../gali-desawar-declare-result.php';
            </script>
            <?php
        }else{
            $update= mysqli_query($con,"update gali_desawar_result_chart set digit='$digit' where game_name='$game_name' and date='$date'");
        }
    }else{
        $insert=mysqli_query($con,"INSERT INTO `gali_desawar_result_chart`(`game_name`, `date`, `digit`, `status`) VALUES ('$game_name','$date','$digit','1')");
    }
    
    if($insert){
        $left_digit =  substr($digit,0,1);
        $right_digit = substr($digit,1,1);

        $left_digit_winner = mysqli_query($con,"SELECT * FROM gali_desawar_bid_history WHERE game_name='$game_name' AND game_type='Left Digit' AND date = '$date' AND digit = '$left_digit'");
        $right_digit_winner = mysqli_query($con,"SELECT * FROM gali_desawar_bid_history WHERE game_name='$game_name' AND game_type='Right Digit' AND date = '$date' AND digit = '$right_digit'");
        $jodi_digit_winner = mysqli_query($con,"SELECT * FROM gali_desawar_bid_history WHERE game_name='$game_name' AND game_type='Jodi Digit' AND date = '$date' AND digit = '$digit'");
        
        while($select_open_pana = mysqli_fetch_array($left_digit_winner)){
            $username = $select_open_pana['username'];
            $game_type = $select_open_pana['game_type'];
            $points_action = $select_open_pana['points_action'];
            $select_win = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM gali_desawar_game_rates WHERE type='$game_type'"));
    						                    $win_amount = round(($select_win['max_value']/$select_win['min_value'])*$points_action);
            $insert_result = mysqli_query($con,"INSERT INTO `gali_desawar_winning_report`(`date`, `username`, `game_name`, `game_type`, `digit`, `winning_points`, `points_action`) VALUES ('$date','$username','$game_name','$game_type','$left_digit','$win_amount','$points_action')");
            if($insert_result){
                $get_wallet = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM user_info WHERE phone='$username'"));
                $wallet = $get_wallet['wallet'];
                $new_wallet = $win_amount + $wallet;
                $update_wallet = mysqli_query($con,"UPDATE user_info SET wallet='$new_wallet' WHERE phone='$username'");
                $today = date('Y-m-d');
                $time = date("H:i:s");
                $transaction = mysqli_query($con,"INSERT INTO `wallet_history`(`status`, `date`, `time`, `amount`, `updated_amount`, `remark`, `phone_number`) VALUES ('1','$today','$time','$win_amount','$new_wallet','Gali Desawar Left Digit Winning Reward','$username')");
            }else{
                echo "1" . mysqli_error($con);
                die();
            }
        }

        while($select_open_pana = mysqli_fetch_array($right_digit_winner)){
            $username = $select_open_pana['username'];
            $game_type = $select_open_pana['game_type'];
            $points_action = $select_open_pana['points_action'];
            $select_win = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM gali_desawar_game_rates WHERE type='$game_type'"));
    						                    $win_amount = round(($select_win['max_value']/$select_win['min_value'])*$points_action);
            $insert_result = mysqli_query($con,"INSERT INTO `gali_desawar_winning_report`(`date`, `username`, `game_name`, `game_type`, `digit`, `winning_points`, `points_action`) VALUES ('$date','$username','$game_name','$game_type','$right_digit','$win_amount','$points_action')");
            if($insert_result){
                $get_wallet = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM user_info WHERE phone='$username'"));
                $wallet = $get_wallet['wallet'];
                $new_wallet = $win_amount + $wallet;
                $update_wallet = mysqli_query($con,"UPDATE user_info SET wallet='$new_wallet' WHERE phone='$username'");
                $today = date('Y-m-d');
                $time = date("H:i:s");
                $transaction = mysqli_query($con,"INSERT INTO `wallet_history`(`status`, `date`, `time`, `amount`, `updated_amount`, `remark`, `phone_number`) VALUES ('1','$today','$time','$win_amount','$new_wallet','Gali Desawar Right Digit Winning Reward','$username')");
            }else{
                echo "1" . mysqli_error($con);
                die();
            }
        }

        while($select_open_pana = mysqli_fetch_array($jodi_digit_winner)){
            $username = $select_open_pana['username'];
            $game_type = $select_open_pana['game_type'];
            $points_action = $select_open_pana['points_action'];
            $select_win = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM gali_desawar_game_rates WHERE type='$game_type'"));
    						                    $win_amount = round(($select_win['max_value']/$select_win['min_value'])*$points_action);
            $insert_result = mysqli_query($con,"INSERT INTO `gali_desawar_winning_report`(`date`, `username`, `game_name`, `game_type`, `digit`, `winning_points`, `points_action`) VALUES ('$date','$username','$game_name','$game_type','$digit','$win_amount','$points_action')");
            if($insert_result){
                $get_wallet = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM user_info WHERE phone='$username'"));
                $wallet = $get_wallet['wallet'];
                $new_wallet = $win_amount + $wallet;
                $update_wallet = mysqli_query($con,"UPDATE user_info SET wallet='$new_wallet' WHERE phone='$username'");
                $today = date('Y-m-d');
                $time = date("H:i:s");
                $transaction = mysqli_query($con,"INSERT INTO `wallet_history`(`status`, `date`, `time`, `amount`, `updated_amount`, `remark`, `phone_number`) VALUES ('1','$today','$time','$win_amount','$new_wallet','Gali Desawar Jodi Digit Winning Reward','$username')");
            }else{
                echo "1" . mysqli_error($con);
                die();
            }
        }
     
    }else{
        echo mysqli_error($con);
    }
echo '<div class="alert alert-success">
    <strong>Success!</strong> Result Declared Successfully.
  </div>';
}
?>