<?php
    require_once 'check_security.php';
$showjoin=false;
if(isset($_GET['join']) or isset($_POST['join']) ){
    if(isset($_GET['join']) ){
$matchid=$_GET['join'];
    }else if(isset($_POST['join']) ){
        $matchid=$_POST['join'];
    }else{
        header("Location: /fairb/"); 
    }

    $m_id=$matchid;
    $joinmatch = mysqli_query($con,"SELECT * FROM matches WHERE m_id = '$m_id and match_status='1'");
    
    
    
    
    
    
    
    $arra=array();
while($row=mysqli_fetch_assoc($joinmatch)){
    $arra[]=$row;
}

if(sizeof($arra)<=0){
  header("Location: /fairb/");
}else{
    
    
  
      $joncomatjo = mysqli_query($conn,"SELECT * FROM match_join_meber WHERE member_id = '$id' and match_id='$matchid");
          $oncomarra=array();
      while($rowson=mysqli_fetch_assoc($joncomatjo)){
          $oncomarra[]=$rowson;
            $joinhh="";
      }
      if(sizeof($oncomarra)<=0){
     
     $endryfee=$arra[0]["entry_fee"];    
if($arra[0]["no_of_player"]==$arra[0]["total_player"]){
    $showjoin=false;
    $titlepo="Contest Full";
    $subpo="The Contest Full Please Try Another Contest";
  
}else{
    
    $showjoin=true;
    
   
    
    if(isset($_POST['yes'])){
        
    $gameid=$_POST['gmaeid'];
   $sqa=$_POST['sqa'];
   $joincode=$_POST['join'];
   
   
   $gameid=mysqli_real_escape_string($conn,$gameid);
   $sqa=mysqli_real_escape_string($conn,$sqa);
   $joincode=mysqli_real_escape_string($conn,$joincode);
   if($referby=="no"){
   
   }else{
   $jujvuyfuygiu = mysqli_query($conn,"SELECT * FROM userdataa WHERE refercode = '$referby'");
          while($rowfgcjgnhf=mysqli_fetch_assoc($jujvuyfuygiu)){
              $referbanbfv=$rowfgcjgnhf["addmoney"]+5;
          }
   }
   
$date = date('d/m/Y_H:i:s');
$totalmycg=$addmoney+$winmoney;
$totalmatchh=$total_match+1;
   $ooojkbjhyfyuo = mysqli_query($conn,"SELECT * FROM matches WHERE m_id = '$matchid'");
          while($rowsssjuuhius=mysqli_fetch_assoc($ooojkbjhyfyuo)){
              $newmabedr=$rowsssjuuhius["no_of_player"];
              $newmaber=$newmabedr+1;
          }
if($totalmycg>=$endryfee){
    if($addmoney>=$endryfee){
        if($referby=="no"){
        }else{
         if($total_match==0){
              if($endryfee<6){
                 
             }else{
             $balquery=mysqli_query($conn,"UPDATE  userdataa Set addmoney='$referbanbfv' WHERE refercode='$referby'");
             }
         }
        }
        $newbalancdf=$addmoney-$endryfee;
        $balquery=mysqli_query($conn,"UPDATE  userdataa Set addmoney='$newbalancdf' ,total_match='$totalmatchh' WHERE id='$id'");
        

      
         $transquary=mysqli_query($conn,"INSERT INTO history_trans (type,title,time,amount,oldamount,status,visble,member_id) Values ('2','1','$date','$endryfee','$totalmycg','0','0','$id')");
           if($balquery){
                $query=mysqli_query($conn,"INSERT INTO match_join_member (match_id,member_id,pubg_id,team,position,place,place_point,killed,win,win_prize,bonus,total_win,refund,entry_from,date_craeted) Values('$joincode','$id','$gameid','$sqa','0','0','0','0','0','0','0','0','0','2','$date')");
                   $matchghdupbalquery=mysqli_query($conn,"UPDATE  matches Set no_of_player='$newmaber' WHERE  m_id = '$matchid'");
                  
                   
   if($query){
        header("Location: /fairb/joinmatch.php?join=$joincode&vvvv=joined");
   }else{
       echo "oooo";
   }
           }
        
    }else{
         if($referby=="no"){
        }else{
         if($total_match==0){
             if($endryfee<6){
                 
             }else{
             $balquery=mysqli_query($conn,"UPDATE  userdataa Set addmoney='$referbanbfv' WHERE refercode='$referby'");
             }
              
         }
        }
        $newbalancdf=$totalmycg-$endryfee;
         $balquery=mysqli_query($conn,"UPDATE  userdataa Set addmoney='0',winmoney='$newbalancdf' ,Totalplay='$totalmatchh' WHERE id='$id'");
          $matchghdupbalquery=mysqli_query($conn,"UPDATE  matches Set no_of_player='$newmaber' WHERE  m_id = '$matchid'");
           $transquary=mysqli_query($conn,"INSERT INTO history_trans (type,title,time,amount,oldamount,status,visble,member_id) Values ('2','1','$date','$endryfee','$totalmycg','0','0','$id')");
           if($balquery){
                $query=mysqli_query($conn,"INSERT INTO match_join_member (match_id,member_id,pubg_id,team,position,place,place_point,killed,win,win_prize,bonus,total_win,refund,entry_from,date_craeted) Values('$joincode','$id','$gameid','$sqa','0','0','0','0','0','0','0','0','0','2','$date')");
   if($query){
        header("Location: /fairb/joinmatch.php?join=$joincode&vvvv=joined");
   }else{
       echo "oooo";
   }
           }
    }
    
}else{
     $showjoin=false;
    $titlepo="Insuficent Balance";
    $subpo="Please Add Money To Join Contest";   
}
  
   

   
    }
}
     
     
      }else{
           if(isset($_GET['vvvv'])){
            $showjoin=false;
    $titlepo="Join SuccessFully";
    $subpo="You Joined this Contest  SuccessFully";   
           }else{
   $showjoin=false;
    $titlepo="You Allready Joined";
    $subpo="You Allready Joined this Contest";
           }
 
       
      }
    
  
    
    
    
    
    

  
}



}else{
    header("Location: /fairb/");
}


    ?>
    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Join MAtch</title>
</head>

<style>

body{
  background-color: aliceblue;
  display: block;
}
*{
  padding: 0;
margin: 0;

box-sizing: border-box;
font-family: Canva Sans,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;;
text-decoration: none;

}
div.bodycontener div.joindeatils{
  text-align: center;
 width: 100%;
 height: 100%;
 align-items: center;
}
div.containerjoin{
  text-align: center;
margin-top: 10%;



}
.form-container h2{
    margin:5px;
}
div.bodycontener div.joinsucesssfully{
  display: block;
width: max-content;
padding: 10px;
align-self: center;
text-align: center;
box-shadow: 0px 5px 10px cadetblue;
border: 1px solid #ccc;
border-radius: 5px;
margin-top: 10%;
background-color: white;
}
div.bodycontener div.joinsucesssfully img{
  width: 40px;
  height: 40px;
  margin: 5px;
}
div.bodycontener div.joinsucesssfully h2{
  margin: 5px;
}
div.bodycontener div.joinsucesssfully p{
  margin: 5px;
}
div.bodycontener div.joinsucesssfully button{
 


box-shadow: 0px 5px 10px cadetblue;
border: 1px solid #ccc;
border-radius: 5px;
padding-top: 5px;
padding-bottom: 5px;
padding-left: 30px;
margin: 5px;
font-size: 18px;
color: white;
padding-right: 30px;
background-color: rgb(69, 29, 226);
}
.form-container {
  max-width: 300px;
  padding: 10px;
  align-self: center;
  box-shadow: 10px;
display: block;


box-shadow: 0px 5px 10px cadetblue;
border: 1px solid #ccc;
border-radius: 5px;

background-color: white;
}
.form-container button{
color: wheat;
font-size: 19px;
padding-left: 30px;
padding-right: 30px;
box-shadow: 10px;
display: block;

padding-top: 5px;
padding-bottom: 5px;
margin: 5px;
box-shadow: 0px 5px 10px cadetblue;
border: 1px solid #ccc;
border-radius: 5px;

background-color: rgb(72, 15, 246);
}
/* Full-width input fields */
.form-container input[type=text], .form-container input[type=password] {
  width: 100%;
  padding: 10px;
  margin: 5px 0 22px 0;
  border: none;
  
border: 2px solid #ccc;
  border-radius: 5px;
  text-align: center;
  background: #f1f1f1;
}

/* When the inputs get focus, do something */
.form-container input[type=text]:focus, .form-container input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}
</style>
<body>
    <div class="bodycontener">
       <?php     if($showjoin==true){
           ?>
           
            <div class="joindeatils">
            <div class="containerjoin">
              <center> <center>
               
                <form  class="form-container" method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">
                    <center>
                  <h2>Join In ₹ <?php echo $endryfee; ?></h2>
              </center>
                  <label for="email"><b>Game Account Name</b></label>
                  <input type="text" placeholder="Enter Game Account Name" name="gmaeid" required>
              
                  <label for="psw"><b>Squard Code</b></label>
                  <input type="text" placeholder="Enter Squard Code ( Optional )" name="sqa" >
                  
                
                    <input type="text" style="display: none;" id="jointt" title="gfhgsa" value="<?php echo $matchid; ?>" name="join"  required >
              
                  <button name="yes" type="submit" value="1" class="btn" id="btnn">Submit</button>
                  <a href="/fairb/" id="btnh" style="display: none;"></a>
                  <button name="yes" type="button"  value="0" onclick="openclose()"  class="btncancel">Close<a style="color:  rgb(72, 15, 246);">_.</a> </button>
                </form>
              </center></center>
            </div>
        </div>
           
       <?php }else{ ?>
        <center> <center>
            
        <div class="joinsucesssfully">
<img src="https://fairbattle.earnmoreapp.in/adminnh/assist/icon/cash.png" />
<h2><?php echo $titlepo; ?></h2>
<p><?php echo $subpo; ?></p>
 <a href="/fairb/" id="btnh" style="display: none;"></a>
<button onclick="openclose()" >Ok</button>
        </div>
      </center> </center>
       <?php }  ?>
       
    </div>
    <script>
function openclose(){
  console.log("ghvhg");
  var sub=document.getElementById("btnh");
  sub.click();
}

    </script>
</body>
</html>
    

    

    