<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Reports extends CI_Controller {

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

        // Require login: this page exposes patient-level report data
        if (!isset($_SESSION['qurbani_user_id'])) {
            redirect('/admin','refresh');
        }

        $this->load->model('User_model', 'user');
        $this->load->model('Token_model','token');
        $this->load->model('Shifts_model','shifts');
        $this->load->model('Departments_model','departments');
        $this->load->model('Services_model','services');
        $this->load->model('Sub_services_model','sub_services');
        $this->load->model('Settings_model','setup');
    }

public function index() {
    // load dropdown data
    $data['shifts']       = $this->shifts->get_all();
    $data['departments']  = $this->departments->get_all();
    $data['services']     = $this->services->get_all();
    $data['sub_services'] = $this->sub_services->get_all();

    // get filters
    $date_from = $this->input->get('date_from');
    $date_to   = $this->input->get('date_to');

    // set default to today if empty
    if (empty($date_from)) {
        $date_from = date('Y-m-d');
    }
    if (empty($date_to)) {
        $date_to = date('Y-m-d');
    }

    $filters = [
        'date_from'     => $date_from,
        'date_to'       => $date_to,
        'shift_id'      => $this->input->get('shift_id'),
        'department_id' => $this->input->get('department_id'),
        'service_id'    => $this->input->get('service_id'),
        'sub_service_id'=> $this->input->get('sub_service_id'),
        'gender'        => $this->input->get('gender'),
        'funded_by'     => $this->input->get('funded_by')
    ];

    // fetch filtered data
    $data['tokens']   = $this->token->get_filtered_tokens($filters);
    $data['settings'] = $this->setup->get_by_id(1);

    $data['filters']  = $filters;
    $data['title']    = 'Daily Patients Reports';
    $data['mainContent'] = 'admin/reports/index';
    $this->load->view('admin/layout/master', $data);
}

}
