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

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper(array('url', 'form'));
        $this->load->model('Worker_tracker_sheet_model');
        if (!$this->session->userdata('is_user_loged')) {
            redirect('login');
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getAnmWorkSheetHRP() {
        //$return = $this->getPregnantWomenStatus($this->uri->segment(3), $this->uri->segment(2));
        $return['active_button'] = 1;
        $return['activePage']=$this->uri->segment(2);
        $_per_page_row = 10;
        $return['totalPage'] = $this->getTotaoPage(count($this->Worker_tracker_sheet_model->getAnmWorkSheetHRPModel($this->uri->segment(3))), $_per_page_row);
        $return['region_detail'] = $this->Worker_tracker_sheet_model->getRegionUserDetailModel($this->uri->segment(3));
        $return['anm_work_detail'] = $this->Worker_tracker_sheet_model->getAnmWorkSheetHRPModel($this->uri->segment(3),$this->uri->segment(2),$_per_page_row);
        $this->session->set_userdata('page_name', 'mira_anm_work_detail_hrp');
        //print_r($return);
        $this->load->view('mira_welcome_page', $return);
    }

    

    public function getAnmWorkSheetANC() {
        $return['active_button'] = 2;
        $return['activePage']=$this->uri->segment(2);
        $return['region_detail'] = $this->Worker_tracker_sheet_model->getRegionUserDetailModel($this->uri->segment(3));
        $_per_page_row = 10;
        $return['totalPage'] = $this->getTotaoPage(count($this->Worker_tracker_sheet_model->getAnmWorkSheetANCModel($this->uri->segment(3))), $_per_page_row);
        $return['anm_work_detail_anc'] = $this->Worker_tracker_sheet_model->getAnmWorkSheetANCModel($this->uri->segment(3),$this->uri->segment(2),$_per_page_row);
        $this->session->set_userdata('page_name', 'mira_anm_work_detail_anc');
        $this->load->view('mira_welcome_page', $return);
       //print_r( $return['anm_work_detail_anc']);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getMiraWorkDetailResult() {
        $return = $this->getPregnantWomenStatus($this->uri->segment(3), $this->uri->segment(2));
        $return['active_button'] = 2;
        $return['region_detail'] = $this->Worker_tracker_sheet_model->getRegionUserDetailModel($this->uri->segment(3));
        $this->session->set_userdata('page_name', 'mira_work_details_result');
        $this->load->view('mira_welcome_page', $return);
    }

    private function getPregnantWomenStatus($regionCode, $currentPage) {
        $this->load->model('Immuization_women_child_model');
        $this->load->model('Registration_house_women_child_model');
        $return = NULL;
        $_per_page_row = 8;
        $return['activePage'] = $currentPage;
        $return['totalPage'] = $this->getTotaoPage(count($this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($regionCode)), $_per_page_row);
        $temp_pregnant_list = $this->Registration_house_women_child_model->getPregnantListByRegionCodeModel($regionCode, $currentPage, $_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 $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 getMiraWorkDetailTracker() {
        $return = $this->getPregnantWomenTracker($this->uri->segment(3), $this->uri->segment(2));
        $return['active_button'] = 3;
        $return['region_detail'] = $this->Worker_tracker_sheet_model->getRegionUserDetailModel($this->uri->segment(3));
        $this->session->set_userdata('page_name', 'mira_work_details_tracker');
        $this->load->view('mira_welcome_page', $return);
    }

    private function getPregnantWomenTracker($regionCode, $currentPage) {
        $this->load->model('Immuization_women_child_model');
        $_per_page_row = 8;
        $return['activePage'] = $currentPage;
        $data = $this->Immuization_women_child_model->getPregnantWomenImmunizatio($regionCode, $return['activePage'], $_per_page_row);
        $return['totalPage'] = $this->getNumberOfPages(count($this->Immuization_women_child_model->getPregnantWomenImmunizatio($regionCode)), $_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;
        return $return;
        //$this->session->set_userdata('page_name', 'mira_tracker_pregnant_women');
        //$this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     * 
     */
    public function getMiraWorkDetailCurrent() {
        $tempData = $this->getCurrentPregnantWomen($this->uri->segment(3), $this->uri->segment(2));
        $return['list'] = $tempData[0];
        $return['activePage'] = $tempData[1];
        $return['totalPage'] = $tempData[2];
        $return['active_button'] = 1;

        $return['region_detail'] = $this->Worker_tracker_sheet_model->getRegionUserDetailModel($this->uri->segment(3));
        // print_r($this->input->post('region_code'));
        //$return['asha_list']=  $this->Worker_tracker_sheet_model->getAshaWorkerTrackSheetModel($this->session->userdata('app_id'));
        $this->session->set_userdata('page_name', 'mira_work_details_current');
        //print_r($return['region_detail']);
        $this->load->view('mira_welcome_page', $return);
    }

    private function getCurrentPregnantWomen($region_code, $currentpage) {
        $this->load->model('Mira_worker_tracking_activity');
        $this->load->model('Immuization_women_child_model');
        $_per_page_row = 8;

        $data = $this->Immuization_women_child_model->getPregnantWomenImmunizatioCurrent($region_code, $currentpage, $_per_page_row);
        $totalPage = $this->getNumberOfPages(count($this->Immuization_women_child_model->getPregnantWomenImmunizatioCurrent($region_code)), $_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]['pregnant_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]['pregnant_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 array($data, $currentpage, $totalPage);
        //$this->session->set_userdata('page_name', 'mira_pregnant_women_current_week_status');
        //print_r(json_encode($data[0]['current_track']['sub_module_data']));
        //$this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    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' => $sub_module);
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    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;
        }
    }

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

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

    private function getWeek1($_number_of_days) {
        if ($_number_of_days != NULL) {
            return (int) ($_number_of_days / 7);
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getAshaWorkerTrackSheet() {
      $ulevel=$this->session->userdata('level');
      $tlevel=$this->session->userdata('app_total_level');
      $region_code=$this->session->userdata('create_region_id');

        $return['asha_list'] = $this->Worker_tracker_sheet_model->getAshaWorkerTrackSheetModel($this->session->userdata('app_id'),$ulevel,$tlevel,$region_code);
        $this->session->set_userdata('page_name', 'mira_asha_worker_summory');
        
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getANMWorkerTrackSheet() {

        $return['anm_list'] = $this->Worker_tracker_sheet_model->getANMWorkerTrackSheetModel($this->session->userdata('app_id'));

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

}
