<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class GraphController extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('registration_house_women_child_model');
        $this->load->model('mira_graph_view_model');
        $this->load->helper('url');
        if (!$this->session->userdata('is_user_loged')) {
            redirect('login');
        }
    }

//    public function mira_graph_view() {
//        $return['anmList'] = $this->registration_house_women_child_model->getANMListWithAppId($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
//
//        $this->session->set_userdata('page_name', 'anm_mirawise_graph');
//        $this->load->view('mira_welcome_page', $return);
//    }

    public function vaccinationDataPage() {
        $ulevel = $this->session->userdata('level');
        $tlevel = $this->session->userdata('app_total_level');
        $region_code = $this->session->userdata('create_region_id');
        $return['anmList'] = $this->registration_house_women_child_model->getANMListWithAppId($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
        $this->session->set_userdata('page_name', 'anm_mirawise_graph');
        $this->load->view('mira_welcome_page', $return);
    }

    public function getAshaDeatilByANMId() {
        header('Content-Type: application/json');
        $return = ['status' => false, 'message' => 'Invalid request', 'miraList' => []];
        if ($this->input->post('region_code')) {
            $region_code = $this->input->post('region_code');
            $app_id = $this->session->userdata('app_id');
            $asha_details = $this->mira_graph_view_model->getAshaDetailWithRegionCode($app_id, $region_code);
            if (!empty($asha_details)) {
                $return = [
                    'status' => true,
                    'message' => 'Data fetched successfully',
                    'miraList' => $asha_details
                ];
            } else {
                $return['message'] = 'No data found';
            }
        }
        echo json_encode($return);
        exit;
    }

    public function getVaccinationDataOfChildren() {
// Get POST inputs
        $anm_region_code = $this->input->post('anmCode'); // Fixed variable name
        $asha_region_code = $this->input->post('ashaCode'); // Fixed variable name// Validate inputs
        if (!$anm_region_code || !$asha_region_code) {
            echo json_encode(['status' => 'error', 'message' => 'Invalid input: Region codes are
required.']);
            return;
        }
// Fetch dose data from the model
        $vaccination_data = $this->mira_graph_view_model->getVaccinationDataOfChildren($anm_region_code, $asha_region_code);
// Check if data retrieval failed
        if (empty($vaccination_data)) {
            echo json_encode(['status' => 'error', 'message' => 'No data found for the provided region
codes.']);
            return;
        }
// Return the data as JSON
        echo json_encode(['status' => 'success', 'data' => $vaccination_data]);
    }

    public function getDoseCompletionData() {
        $anm_region_code = $this->input->post('anmCode');
        $asha_region_code = $this->input->post('ashaCode');
// Validate inputs
        if (!$anm_region_code || !$asha_region_code) {
            echo json_encode(['status' => 'error', 'message' => 'Invalid input']);
            return;
        }
// Fetch dose data from the model
        $dose_data = $this->mira_graph_view_model->getDoseCompletionData($anm_region_code, $asha_region_code);
// Check if data is available
        if (!$dose_data || !is_array($dose_data)) {
            echo json_encode(['status' => 'error', 'message' => 'No data found']);
            return;
        }
// Convert data to an array format and typecaste to int  
        $response_data = [
            ['label' => 'Completed Doses', 'value' => (int) (isset($dose_data['done']) ? $dose_data['done'] : 0)],
            ['label' => 'Missed Doses', 'value' => (int) (isset($dose_data['missed']) ? $dose_data['missed'] : 0)],
            ['label' => 'Total Children', 'value' => (int) (isset($dose_data['total']) ? $dose_data['total'] : 0)]
        ];
// Return the response in JSON format
        echo json_encode(['status' => 'success', 'data' => $response_data]);
    }

    public function get_women_pregnancy_info() {
// Get POST inputs
        $anm_region_code = $this->input->post('anmCode');
        $asha_region_code = $this->input->post('ashaCode');
// Fetch data from model
        $statistics = $this->mira_graph_view_model->getPregnancyStatistics($anm_region_code, $asha_region_code);
        if (empty($statistics)) {
            echo json_encode(['status' => 'error', 'message' => 'No data found for the provided region
codes.']);
            return;
        }
// Convert associative array to indexed array
    $formattedData = [
        ['label' => 'Hospital Delivery', 'value' => (int) $statistics['hospital_delivery']],
        ['label' => 'Home Delivery', 'value' => (int) $statistics['home_delivery']],
        ['label' => 'Other', 'value' => (int) $statistics['other']],
        ['label' => 'Running Pregnancy', 'value' => (int) $statistics['running_pregnancy']]
        ];
// Return the data as JSON
        echo json_encode(['status' => 'success', 'data' => $formattedData]);
    }

}
