<?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 tikatoy_model
 *
 * @author zmq163
 */
class Tikatoy_model extends CI_Model {

    //put your code here
    var $mdb;

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->mdb = $this->load->database('mdb', TRUE);
    }
    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getRegionIdUserIdPassword($userId=NULL,$password=NULL){
        if($userId!=NULL and $password!=NULL){
            $this->mdb->select('*');
            $this->mdb->from('master_login_user');
            $this->mdb->where(array('user_login_id'=>$userId,'user_password'=>$password));
            return $this->getData($this->mdb->get());
           }  else {
            return FALSE;    
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getPregnantWomenListModel($regionId=NULL) {
        if ($regionId != NULL) {
            $this->mdb->select('mira_house.house_number as houseNumber,'
                    . 'mira_house.house_head_name as houseHeadName,'
                    . 'mira_women.name as womenName,'
                    . 'mira_pregnant_women.pregnant_id as pregnantId,'
                    . 'mira_pregnant_women.pregnant_date as prgnancyDate,'
                    );
            $this->mdb->from('mira_house');
            $this->mdb->join('mira_women', 'mira_women.house_id=mira_house.auto_id', 'INNER');
            $this->mdb->join('mira_pregnant_women', 'mira_pregnant_women.women_id=mira_women.women_id', 'INNER');
            $this->mdb->where(array('mira_house.region_id' => $regionId,'mira_pregnant_women.pregnant_status'=>1));
            $pregnantWomenData = $this->getData($this->mdb->get());
            if ($pregnantWomenData) {
                for ($i = 0; $i < count($pregnantWomenData); $i++) {
                    $pregnantWomenData[$i]['prgnancyDate']=strtotime($pregnantWomenData[$i]['prgnancyDate']);
                }
            }
            return $pregnantWomenData;
        } else {
            return FALSE;
        }
        
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getChildImmunizationListModel($regionId = NULL) {
        if ($regionId != NULL) {
            $this->mdb->select('mira_house.house_number as houseNumber,'
                    . 'mira_house.house_head_name as houseHeadName,'
                    . 'mira_women.name as motherName,'
                    . 'mira_child.child_id as childId,'
                    . 'mira_child.child_name as childName,'
                    . 'mira_child.dob as childDob,'
                    . 'mira_child.gender as childGender');
            $this->mdb->from('mira_house');
            $this->mdb->join('mira_women', 'mira_women.house_id=mira_house.auto_id', 'INNER');
            $this->mdb->join('mira_child', 'mira_child.women_id=mira_women.women_id', 'INNER');
            $this->mdb->where(array('mira_house.region_id' => $regionId));
            $childImmunizationData = $this->getData($this->mdb->get());
            if ($childImmunizationData) {
                for ($i = 0; $i < count($childImmunizationData); $i++) {
                    $childImmunizationData[$i]=  $this->childImmunizationFormat($childImmunizationData[$i]);
                }
            }
            return $childImmunizationData;
        } else {
            return FALSE;
        }
    }

    private function childImmunizationFormat($childDetail) {
        $childDetail['childDob'] = strtotime($childDetail['childDob']);
        $childDetail['childName'] = utf8_decode($childDetail['childName']);
        if ($childDetail['childGender'] == 1) {
            $childDetail['childGender'] = 'male';
        } else {
            $childDetail['childGender'] = 'female';
        }
        return $childDetail;
    }

    /**
     * 
     * 
     * 
     */
    private function getData($query = NULL) {
        if ($query != NULL and $query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return FALSE;
        }
    }

}
