<?php
    include "Admin/config/setup.php" ;
    session_start();
    $uid = $_SESSION['user_id'] ;
    if(isset($_GET['apply']) && !empty($_GET['apply'])){
        $eid = $_GET['apply'];
        $entry_sql = $config->query("SELECT * FROM event_post WHERE event_id = '$eid' ");
        $entry_res = $entry_sql->fetch_assoc();
        $entry = $entry_res['entry'];
        if($entry == 'paid'){
           $price_id = $_SESSION['pid'];
            $price = 0;
            foreach($price_id as $val){
                $price += $val['amt'];
            }
        }else if($entry == 'multi'){
            $price_id = $_SESSION['pid'];
            $price = 0;
            foreach($price_id as $val){
                $price += $val['amt'];
            }
        }else if($entry == 'free'){
            $price = 0;
        }
        $chksql = $config->query("SELECT * FROM players WHERE user_id = '$uid' " );
        
        $subsql = "SELECT * FROM ewallet_master WHERE user_id = '$uid' " ;
        $result = $config->query($subsql);
        $subrow = $result->fetch_assoc();
        $wallet = $subrow['total_amount'] - $price ;
        $created_at = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ewallet_master SET total_amount = '$wallet' WHERE user_id = '$uid' " ;
       
           $sql1 = "INSERT INTO buypack_master(user_id, transaction_id, purpose, event_id, buy_amount,payment_mode,buy_on, buy_status) VALUES('$uid', 'Wallet', 'Event', '$eid', '$price', 'Wallet','$created_at', 2)";
           $config->query($sql1);
          $insert_id1 = $config->insert_id;
        if($config->query($sql) === TRUE){
            foreach($chksql as $chk){
                $ply_id = $chk['id'];
                if(array_search($ply_id,$_SESSION['pid'][$ply_id])){
                    if($entry == 'paid'){
                        $comm_a = $_SESSION['pid'][$ply_id]['comm_a'];
                        $entry_fees = $_SESSION['pid'][$ply_id]['amt'];
                        $entry_type = $_SESSION['pid'][$ply_id]['cate'];
                        $sql = $config->query("INSERT INTO apply_request(user_id, event_id, player_id, entry,entry_type, entry_fees, comm_amt, apply_status,pay_id)
                        VALUES('$uid', '$eid', '$ply_id','$entry','$entry_type', '$entry_fees','$comm_a', 1,'$insert_id1') ");
                    }else if($entry == 'multi'){
                        $comm_a = $_SESSION['pid'][$ply_id]['comm_a'];
                        $entry_fees = $_SESSION['pid'][$ply_id]['amt'];
                        $entry_type = $_SESSION['pid'][$ply_id]['cate'];
                        $sql = $config->query("INSERT INTO apply_request(user_id, event_id, player_id, entry, entry_type, entry_fees, comm_amt,apply_status,pay_id)
                        VALUES('$uid', '$eid', '$ply_id','$entry', '$entry_type', '$entry_fees','$comm_a' ,1,'$insert_id1') ");
                    }else if($entry == 'free'){
                        $entry_type = $_SESSION['pid'][$ply_id]['cate'];
                        $comm_a = $_SESSION['pid'][$ply_id]['comm_a'];
                        $entry_fees = $_SESSION['pid'][$ply_id]['amt'];
                        $sql = $config->query("INSERT INTO apply_request(user_id, event_id, player_id, entry,entry_type, entry_fees,comm_amt, apply_status,pay_id)
                        VALUES('$uid', '$eid', '$ply_id', '$entry','$entry_type','$entry_fees','$comm_a', 1,'$insert_id1') ");
                    }
                }
            }
            $_SESSION['check'] = "Applied Successfully";
            $_SESSION['alert'] = "success" ;
            unset($_SESSION['price']);
            unset($_SESSION['pid']);
            header('Location:tournaments.php');
        }else{
            $_SESSION['check'] = "Failed" ;
            $_SESSION['alert'] = "error" ;
        }
    }

?>