<?php 	
    include_once('../../config.php');     
     
     die;
  
    $ExplR[0] = 4;
    $ExplR[1] = 7;
    $ExplR[2] = 8;
    $ExplR[3] = 11;
    $ExplR[4] = 12;
    $ExplR[5] = 15;
    $ExplR[6] = 22;
    $ExplR[7] = 23;
    $ExplR[8] = 9;
    $ExplR[9] = 16;
    $ExplR[10] = 12;
    $ExplR[11] = 13;
    
    $postdata = $_REQUEST['postdata'];
    mysql_query("insert into  test (name) values('$postdata') ");   
	 		
    $BazarRecords =  json_decode($postdata); 
    $_POST['OpenAnk'] = $BazarRecords->OpenAnk;
    $_POST['OpenPatti'] = $BazarRecords->OpenPatti;
    $_POST['CloseAnk'] = $BazarRecords->CloseAnk;
    $_POST['ClosePatti'] = $BazarRecords->ClosePatti;
    $_REQUEST['id']  = $BazarRecords->BazarId;
    $_POST['datepicker']  = $BazarRecords->datepicker;
    
    
    if(in_array($BazarRecords->BazarId, $ExplR))
    {
        if( ($_POST['OpenAnk']!='') || ($_POST['CloseAnk']!='') )
		{ 
	        if($_POST['OpenAnk']!='')
			{
				$gametype =  1;
				$Ank =  $_POST['OpenAnk'];
			}
			else 
			{
				$gametype =  2;
				$Ank =  $_POST['CloseAnk'];
			}
			
			 
	 		$freeGaMeZone = mysql_fetch_object(mysql_query("select * from freeGaMeZone   where bazarId = $_REQUEST[id]")); 
			$ExplR1 = explode("-",$freeGaMeZone->value1);
			$ExplP = explode("-",$freeGaMeZone->value2);
			$ExplJ = explode("-",$freeGaMeZone->value3);
			
			$found= 0;
			$foundp= 0;
			$foundj= 0;
            if($gametype==1)
            {
                if (in_array($_POST['OpenAnk'], $ExplR1))
                     $found= 1;
                     $digitP =  $_POST['OpenAnk'];
                     
                if (in_array($_POST['OpenPatti'], $ExplP))
                    $foundp = 1;
                    
                    $Patti =  $_POST['OpenPatti'];  
                
            }else
            {
                if (in_array($_POST['CloseAnk'], $ExplR1))
                     $found= 1;
                     $digitP =  $_POST['CloseAnk'];
                     
                    if (in_array($_POST['ClosePatti'], $ExplP))
                     $foundp = 1;
                    
                    $Patti =  $_POST['ClosePatti'];  
                    
                    
                         
                    $check_in =  date('Y-m-d'); 
                    $ExpBData1 = mysql_fetch_object(mysql_query("select * from expandBazzar where fdate = '$check_in' and bazzarId = $_REQUEST[id]  ")); 
                    $Jval2 =  $ExpBData1->val2."".$_POST['CloseAnk'];
                    
                    if (in_array($Jval2, $ExplP))
                    {
                        $foundj = 1;   
                        mysql_query("update  expandBazzar set passjodi  = '$Jval2'  where   bazzarId = $_REQUEST[id]  ");  
                    }
            }
            
            
            $tdate = date("Y-m-d");
            if( ($found==1))
                mysql_query("update  freeGaMeZone set    date_p='$tdate', status = 1, type=$gametype , digit =$digitP   where bazarId = $_REQUEST[id]");      
            else
                mysql_query("update  freeGaMeZone set    date_p='$tdate', status = 0, type=$gametype , digit =$digitP   where bazarId = $_REQUEST[id]");     
		 
		    if( ($foundp==1))
                mysql_query("update  freeGaMeZone set     panna = 1, type=$gametype , passPana ='$Patti'   where bazarId = $_REQUEST[id]");      
            else
                mysql_query("update  freeGaMeZone set    panna = 0, type=$gametype , passPana ='$Patti'   where bazarId = $_REQUEST[id]");     
		 
		 
			 
		    if(($foundj==1))
                mysql_query("update  freeGaMeZone set   $foundj = 1   where bazarId = $_REQUEST[id]");      
            else
                mysql_query("update  freeGaMeZone set   $foundj = 0    where bazarId = $_REQUEST[id]");   
		    
		    
			$datepicker = $_POST['datepicker'];  
			mysql_query("update  biddings set   wonFor = 1 where gametype = $gametype and  BidNo = $Ank  and optionType = 1  and adminDate='$datepicker'  and bazarId = $_REQUEST[id]");    
			mysql_query("insert into updatedValues (gametype,Fdate,BidNo,bazarId,optionType)    values($gametype,'$datepicker',$Ank,$_REQUEST[id],1) ");   
	 		
 				$resultData = mysql_query("select * from   biddings   where wonFor = 1 and BidNo = $Ank  and optionType = 1  and adminDate='$datepicker' and gametype = $gametype "); 
				while($rw = mysql_fetch_object($resultData))
				{
					$valUe = (int)$MarketRate->singleAnk * (int)$rw->Points;     
					mysql_query("update  wallet  set   amount = amount + $valUe  where  custId = '$rw->custId' ");      
				} 
				
			/// Jodi   
			if($gametype ==2)
			{
			 
				$resultData1 = mysql_query("select * from  updatedValues where   optionType = 1  and Fdate='$datepicker'   "); 
				while($rw = mysql_fetch_object($resultData1))
						$MyAnk .= $rw->BidNo;  
			 
				$resultData1 = mysql_query("select * from   biddings where   BidNo = $MyAnk  and optionType = 2 and adminDate='$datepicker'  and bazarId = $_REQUEST[id]  "); 
				$ky = 0;
				while($rw = mysql_fetch_object($resultData1))
				{
					$MYid1[$ky]['id'] = $rw->id;
					$MYid1[$ky]['custId'] = $rw->custId;
					$MYid1[$ky]['points'] = $rw->Points;
					$ky++;
				}
				 
				foreach($MYid1 as $dtm)
				{
					$Id = 	$dtm['id'];
					$custId = 	$dtm['custId'];							 
					$Points = 	$dtm['points'];	
					
					mysql_query("update  biddings set   wonFor = 1 where id=$Id");   
					$valUe = (int)$MarketRate->jodi * (int)$Points;     
					mysql_query("update  wallet  set   amount = amount + $valUe  where  custId = '$custId' ");     
				} 
			}

			
	
			$check_in =  date('Y-m-d');  	
			if($gametype==1) {
				mysql_query("insert into expandBazzar (fdate,bazzarId,type,adminId) values('$check_in',$_REQUEST[id],2,1) "); 
				$mysql_insert_id = mysql_insert_id();
			}

			
		}  
			
				if( ($_POST['OpenPatti']!='') || ($_POST['ClosePatti']!='') )
				{  
					if($_POST['OpenPatti']!='')
					{
						$gametype =  1;
						$Ank =  $_POST['OpenPatti'];
					}
					else 
					{
						$gametype =  2;
						$Ank =  $_POST['ClosePatti'];
					}
					
    					
    			    $str = $Ank;
                    $Pt = 1;
                    if( ($str[0] == $str[1]) && ($str[1]==$str[2]) )
                        $Pt = 3;
                     else if( ($str[0] == $str[1]) )
                        $Pt = 2;
                    else if( ($str[1] == $str[2]) )
                        $Pt = 2; 
					
					$datepicker = $_POST['datepicker'];     
    				mysql_query("update  biddings set   wonFor = 1 where gametype = $gametype and  BidNo = $Ank  and optionType = 3  and adminDate='$datepicker'  and bazarId = $_REQUEST[id]");    
					mysql_query("insert into updatedValues (gametype,Fdate,BidNo,bazarId,optionType)    values($gametype,'$datepicker',$Ank,$_REQUEST[id],3) ");     
					 
    	 				$resultData = mysql_query("select * from   biddings   where wonFor = 1 and BidNo = $Ank  and optionType = 3  and adminDate='$datepicker' and gametype = $gametype "); 
    					while($rw = mysql_fetch_object($resultData))
    					{
    						if($Pt==1)
    						    $valUe = (int)$MarketRate->patti * (int)$rw->Points;    
    						else if($Pt==2)
    						    $valUe = (int)$MarketRate->doublePatti * (int)$rw->Points;    
    						else if($Pt==3)
    						    $valUe = (int)$MarketRate->triplePatti * (int)$rw->Points;    
    						    
    						mysql_query("update  wallet  set   amount = amount + $valUe  where  custId = '$rw->custId' ");      
    					}  


					$check_in =  date('Y-m-d');     
                    $old_dateP = $_POST['datepicker'];
                    $old_date_timestamp = strtotime($old_dateP);
                    $GameDateShow =  date('Y-m-d', $old_date_timestamp); 
					
					if($gametype==1)  
					{ 
						$AllVal = $Ank."-".$_POST['OpenAnk'];
						$loadingDate = date("Y-m-d");
						mysql_query("update  live_result set value='$AllVal',loadingDate = '$loadingDate' where bazzarId  = $_REQUEST[id]  ");
						//mysql_query("update  live_result set value='$AllVal' where bazzarId  = $_REQUEST[id]  ");
						mysql_query("update    expandBazzar  set val1 = '$Ank' , val2 = '$_POST[OpenAnk]'  where id  = $mysql_insert_id "); 
					}else if($gametype==2)
					{   
						$ExpBData = mysql_fetch_object(mysql_query("select * from expandBazzar where fdate = '$GameDateShow' and bazzarId = $_REQUEST[id]  ")); 
						$val2 =  $ExpBData->val2."".$_POST['CloseAnk'];  
						$AllVal =  $ExpBData->val1."-".$ExpBData->val2."".$_POST['CloseAnk']."-".$Ank;
						$loadingDate = date("Y-m-d");
						mysql_query("update  live_result set value='$AllVal'  where bazzarId  = $_REQUEST[id]  ");
						mysql_query("update  expandBazzar  set val2 = '$val2' , val3 = '$Ank'  where fdate = '$GameDateShow' and bazzarId = $_REQUEST[id]");  
					}						
			
				}
    }