<?php
if(!defined('BASEPATH')) {
   die('Direct access to the script is not allowed');
}
if( $admin["access"]["admin_access"] != 1  ){

    header("Location:".site_url("admin"));

    exit();
}
  
  $clients = $conn->prepare("SELECT * FROM clients ORDER BY client_id DESC LIMIT 500");
  $clients->execute(array());
  $clients = $clients->fetchAll(PDO::FETCH_ASSOC);

$pending_orders = $conn->query("SELECT COUNT(order_id) FROM orders WHERE order_status='pending'")->fetchColumn();
$inprogress_orders = $conn->query("SELECT COUNT(order_id) FROM orders WHERE order_status='inprogress'")->fetchColumn();
$failed_orders = $conn->query("SELECT COUNT(order_id) FROM orders WHERE order_status='fail'")->fetchColumn();
$cancelled_orders = $conn->query("SELECT COUNT(order_id) FROM orders WHERE order_status='canceled'")->fetchColumn();

$completed_orders = $conn->query("SELECT COUNT(order_id) FROM orders WHERE order_status='completed'")->fetchColumn();



$total_users = $conn->query("SELECT COUNT(client_id) FROM clients")->fetchColumn();
$total_orders = $conn->query("SELECT COUNT(order_id) FROM orders")->fetchColumn();
$total_providers = $conn->query("SELECT COUNT(id) FROM service_api")->fetchColumn();
$active_services = $conn->query("SELECT COUNT(service_id) FROM services WHERE service_deleted=0")->fetchColumn();




  $user_count_total = countRow(["table"=>"clients"]);
  $user_count_week = countRow(["table"=>"clients", "where" => ["creation_date >" => date('Y-m-d H:i:s', strtotime('-7 days'))] ]);
  $user_trend = (($user_count_week / ($user_count_total - $user_count_week)) * 100);


$failCount      = $conn->prepare("SELECT * FROM orders WHERE orders.dripfeed='1' && orders.subscriptions_type='1' && order_error!=:error ");
  $failCount     -> execute(array("error"=>"-"));
  $failCount      = $failCount->rowCount();
$todayCount      = $conn->prepare("SELECT * FROM orders WHERE last_check=:error ");
  $todayCount     -> execute(array("error"=> date("Y-m-d") ));
  $todayCount     = $todayCount->rowCount();



   $services       = $conn->prepare("SELECT * FROM services RIGHT JOIN categories ON categories.category_id = services.category_id LEFT JOIN service_api ON service_api.id = services.service_api ORDER BY categories.category_line,services.service_line ASC ");
  $services       -> execute(array());
  $services       = $services->fetchAll(PDO::FETCH_ASSOC);
  $serviceList    = array_group_by($services, 'category_name');
  if( count($yearList) == 0 ): $yearList[0] = date("Y"); endif;
  if( $_GET["year"] ):
    $year = $_GET["year"];
  else:
    $year = date("Y");
  endif;
  


require admin_view('index');