<?php
include "../connection/config.php";
extract($_REQUEST);

//number   multipule
//amount   multipule    
//bazar
//total
//game
//mobile

    $date = date('d/m/Y');

    $get_mrkt =  query("select * from gametime_manual where market='$bazar'");
    $xc = fetch($get_mrkt);
    
if($xc['days'] == "ALL" || substr_count($xc['days'],$day) == 0){
    
        if(strtotime($time)<strtotime($xc['open']))
        {
            $xc['is_open'] = "1";
        }
        else
        {
            $xc['is_open'] = "0";
        }
        
        if(strtotime($time)<strtotime($xc['close']))
        {
            $xc['is_close'] = "1";
        }
        else
        {
            $xc['is_close'] = "0";
        }
    
} else if(substr_count($xc['days'],$day."(CLOSE)") > 0){
       
       
        $data['success'] = "0";
        $data['msg'] = "Market already closeed, Try again later";
        echo json_encode($data);
        return;
       
       
} else {
        $time_array = explode(",",$xc['days']);
        for($i =0;$i< count($time_array);$i++){
            if(substr_count($time_array[$i],$day) > 0){
                $day_conf = $time_array[$i];
            }
        }
        
        $day_conf = str_replace($day."(","",$day_conf);
        $day_conf = str_replace(")","",$day_conf);
        
        $mrk_time = explode("-",$day_conf);
        
        $xc['open'] = $mrk_time[0];
        $xc['close'] = $mrk_time[1];
        
        if(strtotime($time)<strtotime($mrk_time[0]))
        {
            $xc['is_open'] = "1";
        }
        else
        {
            $xc['is_open'] = "0";
        }
        
        if(strtotime($time)<strtotime($mrk_time[1]))
        {
            $xc['is_close'] = "1";
        }
        else
        {
            $xc['is_close'] = "0";
        }
}
  
  if($game == "halfsangam" || $game == "fullsangam"){
    
    if($xc['is_open'] == "0"){
      $data['success'] = "0";
                $data['msg'] = "Game already closed";
                echo json_encode($data);
                return;
    }
  }

$check = query("select wallet from users where mobile='$mobile' AND wallet < '$total'");

if(rows($check) == 0)
{
    
    query("update users set wallet=wallet-'$total' where mobile='$mobile'");
    
    $bazar = str_replace(" ","_",$bazar);
    
    $nm = explode(",",$number);
    $am = explode(",",$amount); 
    for($i=0;$i<count($nm);$i++)
      {
        query("INSERT INTO `games`(`user`, `game`, `bazar`, `date`, `number`, `amount`, `created_at`) VALUES ('$mobile','$game','$bazar','$date','$nm[$i]','$am[$i]','$stamp')");
        query("INSERT INTO `transactions`(`user`, `amount`, `type`, `remark`, `created_at`,`owner`) VALUES ('$mobile','$am[$i]','0','Sangam Placed','$stamp','$mobile')"); 
      }
    
    $data['success'] = "1";

}
else
{
    
    $data['success'] = "0";
    $data['msg'] = "You don't have enough wallet balance";
}

echo json_encode($data);