<?php
require_once ("varables.php");

$myJSON['message'] = "500";
$myJSON['kopon'] = array();

if(isset($_POST["an1d"]) && isset($_POST["nekot"]))
{
	
     $now = jdate('Y/m/d');
	$user_id =$_POST["an1d"];
	$toke = $_POST["nekot"];

    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
        @mysqli_query($connect,"SET CHARACTER SET utf8mb4;");
        $query_token = "SELECT id FROM ".$tbl_sessions." WHERE token = '".$toke."' AND uid ='".$user_id."' AND type ='A' LIMIT 1";
		$tokres=@mysqli_query($connect,$query_token);
        if (!$tokres) {
            $myJSON['message']= "400" ;

        }
		else{
			$countoft = 0;
            while($rowt = @mysqli_fetch_assoc($tokres)){
                $countoft++;
			}
			if($countoft == 0){
				$myJSON['message']= "405" ;
			}
			else{
				
				$q = "SELECT tbl_kopon.kpnId,tbl_kopon.kopon FROM tbl_kopon,tbl_kopon_meta WHERE tbl_kopon.kpnId = tbl_kopon_meta.koponid AND tbl_kopon_meta.tag ='expire' AND tbl_kopon_meta.value >= '".$now."' ORDER BY tbl_kopon_meta.value DESC";
				$res=@mysqli_query($connect,$q);
				if(!$res)
					$myJSON['message']= "404" ;
				else{
					$i=0;
					while($row = @mysqli_fetch_assoc($res))
					{
						array_push($myJSON["kopon"], $row);
						$myJSON["kopon"][$i]["meta"] = array();
						$qm  = "SELECT tag,value FROM tbl_kopon_meta WHERE koponid = '".$row["kpnId"]."'";
						$resm=@mysqli_query($connect,$qm);
						if($resm){
							while($rowm = @mysqli_fetch_assoc($resm))
							array_push($myJSON["kopon"][$i]["meta"], $rowm);
						}
						$i++;
					}
					$myJSON['message']= "200" ;
				}
				}	
		}	
	}
	 else
		 $myJSON['message']="402";
}
    echo json_encode($myJSON);
?>
