<?php
 


   $service_orders = $conn->prepare("SELECT orders.service_id,orders.last_check,orders.order_create FROM services right JOIN orders  ON services.service_id=orders.service_id where order_status='completed'  order by orders.order_id DESC");
   
   
$service_orders->execute();

$service_orders = $service_orders->fetchAll(PDO::FETCH_ASSOC);

$s_orders = [];

foreach($service_orders as $service_order){
    if( isset($s_orders[$service_order['service_id']] ) && count($s_orders[$service_order['service_id']])>9){
        continue;
    }
    $s_orders[$service_order['service_id']] []= [
        'last_check'=> $service_order['last_check'],
        'order_create'=> $service_order['order_create'],
        ]; 
}

foreach($s_orders as $id => $s_order){
    if(count($s_order)<10){
        unset($s_orders[$id]);
    }
    }



$title .= $languageArray["services.title"];
 
if( $settings["service_list"] == 1 && !$_SESSION["neira_userlogin"] ):
  header("Location:".site_url());
endif;
 
 
if($_SESSION["neira_userlogin"] == 1 ):
    if($settings["sms_verify"] == 2 && $user["sms_verify"] != 2){
        header("Location:".site_url('verify/sms'));
    }
    if($settings["mail_verify"] == 2 && $user["mail_verify"] != 2 ){
        header("Location:".site_url('verify/mail'));
    }
    endif;
   
$categoriesRows = $conn->prepare("SELECT category_id,category_secret,category_name,category_icon,category_icon_color FROM categories WHERE category_type=:type  ORDER BY categories.category_line ASC");
$categoriesRows->execute(array("type"=>2));

$categoriesRows = $categoriesRows->fetchAll(PDO::FETCH_ASSOC);
 
$categories = [];

          $language   = $conn->prepare("SELECT * FROM languages WHERE default_language=:default");
        $language->execute(array("default"=>1));
        $language_code   = $language->fetch(PDO::FETCH_ASSOC);
        
  foreach ( $categoriesRows as $categoryRow ) {
    $search = $conn->prepare("SELECT id FROM clients_category WHERE category_id=:category && client_id=:c_id ");
    $search->execute(array("category"=>$categoryRow["category_id"],"c_id"=>$user["client_id"]));
    if( $categoryRow["category_secret"] == 2 || $search->rowCount() ):
      $rows     = $conn->query("SELECT name_lang,service_name,description_lang,service_description,service_id,service_min,service_max,service_secret FROM services WHERE category_id='{$categoryRow["category_id"]}' && service_type=2 ORDER BY service_line ");
      $rows     = $rows->fetchAll(PDO::FETCH_ASSOC);
     
      $services = [];
     
       foreach ( $rows as $row ) {
           
        if($settings["avarage"] == 2):      
           
         
                $avarageTime = true;
                
                if(!isset($s_orders[$row['service_id']])) {
                       $veri = $languageArray["monitor.error"];
                }
                else{
                foreach($s_orders[$row['service_id']] as $order) {
                    $basla = strtotime($order["order_create"]);
                    $bitis = strtotime($order["last_check"]);
                    $bitissil = $bitis-900;
                    $ortalama= ($bitissil-$basla) ;
                    $orta = $ortalama/60;
                    $ortalama1 = round(abs($basla - $bitissil));
                   
                    $callback = $ortalama1.",";
                }
                
                $parcala = explode(",",$callback);
   
                $dizi = array($parcala["0"],$parcala["2"],$parcala["3"],$parcala["4"],$parcala["5"],$parcala["6"],$parcala["7"],$parcala["8"],$parcala["9"],$parcala["1"]);
                $ortalamamiz = explode(".",ortalama($dizi));
               
                if($ortalamamiz[0] == "NaN") {
                  $veri = $languageArray["monitor.error"];
                } else {
                  $veri = convertSecToStr($ortalamamiz[0]);
                  
                }  
                }
               
                $s["service_speed"] = $veri;
               
        endif;
            $multiName   =  json_decode($row["name_lang"],true);
            
         

            if(!$user["lang"]):
                $user["lang"] = $language_code['language_code'] ?? "ar";
            endif; 
         
            if( $multiName[$user["lang"]] ):
              $name = $multiName[$user["lang"]];
            else:
              $name = $row["service_name"];
            endif;
 
 
           $multiDesc   =  json_decode($row["description_lang"],true);
           
  

            if( $multiDesc[$user["lang"]] ):
              $desc = $multiDesc[$user["lang"]];
            else:
              $desc = $row["service_description"];
            endif;
 
            $desc = str_replace("\n","<br />",$desc);
 
 
          $s["service_id"]    = $row["service_id"];
          $s["service_name"]  = mb_convert_encoding($name,"UTF-8","auto");
          $s["service_description"]  = mb_convert_encoding($desc,"UTF-8","auto");
          $s["service_price"] = priceFormat(service_price($row["service_id"]));
          $s["service_min"]   = $row["service_min"];
          $s["service_max"]   = $row["service_max"];
       
          $search = $conn->prepare("SELECT id FROM clients_service WHERE service_id=:service && client_id=:c_id ");
          $search->execute(array("service"=>$row["service_id"],"c_id"=>$user["client_id"]));
          if( $row["service_secret"] == 2 || $search->rowCount() ):
            array_push($services,$s);
          endif;
        }
      $c["category_name"]          = $categoryRow["category_name"];
      $c["category_icon"]          = $categoryRow["category_icon"];
        $c["category_icon_color"]          = $categoryRow["category_icon_color"];
      $c["category_id"]            = $categoryRow["category_id"];
      $c["services"]               = $services;
      array_push($categories,$c);
    endif;
 
  }
