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

$myJSON['message'] = "4004";
$myJSON['INSIGHTS'] = array();
 $myJSON['INSIGHTS'] = array();
if(isset($_POST["nekot"]) && isset($_POST["an1d"]))
{

    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
        @mysqli_query($connect,"SET CHARACTER SET utf8;");

        $query_all = "SELECT * FROM tbl_androidid";
        $allres = @mysqli_query($connect,$query_all);
        if($allres){
          $countins = 0;
         while($row = @mysqli_fetch_assoc($allres))
            $countins++;
             $myJSON['INSIGHTS']["all_ins"] = $countins;
        }
            $date = jdate('Y/m/d');
           $date1 = remMounths($date,1,true); 
           $firstbazefrom = addNumbers($date1,23,true);
           $firstbazeto= addNumbers($date1,30,true);
           $secondbazefrom = addNumbers($date1,15,true);
           $secondbazeto = addNumbers($date1,22,true);  

            $myJSON['INSIGHTS']["firstbazefrom"] =  $firstbazefrom;
            $myJSON['INSIGHTS']["firstbazeto"] =  $firstbazeto;
            $myJSON['INSIGHTS']["secondbazefrom"] =  $secondbazefrom;
            $myJSON['INSIGHTS']["secondbazeto"] =  $secondbazeto;

           $query_all = "SELECT * FROM tbl_androidid WHERE firstdate BETWEEN '".$firstbazefrom."' AND '".$firstbazeto."'";
           $allres = @mysqli_query($connect,$query_all);
           if($allres){
             $countactivity = 0;
            while($row = @mysqli_fetch_assoc($allres)){
               $countactivity++;
            }
  
                $myJSON['INSIGHTS']["baze_1_news"] = $countactivity;
           }
               $query_all = "SELECT * FROM tbl_androidid WHERE firstdate BETWEEN '".$secondbazefrom."' AND '".$secondbazeto."'";
               $allres = @mysqli_query($connect,$query_all);
               if($allres){
                 $countactivity = 0;
                while($row = @mysqli_fetch_assoc($allres)){
                  $countactivity++;
               }
            
                    $myJSON['INSIGHTS']["baze_2_news"] =$countactivity;
               }
                   $query_all = "SELECT * FROM tbl_androidid WHERE date BETWEEN '".$firstbazefrom."' AND '".$firstbazeto."'";
           $allres = @mysqli_query($connect,$query_all);
           if($allres){
             $count = 0;
            while($row = @mysqli_fetch_assoc($allres))
               $count++;
  
                $myJSON['INSIGHTS']["baze_1_actives"] = $count;
           }
               $query_all = "SELECT * FROM tbl_androidid WHERE date BETWEEN '".$secondbazefrom."' AND '".$secondbazeto."'";
               $allres = @mysqli_query($connect,$query_all);
               if($allres){
                 $countactivity = 0;
                while($row = @mysqli_fetch_assoc($allres))
                   $countactivity++;
            
                    $myJSON['INSIGHTS']["baze_2_actives"] =$countactivity;
        }
//orders
        $query_all = "SELECT * FROM ".$tbl_orders;
			$allres = @mysqli_query($connect,$query_all);
			if($allres){
        $countins = 0;
			 while($row = @mysqli_fetch_assoc($allres))
          $countins++;
           $myJSON['INSIGHTS']["all_ords"] = $countins;
      }
          
         $firstbazefrom .=" - 00:00";
         $firstbazeto .=" - 23:59";
         $secondbazefrom .=" - 00:00";
         $secondbazeto .=" - 23:59";        

         $query_all = "SELECT * FROM ".$tbl_orders." WHERE type != 'K' AND date BETWEEN '".$firstbazefrom."' AND '".$firstbazeto."'";
         $allres = @mysqli_query($connect,$query_all);
         if($allres){
           $countactivity = 0;
           $ordprice = 0;
          while($row = @mysqli_fetch_assoc($allres)){
             $countactivity++;
             $ordprice += $row["Price"];
          }

          $myJSON['INSIGHTS']["baze_1_orders"] = $countactivity;
           $myJSON['INSIGHTS']["baze_1_ordersPrice"] = $ordprice;
         }
             $query_all = "SELECT * FROM ".$tbl_orders." WHERE type != 'K' AND date BETWEEN '".$secondbazefrom."' AND '".$secondbazeto."'";
             $allres = @mysqli_query($connect,$query_all);
             if($allres){
               $countactivity = 0;
               $ordprice = 0;
              while($row = @mysqli_fetch_assoc($allres)){
                $countactivity++;
                $ordprice += $row["Price"];
             }
                  $myJSON['INSIGHTS']["baze_2_orders"] = $countactivity;
                  $myJSON['INSIGHTS']["baze_2_ordersPrice"] = $ordprice;
             }
       
      
    //  array_push( $myJSON['INSIGHTS'],   $myJSON['INSIGHTS']);
      $myJSON['message']="200" ;
	}
	 else
		 $myJSON['message']="400";

}
echo json_encode($myJSON);
?>