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

    public function __construct() {

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

    /**
     * This function used particulor row block. 
     */
    public function updateHouseRow() {
        if ($this->Registration_house_women_child_model->houseUpdateModel(array($_POST['auto_id']))) {
            print_r(json_encode(array('status' => TRUE, 'house_id' => $_POST['auto_id'])));
        } else {
            print_r(json_encode(array('status' => FALSE)));
        }
    }

    /**
     * 
     * 
     * 
     */
    public function updateWomenRow() {
        if ($this->Registration_house_women_child_model->womenUpdateModel(array($_POST['women_id']))) {
            print_r(json_encode(array('status' => TRUE, 'women_id' => $_POST['women_id'])));
        } else {
            print_r(json_encode(array('status' => FALSE)));
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function childUpdateRow() {
        if ($this->Registration_house_women_child_model->childUpdateModel(array($_POST['child_id']))) {
            print_r(json_encode(array('status' => TRUE, 'child_id' => $_POST['child_id'])));
        } else {
            print_r(json_encode(array('status' => FALSE)));
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function updatePregnanByPregnantId() {


        //$this->db->set('points', 'points + $points');
        $pregnant_id = $_POST['pregnant_id'];
        $update_data = NULL;
        $newWeek = 0;
        $oldWeek = 0;
        $sign = NULL;
        $numWeek = 0;
        if (isset($_POST['lpm_date']) and $_POST['lpm_date'] != '') {
            $newWeek = $this->getWeek(floor((time() - strtotime($_POST['lpm_date'])) / (60 * 60 * 24)));
            $oldWeek = $this->getWeek(floor((time() - strtotime($_POST['old_lpm_date'])) / (60 * 60 * 24)));
            $update_data['pregnant_date'] = date('Y-m-d', strtotime($_POST['lpm_date']));
            if ($newWeek > $oldWeek) {
                $sign = '+';
                $numWeek = $newWeek - $oldWeek;
            } else {
                $sign = '-';
                $numWeek = $oldWeek - $newWeek;
            }
        }
        if (isset($_POST['pregnant_status']) and $_POST['pregnant_status'] != '') {
            $update_data['delivery_status'] = $_POST['pregnant_status'];
            if ($_POST['pregnant_status'] == '-999') {
                $update_data['pregnant_status'] = 1;
                $update_data['closing_date'] = NULL;
            }
        }
        $weekUpdate = $this->Registration_house_women_child_model->updateWeekAttempted($pregnant_id, $numWeek, $sign);
        $data = $this->Registration_house_women_child_model->updatePregnanByPregnantIdModel($pregnant_id, $update_data);
        if ($data and $weekUpdate) {
            print_r(json_encode(array('status' => $data, 'week' => $newWeek)));
        } else {
            print_r(json_encode(array('status' => FALSE)));
        }
//
    }

    public function childRegistrationForm() {
        $return['house_list'] = $this->Registration_house_women_child_model->getHouseRegionIdModel($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_child_registration');
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->session->set_userdata('page_name', 'mira_registration_child');
        $this->load->view('mira_welcome_page', $return);
    }

    public function childRegistrationSubmit() {
        $data = array(
            'women_id' => $_POST['women_id'],
            'dob' => date('Y-m-d', strtotime($_POST['child_dob'])),
            'child_name' => $_POST['child_full_name'],
            'gender' => $_POST['gender']
        );
        if ($this->session->userdata('data_submit_status')) {
            $this->session->set_userdata('data_submit_status', FALSE);
            if ($this->Registration_house_women_child_model->childRegistrationModel($data)) {
                $this->session->set_userdata('status', TRUE);
                $this->session->set_userdata('message', 'Child registration success.');
            } else {
                $this->session->set_userdata('data_submit_status', FALSE);
                $this->session->set_userdata('status', FALSE);
                $this->session->set_userdata('message', 'Child registration fail.');
            }
        }
        redirect('child_form');
    }

    public function getWomenForChildRegistration() {
        $return['women_list'] = $this->Registration_house_women_child_model->getWomenModel(array('house_id' => $_POST['house_id']));
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function pregnantRegistrationForm() {

        $return['house_list'] = $this->Registration_house_women_child_model->getHouseRegionIdModel($this->session->userdata('create_region_id'));
        $return['status'] = $this->session->flashdata('status');
        $return['message'] = $this->session->flashdata('message');
        $this->session->set_userdata('alert_message_info', 'alert_pregnant_registration');
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->session->set_userdata('page_name', 'mira_registration_pregnant');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function pregnantRegistrationSubmit() {
        $data = array(
            'women_id' => $_POST['women_id'],
            'pregnant_date' => date('Y-m-d', strtotime($_POST['lpm_date_input'])),
        );

        if ($this->session->userdata('data_submit_status')) {
            $this->session->set_userdata('data_submit_status', FALSE);
            if ($this->Registration_house_women_child_model->pregnantRegistrationModel($data)) {
                $this->session->set_userdata('status', TRUE);
                $this->session->set_userdata('message', 'Pregnant registration success.');
            } else {
                $this->session->set_userdata('data_submit_status', FALSE);
                $this->session->set_userdata('status', FALSE);
                $this->session->set_userdata('message', 'Pregnant registration fail.');
            }
        }
        redirect('pregnant_form');
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getWomenForPregnantRegistration() {
        $return['women_list'] = $this->Registration_house_women_child_model->getWomenModel(array('house_id' => $_POST['house_id'], 'pregnant_status' => 0));
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function womenRegistrationForm() {
        $return['house_list'] = $this->Registration_house_women_child_model->getHouseRegionIdModel($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_women_registration');
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->session->set_userdata('page_name', 'mira_registration_women');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function womenRegistrationSubmit() {
        $data = array(
            'house_id' => $_POST['house_number'],
            'name' => $_POST['women_name'],
            'dob' => date('Y-m-d', strtotime($_POST['women_dob'])),
            'husband' => $_POST['husband_name'],
            'pregnant_status' => $_POST['pregnant_status']
        );
        $lpm_date_input = NULL;
        if ($_POST['pregnant_status']) {
            $lpm_date_input = date('Y-m-d', strtotime($_POST['lpm_date_input']));
        }

        if ($this->session->userdata('data_submit_status')) {
            $this->session->set_userdata('data_submit_status', FALSE);
            if ($this->Registration_house_women_child_model->womenRegistrationModel($data, $lpm_date_input)) {
                $this->session->set_userdata('status', TRUE);
                $this->session->set_userdata('message', 'Women registration success.');
            } else {
                $this->session->set_userdata('data_submit_status', FALSE);
                $this->session->set_userdata('status', FALSE);
                $this->session->set_userdata('message', 'Women registration fail.');
            }
        }
        redirect('women_form');
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function houseRegistrationForm() {

        $return['status'] = $this->session->userdata('status');
        $return['message'] = $this->session->userdata('message');
        $this->session->set_userdata('alert_message_info', 'alert_house_registration');
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->session->set_userdata('page_name', 'mira_registration_house');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function houseRegistrationSubmit() {



        $data = array(
            'house_number' => $_POST['house_number'],
            'total_family_member' => $_POST['total_family'],
            'married_women' => $_POST['married_women'],
            'unmarried_women' => $_POST['unmarried_women'],
            'total_adolenc_girl' => $_POST['adolescence'],
            'total_child' => $_POST['child'],
            'house_head_name' => $_POST['full_name'],
            'region_id' => $this->session->userdata('create_region_id')
        );
        if ($this->session->userdata('data_submit_status')) {
            $this->session->set_userdata('data_submit_status', FALSE);
            if ($this->Registration_house_women_child_model->houseRegistrationModel($data)) {
                $this->session->set_userdata('status', TRUE);
                $this->session->set_userdata('message', 'House registration success.');
            } else {
                $this->session->set_userdata('data_submit_status', FALSE);
                $this->session->set_userdata('status', FALSE);
                $this->session->set_userdata('message', 'House registration fail.');
            }
        }
        redirect('house_form');
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getHouseListByRegionID() {
        $return['activePage'] = '';
        $return['regionCode'] = '';
        $this->session->set_userdata('asharegionId', 0);
        $this->session->set_userdata('anmregionId', 0);
        $this->session->set_userdata('start_date', '');
        $this->session->set_userdata('end_date', '');
        $this->session->set_userdata('search_by', 0);
        switch (returnMiraAnm($this->session->userdata('level'), $this->session->userdata('app_total_level'))) {
            case 1:
                $per_page_row = 10;
                $return['activePage'] = $this->uri->segment(2);
                $return['houseList'] = $this->Registration_house_women_child_model->getHouseListByRegionCodeModel($this->session->userdata('create_region_id'), $return['activePage'], $per_page_row);
                $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getHouseListByRegionCodeModel($this->session->userdata('create_region_id'))), $per_page_row);
                break;
            case 2:
                $per_page_row = 8;
                $return['miraList'] = $this->Registration_house_women_child_model->getAshaListWithRegionCode($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                $return['activePage'] = $this->uri->segment(2);
                $return['regionCode'] = $this->uri->segment(3);
                if ($return['activePage'] != '' and $return['regionCode'] != '') {
                    $return['houseList'] = $this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
                    $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'])), $per_page_row);
                }
                break;
            case 3:
                $per_page_row = 8;
                $return['anmList'] = $this->Registration_house_women_child_model->getANMListWithAppId($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                 $return['activePage'] = $this->uri->segment(2);
                 $return['regionCode'] = $this->uri->segment(3);
                 if ($return['activePage'] != ''and $return['regionCode'] != '') {
                     $return['houseList'] = $this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
                     $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'])), $per_page_row);
                 }
                
                
                break;
        }

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

    /**
     * 
     * 
     * 
     * 
     */
    public function getHouseListByRegionIDJquery() {
        if (isset($_POST['region_code'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
            $return['totalRecords'] = $this->Registration_house_women_child_model->getHouseListTotalRecordsByRegionCodeModel($return['regionCode']);
            $return['houseList'] = $this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
            $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'])), $per_page_row);
            print_r(json_encode($return));
        }
    }

    public function getHouseListByRegionIdUsingPagination() {
        if (isset($_POST['regionCode'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['regionCode'];
            $return['pageNumber'] = $_POST['pageNumber'];
            $return['activePage'] = 1;
            $return['houseList'] = $this->Registration_house_women_child_model->getHouseListUsingPagination($return['regionCode'], $return['pageNumber']);
            // $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'])), $per_page_row);
            print_r(json_encode($return));
        }
    }

    public function getHouseListByRegionIDJqueryVersion2() {
        if (isset($_POST['region_code'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
            $return['stDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['start_date'])));
            $return['enDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['end_date'])));
            $return['searchBy'] = $_POST['search_by'];
            $return['houseList'] = $this->Registration_house_women_child_model->getHouseListByRegionCodeModelV2($return['regionCode'], $return['activePage'], $per_page_row,$return['stDate'],$return['enDate'],$return['searchBy']);
            $return['totalRecords'] = $this->Registration_house_women_child_model->getTotalHouseByRegionCodeModelV2($return['regionCode'], $return['activePage'], $per_page_row,$return['stDate'],$return['enDate'],$return['searchBy']);
            if($return['totalRecords'] > 8){
                $return['totalPage'] = ($return['totalRecords'] / 8) + ($return['totalRecords'] % 8);
            }
            else{
                $return['totalPage'] = 1;
            }
            print_r(json_encode($return));
        }
    }

    public function getFilteredHouseDataUsingPagination(){
        if (isset($_POST['region_code'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
            $return['stDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['start_date'])));
            $return['enDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['end_date'])));
            $return['searchBy'] = $_POST['search_by'];
            $return['pageNumber'] = $_POST['page_number'];
            $return['houseList'] = $this->Registration_house_women_child_model->getHouseListByRegionCodeModelV2Pagination($return['pageNumber'], $return['regionCode'], $return['stDate'], $return['enDate'], $return['searchBy']);
            print_r(json_encode($return));
        }
    }

    public function getFilteredWomenDataUsingPagination(){
        if (isset($_POST['region_code'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
            $return['stDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['start_date'])));
            $return['enDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['end_date'])));
            $return['searchBy'] = $_POST['search_by'];
            $return['pageNumber'] = $_POST['page_number'];
            $return['womenList'] = $this->Registration_house_women_child_model->getWomenListByRegionCodeModelV2Pagination($return['pageNumber'], $return['regionCode'], $return['stDate'], $return['enDate'], $return['searchBy']);
            print_r(json_encode($return));
        }
    }

    public function getFilteredChildDataUsingPagination(){
        if (isset($_POST['region_code'])) {
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
            $return['stDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['start_date'])));
            $return['enDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['end_date'])));
            $return['searchBy'] = $_POST['search_by'];
            $return['pageNumber'] = $_POST['page_number'];
            $return['childList'] = $this->Registration_house_women_child_model->getChildListByRegionCodeModelV2Pagination($return['pageNumber'], $return['regionCode'], $return['stDate'], $return['enDate'], $return['searchBy']);
            print_r(json_encode($return));
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    private function getTotaoPage($_total_row = NULL, $_per_page_row = NULL) {
        if (!($_total_row % $_per_page_row)) {
            return (int) ($_total_row / $_per_page_row);
        } else {
            return ((int) ($_total_row / $_per_page_row) + 1);
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getWomenListByRegionID() {
        $return = NULL;
        switch (returnMiraAnm($this->session->userdata('level'), $this->session->userdata('app_total_level'))) {
            case 1:
                $_per_page_row = 10;
                $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getWomenListByRegionCodeModel($this->session->userdata('create_region_id'))), $_per_page_row);
                $return['activePage'] = $this->uri->segment(2);
                $return['womenList'] = $this->Registration_house_women_child_model->getWomenListByRegionCodeModel($this->session->userdata('create_region_id'), $return['activePage'], $_per_page_row);
                break;
            case 2:
                $per_page_row = 8;
                $return['miraList'] = $this->Registration_house_women_child_model->getAshaListWithRegionCode($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                $return['activePage'] = $this->uri->segment(2);
                $return['regionCode'] = $this->uri->segment(3);
                if ($return['activePage'] != ''and $return['regionCode'] != '') {
                    $return['womenList'] = $this->Registration_house_women_child_model->getWomenListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
                    $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getWomenListByRegionCodeModel($return['regionCode'])), $per_page_row);
                }
                break;
                
                case 3:
                $per_page_row = 8;
                $return['anmList'] = $this->Registration_house_women_child_model->getANMListWithAppId($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                $return['flag'] = true;
                break;
        }
        $this->session->set_userdata('page_name', 'mira_registered_list_women');
        $this->load->view('mira_welcome_page', $return);
        //print_r($return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getWomenListByRegionIDJquery() {
        if (isset($_POST['region_code'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
            $return['totalRecords'] = $this->Registration_house_women_child_model->getWomenListTotalRecordsByRegionCodeModel($return['regionCode']);
            $return['womenList'] = $this->Registration_house_women_child_model->getWomenListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
            $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getWomenListByRegionCodeModel($return['regionCode'])), $per_page_row);
            print_r(json_encode($return));
        }
    }

    public function getWomenListByRegionIdUsingPagination() {
        if (isset($_POST['regionCode'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['regionCode'];
            $return['pageNumber'] = $_POST['pageNumber'];
            $return['activePage'] = 1;
            $return['womenList'] = $this->Registration_house_women_child_model->getWomenListUsingPagination($return['regionCode'], $return['pageNumber']);
            print_r(json_encode($return));
        }
    }

    public function getWomenListByRegionIDJqueryV2() {
        if (isset($_POST['region_code'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
            $return['stDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['start_date']))) ;
            $return['enDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['end_date']))) ;
            $return['searchBy'] = $_POST['search_by'];
            $return['womenList'] = $this->Registration_house_women_child_model->getWomenListByRegionCodeModelV2($return['regionCode'], $return['activePage'], $per_page_row,$return['stDate'],$return['enDate'],$return['searchBy']);
            $return['totalRecords'] = $this->Registration_house_women_child_model->getTotalWomenByRegionCodeModelV2($return['regionCode'], $return['activePage'], $per_page_row,$return['stDate'],$return['enDate'],$return['searchBy']);
            if($return['totalRecords'] > 8){
                $return['totalPage'] = ($return['totalRecords'] / 8) + ($return['totalRecords'] % 8);
            }
            else{
                $return['totalPage'] = 1;
            }
            print_r(json_encode($return));
        }
    }
    
    /**
     * This block run for running women list.
     * 
     * 
     */
    public function getPregnantRunningListByRegionID() {
        $this->load->model('Immuization_women_child_model');
        $return = NULL;
        switch (returnMiraAnm($this->session->userdata('level'), $this->session->userdata('app_total_level'))) {
            case 1:
                $_per_page_row = 8;
                $return['activePage'] = $this->uri->segment(2);
                $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getPregnantListByRegionCodeRunningModel($this->session->userdata('create_region_id'))), $_per_page_row);
                $temp_pregnant_list = $this->Registration_house_women_child_model->getPregnantListByRegionCodeRunningModel($this->session->userdata('create_region_id'), $this->uri->segment(2), $_per_page_row);
                for ($i = 0; $i < count($temp_pregnant_list); $i++) {
                    $temp_pregnant_list[$i]['anc_data'] = $this->Immuization_women_child_model->getPregnantWomenAncStatus($temp_pregnant_list[$i]['pregnant_id'], $this->getWeek(floor((time() - strtotime($temp_pregnant_list[$i]['pregnant_date'])) / (60 * 60 * 24))));
                }
                $return['pregnantList'] = $temp_pregnant_list;
                break;
            case 2:
                $per_page_row = 7;
                $return['miraList'] = $this->Registration_house_women_child_model->getAshaListWithRegionCode($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                $return['activePage'] = $this->uri->segment(2);
                $return['regionCode'] = $this->uri->segment(3);
                if ($return['activePage'] != '' and $return['regionCode'] != '') {
                    $temp_pregnant_list = $this->Registration_house_women_child_model->getPregnantListByRegionCodeRunningModel($this->session->userdata('create_region_id'), $this->uri->segment(2), $_per_page_row);
                    for ($i = 0; $i < count($temp_pregnant_list); $i++) {
                        $temp_pregnant_list[$i]['anc_data'] = $this->Immuization_women_child_model->getPregnantWomenAncStatus($temp_pregnant_list[$i]['pregnant_id'], $this->getWeek(floor((time() - strtotime($temp_pregnant_list[$i]['pregnant_date'])) / (60 * 60 * 24))));
                    }
                    $return['pregnantList'] = $temp_pregnant_list;
                    $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getPregnantListByRegionCodeRunningModel($return['regionCode'])), $per_page_row);
                }

                break;
                
                
         case 3:
                $per_page_row = 8;
                $return['anmList'] = $this->Registration_house_women_child_model->getANMListWithAppId($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                 $return['activePage'] = $this->uri->segment(2);
                 $return['regionCode'] = $this->uri->segment(3);
                 if ($return['activePage'] != ''and $return['regionCode'] != '') {
                     $return['houseList'] = $this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
                     $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'])), $per_page_row);
                 }
                
                
                break;       
        }
        $this->session->set_userdata('page_name', 'mira_registered_list_pregnant_running');
        // print_r($return['pregnantList']);
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getPregnantListByRegionID() {
        $this->load->model('Immuization_women_child_model');
        $return = NULL;
        switch (returnMiraAnm($this->session->userdata('level'), $this->session->userdata('app_total_level'))) {
            case 1:
                $_per_page_row = 8;
                $return['activePage'] = $this->uri->segment(2);
                $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($this->session->userdata('create_region_id'))), $_per_page_row);
                $temp_pregnant_list = $this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($this->session->userdata('create_region_id'), $this->uri->segment(2), $_per_page_row);
                if ($temp_pregnant_list) {
                    for ($i = 0; $i < count($temp_pregnant_list); $i++) {
                        $temp_pregnant_list[$i]['anc_data'] = $this->Immuization_women_child_model->getPregnantWomenAncStatusDeliver($temp_pregnant_list[$i]['pregnant_id'], $this->getWeek(floor((strtotime($temp_pregnant_list[$i]['closing_date']) - strtotime($temp_pregnant_list[$i]['pregnant_date'])) / (60 * 60 * 24))));
                    }
                }
                $return['pregnantList'] = $temp_pregnant_list;
                break;
            case 2:
                $per_page_row = 7;
                $return['miraList'] = $this->Registration_house_women_child_model->getAshaListWithRegionCode($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                $return['activePage'] = $this->uri->segment(2);
                $return['regionCode'] = $this->uri->segment(3);
                if ($return['activePage'] != '' and $return['regionCode'] != '') {
                    $temp_pregnant_list = $this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($this->session->userdata('create_region_id'), $this->uri->segment(2), $_per_page_row);
                    for ($i = 0; $i < count($temp_pregnant_list); $i++) {
                        $temp_pregnant_list[$i]['anc_data'] = $this->Immuization_women_child_model->getPregnantWomenAncStatusDeliver($temp_pregnant_list[$i]['pregnant_id'], $this->getWeek(floor((strtotime($temp_pregnant_list[$i]['closing_date']) - strtotime($temp_pregnant_list[$i]['pregnant_date'])) / (60 * 60 * 24))));
                    }
                    $return['pregnantList'] = $temp_pregnant_list;
                    $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($return['regionCode'])), $per_page_row);
                }

                break;
                
            case 3:
                $per_page_row = 8;
                $return['anmList'] = $this->Registration_house_women_child_model->getANMListWithAppId($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                 $return['activePage'] = $this->uri->segment(2);
                 $return['regionCode'] = $this->uri->segment(3);
                 if ($return['activePage'] != ''and $return['regionCode'] != '') {
                     $return['houseList'] = $this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
                     $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'])), $per_page_row);
                 }
                
                
                break;
   
                
        }
        $this->session->set_userdata('page_name', 'mira_registered_list_pregnant');
        //print_r($return['pregnantList']);
        $this->load->view('mira_welcome_page', $return);
    }
    
     public function childImmunization() {
        $this->load->model('Immuization_women_child_model');
        $return = NULL;
        switch (returnMiraAnm($this->session->userdata('level'), $this->session->userdata('app_total_level'))) {
            case 1:
                $_per_page_row = 8;
                $return['activePage'] = $this->uri->segment(2);
                $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($this->session->userdata('create_region_id'))), $_per_page_row);
                $temp_pregnant_list = $this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($this->session->userdata('create_region_id'), $this->uri->segment(2), $_per_page_row);
                if ($temp_pregnant_list) {
                    for ($i = 0; $i < count($temp_pregnant_list); $i++) {
                        $temp_pregnant_list[$i]['anc_data'] = $this->Immuization_women_child_model->getPregnantWomenAncStatusDeliver($temp_pregnant_list[$i]['pregnant_id'], $this->getWeek(floor((strtotime($temp_pregnant_list[$i]['closing_date']) - strtotime($temp_pregnant_list[$i]['pregnant_date'])) / (60 * 60 * 24))));
                    }
                }
                $return['pregnantList'] = $temp_pregnant_list;
                break;
            case 2:
                $per_page_row = 7;
                $return['miraList'] = $this->Registration_house_women_child_model->getAshaListWithRegionCode($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                $return['activePage'] = $this->uri->segment(2);
                $return['regionCode'] = $this->uri->segment(3);
                if ($return['activePage'] != '' and $return['regionCode'] != '') {
                    $temp_pregnant_list = $this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($this->session->userdata('create_region_id'), $this->uri->segment(2), $_per_page_row);
                    for ($i = 0; $i < count($temp_pregnant_list); $i++) {
                        $temp_pregnant_list[$i]['anc_data'] = $this->Immuization_women_child_model->getPregnantWomenAncStatusDeliver($temp_pregnant_list[$i]['pregnant_id'], $this->getWeek(floor((strtotime($temp_pregnant_list[$i]['closing_date']) - strtotime($temp_pregnant_list[$i]['pregnant_date'])) / (60 * 60 * 24))));
                    }
                    $return['pregnantList'] = $temp_pregnant_list;
                    $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($return['regionCode'])), $per_page_row);
                }

                break;
                
            case 3:
                $per_page_row = 8;
                $return['anmList'] = $this->Registration_house_women_child_model->getANMListWithAppId($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                 $return['activePage'] = $this->uri->segment(2);
                 $return['regionCode'] = $this->uri->segment(3);
                 if ($return['activePage'] != ''and $return['regionCode'] != '') {
                     $return['houseList'] = $this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
                     $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getHouseListByRegionCodeModel($return['regionCode'])), $per_page_row);
                 }
                
                
                break;
   
                
        }
        $this->session->set_userdata('page_name', 'mira_child_vaccination');
        //print_r($return['pregnantList']);
        $this->load->view('mira_welcome_page', $return);
    }
    /**
     * 
     * 
     * 
     * 
     */
    public function getWeek($_number_of_days = NULL) {

        if ($_number_of_days != NULL) {
            $num_week = (int) ($_number_of_days / 7);
            if ($_number_of_days % 7) {
                $num_week++;
            }
            return $num_week;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getPregnantListByRegionIDJquery() {
        if (isset($_POST['region_code'])) {
            $per_page_row = 7;
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
            $return['pregnantList'] = $this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
            for ($i = 0; $i < count($return['pregnantList']); $i++) {
                if ($return['pregnantList'][$i]['delivery_status'] == -999) {
                    $day = floor((time() - strtotime($return['pregnantList'][$i]['pregnant_date'])) / (60 * 60 * 24));
                } else {
                    $day = floor((strtotime($return['pregnantList'][$i]['closing_date']) - strtotime($return['pregnantList'][$i]['pregnant_date'])) / (60 * 60 * 24));
                }
                if ($day % 7 == 0) {
                    $num_week = (int) ($day / 7);
                } else {
                    $num_week = (int) ($day / 7) + 1;
                }
                $return['pregnantList'][$i]['current_week'] = $num_week;
            }


           // $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($return['regionCode'])), $per_page_row);
            print_r(json_encode($return));
        }
    }
    
    public function getChildVaccination() {
        if (isset($_POST['region_code'])) {
            $return['regionCode'] = $_POST['region_code'];
            $return['vaccineData'] = $this->Registration_house_women_child_model->getChildVaccinationByRegioncode($return['regionCode']);
            print_r(json_encode($return));
        }
    }
    
     public function getPregnantRunningListByRegionIDJquery() {
        if (isset($_POST['region_code'])) {
            $per_page_row = 7;
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
            $return['pregnantList'] = $this->Registration_house_women_child_model->getPregnantRunningListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
            for ($i = 0; $i < count($return['pregnantList']); $i++) {
                if ($return['pregnantList'][$i]['delivery_status'] == -999) {
                    $day = floor((time() - strtotime($return['pregnantList'][$i]['pregnant_date'])) / (60 * 60 * 24));
                } else {
                    $day = floor((strtotime($return['pregnantList'][$i]['closing_date']) - strtotime($return['pregnantList'][$i]['pregnant_date'])) / (60 * 60 * 24));
                }
                if ($day % 7 == 0) {
                    $num_week = (int) ($day / 7);
                } else {
                    $num_week = (int) ($day / 7) + 1;
                }
                $return['pregnantList'][$i]['current_week'] = $num_week;
            }


         
            print_r(json_encode($return));
        }
    }
    /**
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getChildListByRegionID() {
        
      //  print_r($this->session->userdata('level'));
      //  print_r($this->session->userdata('app_total_level'));
      //  print_r($this->session->userdata('create_region_id'));
      //  die();
        $return = NULL;
        switch (returnMiraAnm($this->session->userdata('level'), $this->session->userdata('app_total_level'))) {
            case 1:
                $_per_page_row = 10;
                $return['activePage'] = $this->uri->segment(2);
                $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getChildListByRegionCodeModel($this->session->userdata('create_region_id'))), $_per_page_row);
                $return['childList'] = $this->Registration_house_women_child_model->getChildListByRegionCodeModel($this->session->userdata('create_region_id'), $this->uri->segment(2), $_per_page_row);
                break;
            case 2:
                $_per_page_row = 8;
                $return['miraList'] = $this->Registration_house_women_child_model->getAshaListWithRegionCode($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                $return['activePage'] = $this->uri->segment(2);
                $return['regionCode'] = $this->uri->segment(3);
                if ($return['activePage'] != ''and $return['regionCode'] != '') {
                    $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getChildListByRegionCodeModel($return['regionCode'])), $_per_page_row);
                    $return['childList'] = $this->Registration_house_women_child_model->getChildListByRegionCodeModel($return['regionCode'], $return['activePage'], $_per_page_row);
                }
                break;
                
            case 3:
                $per_page_row = 8;
                $return['anmList'] = $this->Registration_house_women_child_model->getANMListWithAppId($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
                $return['flag'] = false;
               
                break;
        }
        $this->session->set_userdata('page_name', 'mira_registered_list_child');
        //print_r($return);
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getChildListByRegionIDJquery() {
        if (isset($_POST['region_code'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
            $return['totalRecords']= $this->Registration_house_women_child_model->getChildListTotalRecordsByRegionCodeModel($return['regionCode']);
            $return['childList']= $this->Registration_house_women_child_model->getChildListByRegionCodeModel($return['regionCode'], $return['activePage'], $per_page_row);
            $return['totalPage'] = ($return['totalRecords'] / 8) + ($return['totalRecords'] % 8);

            print_r(json_encode($return));
        }
    }

    public function getChildListByRegionIdUsingPagination(){
        if (isset($_POST['regionCode'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['regionCode'];
            $return['pageNumber'] = $_POST['pageNumber'];
            $return['childList'] = $this->Registration_house_women_child_model->getChildListUsingPagination($return['regionCode'], $return['pageNumber']);
            print_r(json_encode($return));
        }
    }  
    
    public function getChildListByRegionIDJqueryV2() {
        if (isset($_POST['region_code'])) {
            $per_page_row = 8;
            $return['regionCode'] = $_POST['region_code'];
            $return['activePage'] = 1;
             $return['stDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['start_date']))) ;
            $return['enDate'] =date('Y-m-d', strtotime(str_replace('/', '-', $_POST['end_date']))) ;
            $return['searchBy'] = $_POST['search_by'];
            $return['childList']= $this->Registration_house_women_child_model->getChildListByRegionCodeModelV2($return['regionCode'], $return['activePage'], $per_page_row,$return['stDate'],$return['enDate'],$return['searchBy']);
            $return['totalRecords']= $this->Registration_house_women_child_model->getTotalChildByRegionCodeModelV2($return['regionCode'], $return['activePage'], $per_page_row,$return['stDate'],$return['enDate'],$return['searchBy']);
            if($return['totalRecords'] > 8){
                $return['totalPage'] = ($return['totalRecords'] / 8) + ($return['totalRecords'] % 8);
            }
            else{
                $return['totalPage'] = 1;
            }
            print_r(json_encode($return));
        }
    }
    
     ///Rahim code
    public function getWomenListByRegionIDFromHouseList()
    {
        // Get the house ID from the URI segment
        $house_id = $this->uri->segment(3);
        $asha_name = $this->uri->segment(4);

        $encodedString = $asha_name;

        // Decode the URL-encoded string
        $decodedString = urldecode($encodedString);

        // Capitalize the first letter of each word
        $ashaName = ucwords(strtolower($decodedString));

        // echo $ashaName;
        // die;

        // Initialize the return array
        $return = array();

        // Check if the house ID is not empty
        if ($house_id !== FALSE) {
            // Retrieve the women list by house ID
            $womenListFromHousePage = $this->Registration_house_women_child_model->getWomenListByRegionCode($house_id);

            // Check if womenList is not empty
            if ($womenListFromHousePage) {
                // Assign modified womenList to return array
                $return['womenListHouse'] = $womenListFromHousePage;

                // Chaneges 01-march-2023

                $return['flag'] = false;

                // changes end

                $this->session->unset_userdata('ashaName');
                $this->session->set_userdata('ashaName', $ashaName);
                // Loop through each woman to access house_id
                // foreach ($womenListFromHousePage as $woman) {
                //     echo $woman['house_id']; // Access 'house_id' for each woman
                // }
                // die;
            } else {
                // Handle the case where no women list is retrieved
                $return['flag'] = true;
                $return['message'] = "No women list found for the specified Asha.";
            }
        } else {
            // Handle the case where the house ID is not provided
            $return['message'] = "House ID is missing in the URI.";
        }


        // Set session data
        $this->session->set_userdata('page_name', 'mira_registered_list_women');
        // $ashaName=$this->session->userdata('ashaName');
        $this->session->set_userdata('backUrlChild', base_url('registrationhousewomenchild/getWomenListByRegionIDFromHouseList/' . $house_id));

        // Load the view with the return data
        $this->load->view('mira_welcome_page', $return);
    }


    public function getChildrenListByWomenId()
    {
        // Get the house ID from the URI segment
        $women_id = $this->uri->segment(3);
       // $women_name = $this->uri->segment(4);

        // Initialize the return array
        $return = array();

        // Check if the house ID is not empty
        if ($women_id !== FALSE) {
            // Retrieve the women list by house ID
            $childrenList = $this->Registration_house_women_child_model->getChildrenListByWomenId($women_id);
            // echo $women_name;
            // die;
            $return['flag'] = true;
            // Check if womenList is not empty
            if ($childrenList) {
                $return['childrenList'] = $childrenList;

               /* if ($women_name) {
                    $return['women_name'] = $women_name;
                } else {
                    $return['women_name'] = '';
                }*/
            } else {
                $return['message'] = "No Child list found for the specified Women.";
               // $return['women_name'] = $women_name;
                $return['childrenList'] = NULL;
            }

        } else {
            // Handle the case where the house ID is not provided
            $return['message'] = "House ID is missing in the URI.";
        }

        // var_dump($childrenList);
        // die();

        $this->session->set_userdata('page_name', 'mira_registered_list_child');
        // print_r($return);
        $this->load->view('mira_welcome_page', $return);
    }
    //End of Rahim code



    public function getAshalistByANMId()
    {
        if (isset($_POST['region_code'])) {
            $return['miraList'] = $this->Registration_house_women_child_model->getAshaListWithRegionCode($this->session->userdata('app_id'), $_POST['region_code']);
        }
        print_r(json_encode($return));
    }

    public function getAshaHouseHoldDataByANMId()
    {
        if (isset($_POST['region_code'])) {
           
            $stdate=date('Y-m-d', strtotime(str_replace('/', '-', $_POST['st_date_input']))) ;
            $endate=date('Y-m-d', strtotime(str_replace('/', '-', $_POST['en_date_input']))) ;
            $searchby=$_POST['searchbydate'];
            
                         log_message('error', 'Something went wrong');
log_message('info', 'Info message');
// Log values
log_message('error', 'Start Date: ' . $_POST['st_date_input']);
log_message('error', 'End Date: ' . $_POST['en_date_input']);
log_message('error', 'Search By: ' . $_POST['searchbydate']);
log_message('error', 'Region By: ' . $_POST['region_code']);
                     
            $return['miraList'] = $this->Registration_house_women_child_model->getAshaHouseHoldDataWithRegionCode($this->session->userdata('app_id'), $_POST['region_code'],$stdate,$endate,$searchby);
        }
        
        print_r(json_encode($return));
    }


    public function backmethodcalling()
    {
        // Access $param1 and $param2 here
        // echo "Parameter 1: " . $param1 . "<br>";
        // echo "Parameter 2: " . $param2 . "<br>";

        $per_page_row = 8;
        $return['anmList'] = $this->Registration_house_women_child_model->getANMListWithAppId($this->session->userdata('app_id'), $this->session->userdata('create_region_id'));
        $return['miraList'] = $this->Registration_house_women_child_model->getAshaListWithRegionCode($this->session->userdata('app_id'), $this->session->userdata('anmregionId'));
        $return['activePage'] = 1;
        $return['regionCode'] = $this->uri->segment(3);
        if ($return['activePage'] != '') {
            $return['houseList'] = $this->Registration_house_women_child_model->getHouseListByRegionCodeModel($this->session->userdata('asharegionId'), $return['activePage'], $per_page_row);
            $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getHouseListByRegionCodeModel($this->session->userdata('asharegionId'))), $per_page_row);
        }

        // print_r($return['houseList']) ;   die();

        $this->session->set_userdata('page_name', 'mira_registered_list_house');
        $this->load->view('mira_welcome_page', $return);



    }
    
    //new changes
    public function getAshaDeatilByANMId()
     {
         header('Content-Type: application/json'); // Ensure JSON response

         $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->Registration_house_women_child_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; // Ensure script execution stops here
     }

}
