<?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 ImmuizationWomenChild
 *
 * @author zmq163
 */
class ImmuizationWomenChild extends CI_Controller {

    //put your code here
    var $per_page_row;

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->model('Immuization_women_child_model');
         $this->load->model('Registration_house_women_child_model');
        $this->per_page_row = 4;
        if (!$this->session->userdata('is_user_loged')) {
            redirect('login');
        }
    }
    /**
     * 
     * 
     * 
     * 
     */
    public function updateMissedVaccine(){
        $vaccination_date=date('Y-m-d',  strtotime($this->input->post('dose_taken_date')));
        $child_id_vaccine_id=explode('_',$this->input->post('child_id_vaccine_id'));
        $responseData=  $this->Immuization_women_child_model->updateMissedVaccineModel($vaccination_date,$child_id_vaccine_id);
        if($responseData){
              print_r(json_encode(array('status'=>TRUE)));
        }  else {
              print_r(json_encode(array('status'=>FALSE)));
        }
      
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getVaccineStartEndDate() {
        $child_id_vaccine_id = explode('_', $this->input->post('child_id_vaccine_id'));
        $responseData = $this->Immuization_women_child_model->getVaccineStartEndDateModel($child_id_vaccine_id);
        if ($responseData) {
            print_r(json_encode(array('status' => TRUE, 'startDate' => $responseData['startDate'], 'endDate' => $responseData['endDate'],'vaccine_code'=>$responseData['vaccine_code'])));
        } else {
            print_r(json_encode(array('status' => FALSE)));
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function updatePregnancyWeekByAnm() {
        print_r(json_encode(array('status' => $this->Immuization_women_child_model->updatePregnancyWeekByAnmModel($_POST['pregnant_id'], $_POST['current_week'], $_POST['refer_to']))));
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function pregnancyUpdateDeliveryStatus() {
        $return['pregnant_list'] = $this->Immuization_women_child_model->getPregnantWomenByAshaRegionIdModel($this->session->userdata('create_region_id'));
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->session->set_userdata('page_name', 'mira_pregnant_women_update_status');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updateDeliveryStatus() {
        if (isset($_POST['pregnant_id']) and $_POST['pregnant_id'] and isset($_POST['pregnancy_status']) and $_POST['pregnancy_status'] and $this->session->userdata('data_submit_status')) {
            $this->session->set_userdata('data_submit_status', FALSE);
            $update_data = NULL;
            $pregnancy_status = $_POST['pregnancy_status'];
            if ($pregnancy_status == 1) {
                $update_data = array('pregnant_status' => 0, 'delivery_status' => $pregnancy_status, 'closing_date' => date('Y-m-d'));
                if (isset($_POST['baby_current_status'])) {
                    $update_data['delivered_baby_status'] = $_POST['baby_current_status'];
                } else {
                    $update_data['delivered_baby_status'] = -1;
                }

                if (isset($_POST['delivery_location'])) {
                    $update_data['delivery_location'] = $_POST['delivery_location'];
                } else {
                    $update_data['delivery_location'] = -1;
                }
            } elseif ($pregnancy_status == 3) {
                $update_data = array('pregnant_status' => 0, 'delivery_status' => $pregnancy_status, 'closing_date' => date('Y-m-d'));
                if (isset($_POST['baby_current_status'])) {
                    $update_data['delivered_baby_status'] = $_POST['baby_current_status'];
                } else {
                    $update_data['delivered_baby_status'] = -1;
                }
                if (isset($_POST['delivery_location'])) {
                    $update_data['delivery_location'] = $_POST['delivery_location'];
                } else {
                    $update_data['delivery_location'] = -1;
                }
            } else {
                $update_data = array('pregnant_status' => 0, 'delivery_status' => $pregnancy_status, 'closing_date' => date('Y-m-d'));
            }
            if ($this->Immuization_women_child_model->updateDeliveryStatusModel($_POST['pregnant_id'], $update_data)) {
                $return['status'] = TRUE;
                $return['message'] = 'Update Successfully.';
            } else {
                $return['status'] = FALSE;
                $return['message'] = 'Update fail. Please retry.';
            }
        } else {
            $return['status'] = FALSE;
            $return['message'] = 'Dont refresh button click.';
        }
        $this->session->set_userdata('page_name', 'mira_success_page');

        $this->session->set_userdata('alert_message_info', 'alert_pregnant_delivery_status');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function updateChildVaccine() {
        if ($this->Immuization_women_child_model->updateVaccineChildUpdate(array('child_id' => $_POST['child_id'], 'vaccine_id' => $_POST['vaccine_id'], 'vaccine_date' => date('Y-m-d', strtotime($_POST['vaccine_date']))))) {
            print_r(json_encode(array('status' => TRUE)));
        } else {
            print_r(json_encode(array('status' => FALSE)));
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function childImmunizationReport() {
        $this->session->set_userdata('page_name', 'mira_tracker_child_immunization');
        $_per_page_row = 8;
        $return['activePage'] = $this->uri->segment(2);
        $return['totalPage'] = $this->getNumberOfPages(count($this->Immuization_women_child_model->getChildListModel($this->session->userdata('create_region_id'))), $_per_page_row);
        $return['vaccine_list'] = $this->Immuization_women_child_model->getChildVaccineListModel($this->session->userdata('app_id'));

        $master_vaccine_list = $return['vaccine_list'];

        $return['child_list'] = $this->Immuization_women_child_model->getChildListModel($this->session->userdata('create_region_id'), $this->uri->segment(2), $_per_page_row);

        if ($return['child_list']) {
            for ($i = 0; $i < count($return['child_list']); $i++) {

                $return['child_list'][$i]['vaccine_list'] = $this->Immuization_women_child_model->getChilVaccineDoneModel($master_vaccine_list, $return['child_list'][$i]);
            }
        }
        //print_r($return);
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getChildImmunization() {
        $_per_page_row = 8;
        $return['activePage'] = $this->uri->segment(2);
        $return['totalPage'] = $this->getNumberOfPages(count($this->Immuization_women_child_model->getChildListDueVaccineModel($this->session->userdata('create_region_id'))), $_per_page_row);
        $this->session->set_userdata('page_name', 'mira_immunization_child');
       // $return['child_list'] = $this->Immuization_women_child_model->getChildListDueVaccineModel($this->session->userdata('create_region_id'));
        $return['child_list'] = $this->Immuization_women_child_model->getChildListDueVaccineModel($this->session->userdata('create_region_id'), $this->uri->segment(2), $_per_page_row);
        
        
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function pregnantImmunizationReportCurrent() {
        $this->load->model('Mira_worker_tracking_activity');
        $_per_page_row = 8;
        $return['activePage'] = $this->uri->segment(2);
        $data = $this->Immuization_women_child_model->getPregnantWomenImmunizatioCurrent($this->session->userdata('create_region_id'), $return['activePage'], $_per_page_row);
        $return['totalPage'] = $this->getNumberOfPages(count($this->Immuization_women_child_model->getPregnantWomenImmunizatioCurrent($this->session->userdata('create_region_id'))), $_per_page_row);
        if (isset($data) and $data) {
            for ($i = 0; $i < count($data); $i++) {
                if ($this->getWeek1(floor((strtotime($data[$i]['registration_date']) - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)))) {
                    $data[$i]['out_side_week_complete'] = $this->getWeek1(floor((strtotime($data[$i]['registration_date']) - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                } else {
                    $data[$i]['out_side_week_complete'] = 0;
                }
                if ($data[$i]['delivery_status'] == -999) {
                    $data[$i]['current_week'] = $this->getWeek(floor((time() - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                    $data[$i]['day'] = $this->getDay(floor((time() - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                    $data[$i]['current_track'] = $this->trackingDataFormatPregnant($this->Mira_worker_tracking_activity->getPregnantWomenTracking($data[$i]['women_id'], $data[$i]['current_week']));
                } else {
                    $data[$i]['day'] = $this->getDay(floor((time() - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                    $data[$i]['current_week'] = $this->getWeek(floor((strtotime($data[$i]['closing_date']) - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                    $data[$i]['current_track'] = $this->trackingDataFormatPregnant($this->Mira_worker_tracking_activity->getPregnantWomenTracking($data[$i]['women_id'], $data[$i]['current_week']));
                }

                $data[$i]['ans'] = $this->Immuization_women_child_model->getAnswerAshaANMCurrent($data[$i]['pregnant_id'], $data[$i]['current_week']);
                if ($data[$i]['current_week'] > 1) {
                    $data[$i]['last_ans'] = $this->Immuization_women_child_model->getAnswerAshaANMCurrent($data[$i]['pregnant_id'], ($data[$i]['current_week'] - 1));
                    $data[$i]['last_track'] = $this->trackingDataFormatPregnant($this->Mira_worker_tracking_activity->getPregnantWomenTracking($data[$i]['pregnant_id'], ($data[$i]['current_week'] - 1)));
                }
            }
        }

      
        $return['list'] = $data;

        $this->session->set_userdata('page_name', 'mira_pregnant_women_current_week_status');

        $this->load->view('mira_welcome_page', $return);
    }

    public function test() {
        $t1 = "2017-02-09 12:01:29";
        $t2 = "2017-02-09 12:02:21";
        print_r(date('i:s', (strtotime($t2) - strtotime($t1))));
     
    }

    /**
     * 
     * 
     * 
     * 
     */
    private function trackingDataFormatPregnant($_data) {
        $pregnant_id = NULL;
        $week_id = NULL;
        $running_time = NULL;
        $k = 0;
        $sub_module = NULL;
        if ($_data) {
            foreach ($_data as $key => $value) {
                if (!$k) {
                    $k++;
                    $pregnant_id = $value['pregnant_id'];
                    $week_id = $value['week_id'];
                    $running_time = date('i:s', (strtotime($value['end_time']) - strtotime($value['start_time'])));
                }
                $sub_module[] = array('scence_id' => $value['scence_id'], 'status' => $value['scence_status']);
            }
            return array('pregnant_id' => $pregnant_id, 'week_id' => $week_id, 'time' => $running_time, 'sub_module_data' => $this->sortingListArray($sub_module));
        } else {
            return FALSE;
        }
    }
    private function sortingListArray($sub_module){
        $tempModeuleArray=array();
        for($i=0;$i<count($sub_module);$i++){
            for($j=0;$j<count($sub_module);$j++){
                if(($i+1)==$sub_module[$j]['scence_id']){
                    $tempModeuleArray[]=$sub_module[$j];
                    break;
                }
            }
        }
        return $tempModeuleArray;  
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    
    public function getPregnantWomenImmunization()
    {
        $return['regionCode'] = $_POST['region_code'];
        $data = $this->Immuization_women_child_model->getPregnantWomenImmunizatio($return['regionCode'], 1, $_per_page_row);
       
        if (isset($data) and $data) {
            for ($i = 0; $i < count($data); $i++) {
                if ($this->getWeek1(floor((strtotime($data[$i]['registration_date']) - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)))) {
                    $data[$i]['out_side_week_complete'] = $this->getWeek1(floor((strtotime($data[$i]['registration_date']) - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                } else {
                    $data[$i]['out_side_week_complete'] = 0;
                }
                if ($data[$i]['delivery_status'] == -999) {
                    $data[$i]['day'] = $this->getDay(floor((time() - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                    $data[$i]['current_week'] = $this->getWeek(floor((time() - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                } else {
                    $data[$i]['day'] = $this->getDay(floor((time() - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                    $data[$i]['current_week'] = $this->getWeek(floor((strtotime($data[$i]['closing_date']) - strtotime($data[$i]['pregnant_date'])) / (60 * 60 * 24)));
                }
                $data[$i]['ans'] = $this->Immuization_women_child_model->getAnswerAshaANM($data[$i]['pregnant_id']);
                $data[$i]['anc_data'] = $this->Immuization_women_child_model->getPregnantWomenAncStatus($data[$i]['pregnant_id'], $data[$i]['current_week']);
            }
        }
        $return['list'] = $data; 
       print_r(json_encode($return));
        
    }
    public function pregnantImmunizationReport() {

        $_per_page_row = 8;
        $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', 'mira_tracker_pregnant_women');

        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function pregnantImmunizationSubmit() {

        if (isset($_POST['pregnant_id']) and isset($_POST['pregnant_id']) and ! $_POST['submited_status']) {
            $data = array(
                'pregnant_id' => $_POST['pregnant_id'],
                'week_id' => $_POST['week_id'],
                'asha_update_date' => date('Y-m-d')
            );
            $data_answer = NULL;
            $alert = 0;
            $alert_flag = TRUE;
            for ($i = 0; $i < $_POST['question_count']; $i++) {
                if ($alert_flag and $_POST['question_ans' . $i] == 2) {
                    $alert = 1;
                    $alert_flag = FALSE;
                }
                $data_answer[] = array('question_id' => $_POST['question_id' . $i], 'asha_answer' => $_POST['question_ans' . $i]);
            }
            $data['alert'] = $alert;

            //print_r($this->Immuization_women_child_model->pregnantImmunizationSubmitModel($data,$data_answer));


            if ($this->session->userdata('data_submit_status')) {
                $this->session->set_userdata('data_submit_status', FALSE);
                if ($this->Immuization_women_child_model->pregnantImmunizationSubmitModel($data, $data_answer)) {
                    $this->session->set_userdata('status', TRUE);
                    $this->session->set_userdata('message', 'Pregnancy questionnair success.');
                } else {
                    $this->session->set_userdata('data_submit_status', FALSE);
                    $this->session->set_userdata('status', FALSE);
                    $this->session->set_userdata('message', 'Pregnancy questionnair fail.');
                }
            }
        } else {
            $this->session->set_userdata('data_submit_status', FALSE);
            $this->session->set_userdata('status', FALSE);
            $this->session->set_userdata('message', 'Pregnancy questionnair fail.');
        }

        redirect('immunization_pregnant');
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function pregnantWomenImmunization() {
        $this->load->model('mira_applicatio_model');
        //$app_details= $this->mira_applicatio_model->getAppDetailModel($this->session->userdata('app_id'));
        if ($this->session->userdata('app_total_level') == ($this->session->userdata('level') + 1)) {
            redirect('immunization_pregnant_anm/' . $this->uri->segment(2));
        } elseif ($this->session->userdata('app_total_level') == ($this->session->userdata('level'))) {
            redirect('immunization_pregnant_asha');
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function pregnantWomenImmunizationAsha() {
        $return['question'] = $this->Immuization_women_child_model->getPregnamtImmunizationQuestion();
        $return['pregnant_list'] = $this->Immuization_women_child_model->getPregnantWomenByAshaRegionIdModel($this->session->userdata('create_region_id'));
        $return['status'] = $this->session->userdata('status');
        $return['message'] = $this->session->userdata('message');
        $this->session->set_userdata('alert_message_info', 'alert_pregnant_immunization');
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->session->set_userdata('page_name', 'mira_immunization_pregnant_women');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function pregnantWomenImmunizationAnm() {
        $return['activePage'] = $this->uri->segment(2);
        $return['womenList'] = $this->Immuization_women_child_model->getCurrentPregnantListWithCurrentWeekByAnmModel($this->session->userdata('create_region_id'), 1, $this->session->userdata('app_id'), $return['activePage'], $this->per_page_row);
        $return['totalPage'] = $this->getNumberOfPages(count($this->Immuization_women_child_model->getCurrentPregnantListWithCurrentWeekByAnmModel($this->session->userdata('create_region_id'), 1, $this->session->userdata('app_id'))), $this->per_page_row);
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->session->set_userdata('page_name', 'mira_immunization_pregnant_women_by_anm');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    private function getNumberOfPages($_total_number = NULL, $_per_page_row = NULL) {
        if ($_total_number != NULL and $_per_page_row != NULL) {
            if (!($_total_number % $_per_page_row)) {
                return (int) ($_total_number / $_per_page_row);
            } else {
                return((int) ($_total_number / $_per_page_row) + 1);
            }
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getCurrentWeekPregnancy() {
        $return = NULL;
        $data_pregnant = $this->Immuization_women_child_model->getCurrentWeekPregnancyModel($_POST['pregnant_id']);
        if ($data_pregnant) {

            $return['current_week'] = $this->getWeek(floor((time() - strtotime($data_pregnant[0]['pregnant_date'])) / (60 * 60 * 24)));
            $return['submit_status'] = $this->Immuization_women_child_model->getCurrentWeekStatus($_POST['pregnant_id'], $return['current_week']);
            $return['status'] = TRUE;
            $return['lpm_date'] = date('m-d-Y', strtotime($data_pregnant[0]['pregnant_date']));
        } else {
            $return['status'] = FALSE;
        }
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getWeek($_number_of_days = NULL) {
        if ($_number_of_days != NULL) {
           return (int) ($_number_of_days / 7)+1;
            
            
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getDay($_number_of_days = NULL) {
        if ($_number_of_days != NULL) {
            if ($_number_of_days % 7) {
                return $_number_of_days % 7;
            } else {
                return 0;
            }
        }
    }

    /**
     * 
     * @param type $_number_of_days
     * @return type
     * 
     * This function return number of week between two date.
     */
    private function getWeek1($_number_of_days = NULL) {
        if ($_number_of_days != NULL) {
            return (int) ($_number_of_days / 7);
        }
    }

}
