<?php

function get_current_user_det(){
    include("config.php");
    $usr_id = $_SESSION['loggedin_id'];
    $query = "SELECT * from users where id = '".$usr_id."' ";
    $run = mysqli_query($conn, $query);
    $res=mysqli_fetch_assoc($run);
    return $res;
  }

function get_user_det($id=0){
    include("config.php");
    $query = "SELECT * from users where id = '".$id."' ";
    $run = mysqli_query($conn, $query);
    $res=mysqli_fetch_assoc($run);
    return $res;
  }


function system_date_format($date){
    return date('d M Y', strtotime($date) );
  }
  
function get_all_feedbacks($type="all" , $branch_id=0, $from=NULL, $to=NULL){
  include("config.php");

    $branchCondition = "";
  if($branch_id>0){
    $branchCondition = " AND branch_id = $branch_id ";
  }
  
  $fromCondition = "";
  if($from!=NULL){
    $fromCondition = " AND created_date >=  '".$from."' ";
  }
  
  $toCondition = "";
  if($to!=NULL){
    $toCondition = " AND created_date <= '".$to."' ";
  }


  $query = "SELECT * from feedbacks WHERE deleted <> 1 AND id <> 0 $branchCondition $fromCondition $toCondition";
  $data = mysqli_query($conn, $query);
  return $data;
}

function get_all_admins($type="all"){
  include("config.php");
  $query = "SELECT * from users WHERE deleted <>1";
  $data = mysqli_query($conn, $query);
  return $data;
}

function get_all_branches($type="all"){
    ini_set('display_errors', '1');
  ini_set('display_startup_errors', '1');
  error_reporting(E_ALL);

  include("config.php");


  if($type == "active"){
    $query = "SELECT * from branches WHERE active = 1";
  }else{
    $query = "SELECT * from branches WHERE deleted = 0";
  }
  $data = mysqli_query($conn, $query);
  return $data;
}
function get_single_branch($id = 0){
  include("config.php");
  $query = "SELECT * from branches where id = '".$id."' ";
  $run = mysqli_query($conn, $query);
  $res=mysqli_fetch_assoc($run);
  return $res;
}



function get_single_feedback_details($id=0){
  ini_set('display_errors', '1');
  ini_set('display_startup_errors', '1');
  error_reporting(E_ALL);
  include("config.php");
  $query = "SELECT * from feedbacks where deleted <> 1 AND id = '".$id."' ";
  $run = mysqli_query($conn, $query);
  $res=mysqli_fetch_assoc($run);
  return $res;
}
function get_rating_by_category($cat="", $rat=0, $branch_id=0, $from=NULL, $to=NULL){
  ini_set('display_errors', '1');
  ini_set('display_startup_errors', '1');
  error_reporting(E_ALL);
  include("config.php");
  
  $branchCondition = "";
  if($branch_id>0){
    $branchCondition = " AND branch_id = $branch_id ";
  }
  
  $fromCondition = "";
  if($from!=NULL){
    $fromCondition = " AND created_date >=  '".$from."' ";
  }
  
  $toCondition = "";
  if($to!=NULL){
    $toCondition = " AND created_date <= '".$to."' ";
  }
  
  $query = "SELECT count(*) as total_rating from feedbacks where deleted <> 1 AND $cat = $rat $branchCondition $fromCondition $toCondition ";
  $data = mysqli_query($conn, $query);
  $res=mysqli_fetch_assoc($data);
   return $res['total_rating'];
    //return $query;
}

function get_company_data($id=0){
  ini_set('display_errors', '1');
  ini_set('display_startup_errors', '1');
  error_reporting(E_ALL);
  include("config.php");
  $query = "SELECT * from company where id <> 0 LIMIT 1";
  $run = mysqli_query($conn, $query);
  $res=mysqli_fetch_assoc($run);
  return $res;
}

function get_user_by_id($id = 0){
  include("config.php");
  $query = "SELECT * from users where id = '".$id."' ";
  $run = mysqli_query($conn, $query);
  $res=mysqli_fetch_assoc($run);
  return $res;
}
?>