<?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.
 */

/**
 * Description of MiraServiceAnm
 *
 * @author zmq163anmUpdateAncStatus
 */
class MiraServiceAnm extends CI_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('Mira_service_anm_model');
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function anmUpdateWeekwiseQusAns() {
        $data = $this->getDataFromMobile($_SERVER);

        //$asha_qus_ans = array('action_anm' => array(array('pregnant_id' => 1, 'week_id' => 11, 'qus_id_ans' => array(array('question_id' => 1, 'anm_answer' => 4), array('question_id' => 3, 'anm_answer' => 4), array('question_id' => 5, 'anm_answer' => 3))), array('pregnant_id' => 1, 'week_id' => 13, 'qus_id_ans' => array(array('question_id' => 2, 'anm_answer' => 4), array('question_id' => 3, 'anm_answer' => 3), array('question_id' => 4, 'anm_answer' => 3),))));
        //print_r(json_encode($asha_qus_ans));
        if ($this->Mira_service_anm_model->updateAnmAnswerModel($data['action_anm'])) {
            $return_status = NULL;
            for ($i = 0; $i < count($data['action_anm']); $i++) {
                $return_status[] = array('women_id' => $data['action_anm'][$i]['pregnant_id'], 'status' => 1);
            }
            print_r(json_encode($return_status));
        } else {
            print_r(json_encode(array('status' => 0)));
        }
        //$data_return[$l++] = array("women_id" => $qus_ans_data[$i][$j]['pregnant_id'], 'status' => 1);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function anmUserAuthontication() {
        $data = $this->getDataFromMobile($_SERVER);
        //$data=array('username'=>'zmqanm2','password'=>'gc1890gc');
        if (isset($data['username']) and isset($data['password'])) {
            $return_data = $this->Mira_service_anm_model->anmUserAuthonticationModel($data['username'], $data['password']);
            if ($return_data and $return_data['user']) {
                for ($i = 0; $i < count($return_data['ashaList']); $i++) {
                    $return_data['ashaList'][$i]['login_code'] = 7;
                }
                print_r(json_encode($return_data['ashaList']));
            } else {
                print_r(json_encode(array('login_code' => 0)));
            }
        } else {
            print_r(json_encode(array('login_code' => 0,'message'=>'Userid or password null')));
        }
    }

    //$asha[$i] = array('login_code' => $user_data['login_code'], 'asha_name' => $user_sign_up->getUserByLoginId($user_login->getUserByAreaId($creat_region_data[$i]['area_code'])), 'asha_id' => $creat_region_data[$i]['area_code']);

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function anmUpdateAncStatus() {

        //$json_data='{"value":[{"qus_ans":{"Hist_Systm_Illness":"N.A.","Hist_Prev_Preg":"N.A.","Famly_Hist_Syst_Illness":"N.A.","Ord_Preg_Bth_Int":"N.A.","Symp_Preg":"N.A.","Hist_Drug_Intake":"N.A."},"anc_code":"0","women_id":"1"}]}';
        //$data = json_decode($json_data, TRUE);
        //print_r($data);
        $data=json_decode(file_get_contents('php://input'), TRUE);
        //$data = $this->getDataFromMobile($_SERVER);
        if (isset($data['value']) and $data['value'] != NULL) {
            foreach ($data['value'] as $key => $value) {
                $temp_data_insert = NULL;
                if (!$value['anc_code']) {
                    $temp_data_anc = array('pregnant_id' => $value['women_id'], 'anc_code' => $value['anc_code'],'critical_status' => 0, 'anc_status' => 1, 'anc_date' => date('Y-m-d'));
                    foreach ($value['qus_ans'] as $key => $value1) {
                        $temp_data_insert[] = array('pregnant_id' => $value['women_id'], 'anc_code' => $value['anc_code'], 'question_id' => $key, 'answer' => $value1);
                    }
                } else {
                    $temp_data_anc = array('pregnant_id' => $value['women_id'], 'anc_code' => $value['anc_code'], 'critical_status' => $value['critical'], 'anc_status' => 1, 'anc_date' => date('Y-m-d'));
                    foreach ($value['qus_ans'] as $key => $value1) {
                        $temp_data_insert[] = array('pregnant_id' => $value['women_id'], 'anc_code' => 1, 'question_id' => $key, 'answer' => $value1);
                    }
                }

                if ($this->Mira_service_anm_model->insertANCStatusAndANMQusAns($temp_data_anc, $temp_data_insert)) {
                    $women_ststus[] = array('women_id' => $value['women_id'], 'status' => 1);
                } else {
                    $women_ststus[] = array('women_id' => $value['women_id'], 'status' => 0);
                }
            }
            print_r(json_encode($women_ststus));
        } else {
            print_r(json_encode(array('status'=>'fail','data'=> $data)));
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
     
     /*
     //Old code
    public function anmGetAllDataByAshaId() {
        $data = $this->getDataFromMobile($_SERVER);
        if (TRUE or isset($data['asha_id']) and $data['asha_id'] != NULL) {
            $data = $this->Mira_service_anm_model->anmGetAllDataByAshaIdModel($data['asha_id']);
            for ($i = 0; $i < count($data); $i++) {
                foreach ($data[$i] as $key => $value) {
                    if (is_null($value)) {
                        $data[$i][$key] = "NULL";
                    }
                }
            }

            print_r(json_encode($data));
        } else {
            
        }
    }
     */
   
   
   public function anmGetAllDataByAshaId() {
    $data = $this->getDataFromMobile($_SERVER);

    // Check if 'asha_id' is set and not NULL
    if (isset($data['asha_id']) && $data['asha_id'] !== NULL) {
        // Get all data by ASHA ID
        $data = $this->Mira_service_anm_model->anmGetAllDataByAshaIdModel($data['asha_id']);
        
        // Check if $data is an array before calling count()
        if (is_array($data)) {
            for ($i = 0; $i < count($data); $i++) {
                if (isset($data[$i]) && is_array($data[$i])) {
                    foreach ($data[$i] as $key => $value) {
                        if (is_null($value)) {
                            $data[$i][$key] = "NULL";
                        }
                    }
                }
            }
        }

        // Print the encoded JSON data
        print_r(json_encode($data));
    } else {
        // Handle the case where 'asha_id' is not set or is NULL
        // Optionally, you can return a specific error message or handle the situation as needed
        print_r(json_encode(['error' => 'asha_id is missing or NULL']));
    }
}
   
   
    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function anmGetWomenHistory() {
        $data = $this->getDataFromMobile($_SERVER);
        if (isset($data['asha_id']) and $data['asha_id'] != NULL) {
            $data = $this->Mira_service_anm_model->anmGetWomenHistoryModel($data['asha_id']);
            for ($i = 0; $i < count($data); $i++) {
                foreach ($data[$i] as $key => $value) {
                    switch ($key) {
                        case 'anc1_code':
                            unset($data[$i][$key]);
                            break;
                        case 'anc2_code':
                            unset($data[$i][$key]);
                            break;
                        case 'anc3_code':
                            unset($data[$i][$key]);
                            break;
                        case 'anc4_code':
                            unset($data[$i][$key]);
                            break;
                    }
                }
            }
            print_r(json_encode($data));
        } else {
            
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function anmGetPregnantImmunzationByAshaId() {
        $data = $this->getDataFromMobile($_SERVER);
        if (isset($data['asha_id']) and $data['asha_id'] != NULL) {
            print_r(json_encode($this->Mira_service_anm_model->anmGetPregnantImmunzationByAshaIdModel($data['asha_id'])));
        } else {
            
        }
    }

    /**
     * 
     * @param type $global_data
     * 
     * 
     * 
     */
    private function getDataFromMobile($global_data=NULL) {
        $data = NULL;
        if ($global_data['REQUEST_METHOD'] == "POST") {
            if (isset($global_data['HTTP_JSON'])) {
                $data = json_decode($global_data['HTTP_JSON'], TRUE);
            } else {
                $data = $_POST;
            }
        } else if ($global_data['REQUEST_METHOD'] == "GET") {
            if (isset($global_data['HTTP_JSON'])) {
                $data = json_decode($global_data['HTTP_JSON'], TRUE);
            } else {
                $data = $_GET;
            }
        }
        return $data;
    }

}
