<?php

class AdminModel extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}


	public function record_count($param){

		$data = [];
		$where = '';
		if (isset($param['name']) && $param['name'] != '') {
			$where .= " AND  fullname like '%".$param['name'].
			"%' ";
		}
		if (isset($param['email']) && $param['email'] != '') {
			$where .= " AND  email like '%".$param['email'].
			"%' ";
		}

		$sql = "SELECT COUNT( DISTINCT admin_id ) AS TotalrecordCount FROM  `admin`  WHERE 1 $where";

		$recordSet = $this->db->query($sql);
		if ($recordSet) {
			$row = $recordSet->row();
			$total_rows = $row->TotalrecordCount;
		}

		return $total_rows;

	}

	public function all_data_list($limit,$start,$param){

		$data=[];
		$where = '';
		if (isset($param['name']) && $param['name'] != '') {
			$where .= " AND  fullname like '%".$param['name'].
			"%' ";
		}
		if (isset($param['email']) && $param['email'] != '') {
			$where .= " AND  email like '%".$param['email'].
			"%' ";
		}

		$sql="SELECT * FROM `admin` WHERE 1  $where  LIMIT ".$start.",".$limit ;
        $data = $this->db->query($sql)->result_array();
        // echo "<pre>";print_r($data);exit();
		return $data;
	}
	public function get_total_customers() {
        return $this->db->count_all('customer');
    }

    public function get_total_bookings() {
        return $this->db->count_all('bookings');
    }

    public function get_total_revenue() {
        $this->db->select_sum('total_price');
        $query = $this->db->get('bookings');
        return $query->row()->total_price ?? 0;
    }

    public function get_active_sports() {
        return $this->db->count_all('sports'); // assuming you have sports table
    }

    public function get_recent_bookings($limit=5) {
        $this->db->select('b.*, c.name as customer_name, c.email, p.name as pitch_name, s.name as sport_name');
        $this->db->from('bookings b');
        $this->db->join('customer c','b.user_id = c.c_id','left');
        $this->db->join('pitches p','b.pitch_id = p.id','left');
        $this->db->join('sports s','p.sport_id = s.id','left');
        $this->db->order_by('b.created_at','DESC');
        $this->db->limit($limit);
        return $this->db->get()->result();
    }
    public function get_bookings() {
        $this->db->select('b.*, c.name as customer_name, c.email, p.name as pitch_name, s.name as sport_name');
        $this->db->from('bookings b');
        $this->db->join('customer c','b.user_id = c.c_id','left');
        $this->db->join('pitches p','b.pitch_id = p.id','left');
        $this->db->join('sports s','p.sport_id = s.id','left');
        $this->db->order_by('b.created_at','DESC');
        return $this->db->get()->result();
    }

    public function get_monthly_bookings() {
        $this->db->select("MONTH(booking_date) as month, COUNT(id) as total");
        $this->db->from("bookings");
        $this->db->group_by("MONTH(booking_date)");
        $query = $this->db->get();
        return $query->result();
    }

    public function get_sports_popularity() {
        $this->db->select("s.name, COUNT(b.id) as total");
        $this->db->from("bookings b");
        $this->db->join("pitches p","b.pitch_id = p.id","left");
        $this->db->join("sports s","p.sport_id = s.id","left");
        $this->db->group_by("s.id");
        $query = $this->db->get();
        return $query->result();
    }

}

?>
