<?php 

    include "Admin/config/setup.php" ;
    session_start();
    $type = $_POST['type'];
    if($type == 'paid'){
        $player = $_POST['selectPlayer'];
        $cate = $_POST['selectCate'];
        $amount = $_POST['paid_entry'];
        $checked= $_POST['checked'];
        $comm_type=$_POST['comm_type'];
        $comm_amount=$_POST['comm_amount'];
        if($comm_type=='Amount'){
              
            $data['total_amount']=$comm_amount;
        }else{
            $data['total_amount']=((($comm_amount/100) * $amount));
            $comm_amount =$data['total_amount'];

        }
        if(array_search($player,$_SESSION['pid'][$player])){
            if($checked==''){
                unset($_SESSION['pid'][$player]);
            }
            $total = 0;
            foreach($_SESSION['pid'] as $val){
                
                $total += $val['amt'];
            }
            $_SESSION['price'] = $total;
            $total_comm = 0;
            foreach($_SESSION['pid'] as $val){
                
                $total_comm += $val['comm_a'];
            }
            $data = array(
                "player"  => $player,
                "amount"  => $amount,
                "total_comm"  => $total_comm,
                "total" => $total
            );
            echo json_encode($data);
        }else{
            if($checked!=''){
                $_SESSION['pid'][$player] = array('pid' => $player,'amt' => $amount+$comm_amount,'cate' => $cate,'checked'=>$checked,'comm_a'=>$comm_amount);
               }
               $total = 0;
               foreach($_SESSION['pid'] as $val){
                   
                   $total += $val['amt'];
               }
               $total_comm = 0;
                foreach($_SESSION['pid'] as $val){ 
                $total_comm += $val['comm_a'];
               }
               $_SESSION['price'] = $total;
               $data = array(
                "player"  => $player,
                "amount"  => $amount,
                "total_comm"  => $total_comm,
                "total" => $total
              );
            echo json_encode($data);
        }
    }else if($type == 'free'){
        $player = $_POST['selectPlayer'];
        $cate = $_POST['selectCate'];
        $amount = $_POST['paid_entry'];
        $checked= $_POST['checked'];
        $comm_type=$_POST['comm_type'];
        $comm_amount=$_POST['comm_amount'];
        $apply='';
        if(array_search($player,$_SESSION['pid'][$player])){
            $id=$player;

            $apply='1';
            if($checked==''){
                $apply='';
                unset($_SESSION['pid'][$player]);
            }
            if(empty($_SESSION['pid'])){
                $select='0';
               }else{
                $select='1';
               }
            $total = 0;
            foreach($_SESSION['pid'] as $val){
                // if($val['pid']==$player && $val['cate']==$cate){

                // }else{
                    $total += $val['amt'];
                // }
            }
            $total_comm = 0;
            foreach($_SESSION['pid'] as $val){
                
                $total_comm += $val['comm_a'];
            }
            $_SESSION['price'] = $total;
            $data = array(
                "player"  => $player,
                "select"  => $select,
                "apply"  => $apply,
                "total_comm"  => $total_comm,
                "amount"  => '0',
                "total" => $total
            );
            echo json_encode($data);
        }else{
            if($checked!=''){
                $apply='1';
                $_SESSION['pid'][$player] = array('pid' => $player,'amt' => $amount+$comm_amount,'cate' => $cate,'checked'=>$checked,'comm_a'=>$comm_amount);
               }
               if(empty($_SESSION['pid'])){
                $select='0';
               }else{
                $select='1';
               }
              
            $total = 0;
            foreach($_SESSION['pid'] as $val){
                
                $total += $val['amt'];
            }
            $_SESSION['price'] = $total;
            $total_comm = 0;
            foreach($_SESSION['pid'] as $val){
                
                $total_comm += $val['comm_a'];
            }
            $data = array(
                "apply"  => $apply,
                "select"  => $select,
                "player"  => $player,
                "total_comm"  => $total_comm,
                "amount"  => $amount,
                "total" => $total
            );
            echo json_encode($data);
        }
    }else if($type == 'multi'){
        $player = $_POST['selectPlayer'];
        $cate = $_POST['selectCate'];
        $amount = $_POST['paid_entry'];
        $checked= $_POST['checked'];
        $comm_type=$_POST['comm_type'];
        $comm_amount=$_POST['comm_amount'];
        if($comm_type=='Amount'){
            // $data['comm_amt']='('.$row['entry_price'].'+'.$comm_amount.')';
            $data['total_amount']=$comm_amount;
        }else{
            $data['total_amount']=((($comm_amount/100) * $amount));
            // $data['total_amount']=(($comm_amount/100) * $row['entry_price']);
            $comm_amount =$data['total_amount'];

        }
        
        if(array_search($player,$_SESSION['pid'][$player])){

            if($checked==''){
                unset($_SESSION['pid'][$player]);
            }
            $total = 0;
            foreach($_SESSION['pid'] as $val){
                // if($val['pid']==$player && $val['cate']==$cate){

                // }else{
                    $total += $val['amt'];
                // }
            }
            $total_comm = 0;
            foreach($_SESSION['pid'] as $val){
                
                $total_comm += $val['comm_a'];
            }
            $_SESSION['price'] = $total;
            $data = array(
                "player"  => $player,
                "amount"  => $amount,
                "total_comm"  => $total_comm,
                "total" => round($total,2)
            );
            echo json_encode($data);
        }else{
            if($checked!=''){
             $_SESSION['pid'][$player] = array('pid' => $player,'amt' => $amount+$comm_amount,'cate' => $cate,'checked'=>$checked,'comm_a'=>$comm_amount);
            }
            $total = 0;
            foreach($_SESSION['pid'] as $val){
                
                $total += $val['amt'];
            }
            $_SESSION['price']=
            $total_comm = 0;
            foreach($_SESSION['pid'] as $val){
                
                $total_comm += $val['comm_a'];
            }
            $_SESSION['price'] = $total;
            $data = array(
                "player"  => $player,
                "amount"  => $amount,
                "total_comm"  => $total_comm,
                "total" =>  round($total,2)
            );
            echo json_encode($data);
        }
    }

    if($_POST['mulit_id']){
        $config=mysqli_connect('localhost','addmyentry_adentry','AdentrY!234','addmyentry_adentry');
         // Check connection
         if (mysqli_connect_errno()) {
           echo "Failed to connect to MySQL: " . mysqli_connect_error();
         }
        ob_start();
        $mulit_id=$_POST['mulit_id'];
        $comm_type=$_POST['comm_type'];
        $comm_amount=$_POST['comm_amount'];
        $comm_amount=0;
		$sql =$config->query("SELECT * FROM `entry_master` WHERE id='$mulit_id'");
        $row = $sql->fetch_assoc();
        if($comm_type=='Amount'){
            // $data['comm_amt']='('.$row['entry_price'].'+'.$comm_amount.')';
            $data['total_amount']=$row['entry_price']+$comm_amount;
        }else{
            // $data['comm_amt']='('.$row['entry_price'].'+'.(($comm_amount/100) * $row['entry_price']).')';
            $data['total_amount']=((($comm_amount/100) * $row['entry_price'])+$row['entry_price']);
        }
        echo json_encode($data);
    }

    if($_POST['mulit_id_f']){
        $config=mysqli_connect('localhost','addmyentry_adentry','AdentrY!234','addmyentry_adentry');
         // Check connection
         if (mysqli_connect_errno()) {
           echo "Failed to connect to MySQL: " . mysqli_connect_error();
         }
        ob_start();
        $mulit_id=$_POST['mulit_id_f'];
        $comm_type=$_POST['comm_type'];
        $comm_amount=$_POST['comm_amount'];
        $comm_amount=0;
		$sql =$config->query("SELECT * FROM `entry_master` WHERE id='$mulit_id'");
        $row = $sql->fetch_assoc();
        if($comm_type=='Amount'){
            // $data['comm_amt']='('.$row['entry_price'].'+'.$comm_amount.')';
            $data['total_amount']=$row['entry_price']+$comm_amount;
        }elseif($comm_type=='Percentage'){
            // $data['comm_amt']='('.$row['entry_price'].'+'.(($comm_amount/100) * $row['entry_price']).')';
            $data['total_amount']=((($comm_amount/100) * $row['entry_price'])+$row['entry_price']);
        }else{
            $data['total_amount']='0';
        }
        echo json_encode($data);
    }
    if($_POST['mulit_id_p']){
        $config=mysqli_connect('localhost','addmyentry_adentry','AdentrY!234','addmyentry_adentry');
         // Check connection
         if (mysqli_connect_errno()) {
           echo "Failed to connect to MySQL: " . mysqli_connect_error();
         }
        ob_start();
        $mulit_id=$_POST['mulit_id_p'];
        $comm_type=$_POST['comm_type'];
        $comm_amount=$_POST['comm_amount'];
        $comm_amount=0;
		$sql =$config->query("SELECT * FROM `entry_master` WHERE id='$mulit_id'");
        $row = $sql->fetch_assoc();
        if($comm_type=='Amount'){
            // $data['comm_amt']='('.$row['entry_price'].'+'.$comm_amount.')';
            $data['total_amount']=$row['entry_price']+$comm_amount;
        }elseif($comm_type=='Percentage'){
            // $data['comm_amt']='('.$row['entry_price'].'+'.(($comm_amount/100) * $row['entry_price']).')';
            $data['total_amount']=((($comm_amount/100) * $row['entry_price'])+$row['entry_price']);
        }else{
            $data['total_amount']='0';
        }
        echo json_encode($data);
    }
?>
