<?php
class Model_infection  extends CI_Model
{

	public function InfectionForm($arrInfo)
	{
		$invalid = $this->db->insert('ic_surveillance_form', $arrInfo);
		$insertId = $this->db->insert_id();
		log_message('debug',  'Infection Form ::');
		log_message('debug',  $this->db->last_query());
		if ($invalid) {
			return $insertId;
		} else {
			return false;
		}
	}
	public function InfectionFormDevice($arrInfoPhlebitis)
	{
		$invalid = $this->db->insert('ic_infection_devices', $arrInfoPhlebitis);
		log_message('debug',  'InfectionFormDevice ::');
		log_message('debug',  $this->db->last_query());
		if ($invalid) {
			$deviceId = $this->db->insert_id();
			return $deviceId;
		} else {
			return false;
		}
	}

	public function InfectionFormCulture($infoAdd)
	{
		$this->db->insert('ic_culture_report', $infoAdd);
		log_message('debug',  'InfectionFormCulture ::');
		log_message('debug',  $this->db->last_query());
		return true;
	}

	public function view_submitted_form_data($date)
	{

		$this->db->select('MAX(form_id) AS form_id,
		MAX(ward_no) AS ward_no,
		MAX(bed_no) AS bed_no,
		MAX(primary_diagnosis) AS primary_diagnosis,
		MAX(final_diagnosis) AS final_diagnosis,
		MAX(discharge_date) AS discharge_date, 
		MAX(admission_date) AS admission_date ,
		MAX(discharge_condition) AS discharge_condition,
		mr_no,pateint_name');
		$this->db->where('form_status', 1);
		if ($date != "") {
			$this->db->where('DATE(form_creation)', date("Y-m-d", strtotime($date)));
		}
		$this->db->group_by('mr_no');
		$result = $this->db->get('ic_surveillance_form')->result_array();

		log_message('debug',  $this->db->last_query());
		return $result;
	}
	public function PatientHistory($mrNo)
	{
		$this->db->select('*');
		$this->db->where('form_status', 1);
		$this->db->where('mr_no', $mrNo);
		$result = $this->db->get('ic_surveillance_form')->result_array();

		log_message('debug',  $this->db->last_query());
		return $result;
	}

	public function SurvData($formId)
	{
		$this->db->select('*');
		$this->db->where('form_status', 1);
		$this->db->where('form_id', $formId);
		$result = $this->db->get('ic_surveillance_form')->result_array();

		log_message('debug',  $this->db->last_query());
		return $result;
	}

	public function CultureData($formId)
	{
		$this->db->select('*');
		// $this->db->where('form_status', 1);
		$this->db->where('form_id', $formId);
		$result = $this->db->get('ic_culture_report')->result_array();

		log_message('debug',  $this->db->last_query());
		return $result;
	}

	public function get_antibiotic_list_by_orgnisam($org_name)
	{
		$this->db->where('organism_name', $org_name);
		$result = $this->db->get('ic_lovs');
		log_message('debug',  'get_antibiotic_list_by_orgnisam ::');
		log_message('debug',  $this->db->last_query());
		if ($result->num_rows() > 0) {
			return $result->row();
		} else {

			return false;
		}
	}

	public function get_ward_list($ward)
	{
		$this->db->where('ward_no', $ward);
		$result = $this->db->get('ic_bed')->result_array();
		log_message('debug',  'get_ward_list ::');
		log_message('debug',  $this->db->last_query());
		if ($result > 0) {
			return $result;
		} else {

			return false;
		}
	}

	public function check_Mr_Number($mr_num)
	{
		$this->db->where('mr_no', $mr_num);
		$this->db->order_by('form_id', 'DESC');
		$this->db->limit(1);
		$result = $this->db->get('ic_surveillance_form');
		log_message('debug', 'check_Mr_Number');
		log_message('debug', $this->db->last_query());
		if ($result->num_rows() > 0) {
			return $result->row();
		} else {

			return false;
		}
	}

	public function view_patient_with_hai($startDate = '', $endDate = '')
	{
		log_message('debug',  "start date: $startDate");

		$this->db->select('*');
		$this->db->from('ic_surveillance_form sf');
		$this->db->join('ic_infection_devices id', 'id.form_id = sf.form_id', 'left');
		$this->db->join('ic_culture_report cr', 'cr.form_id = sf.form_id', 'left');

		$this->db->where('DATE(admission_date) <=', "DATE(DATE_SUB(NOW(), INTERVAL 48 HOUR))", FALSE);

		if ($startDate == '') {
			$startDate = date('Y-m-d');
			$endDate = date('Y-m-d');
			$this->db->where("DATE(admission_date) BETWEEN '$startDate' AND '$endDate'", NULL, FALSE);
		} elseif ($startDate == 'none') {
		} else {
			$startDate = date('Y-m-d', strtotime($startDate));
			$endDate = date('Y-m-d', strtotime($endDate));
			$this->db->where("DATE(admission_date) BETWEEN '$startDate' AND '$endDate'", NULL, FALSE);
		}

		$this->db->where('id.`check`', 'yes');

		$this->db->group_by('admission_date, id.`device_name`');

		$result = $this->db->get()->result_array();

		log_message('debug',  $this->db->last_query());
		return $result;
	}

	public function view_patient_with_mdro($startDate = '', $endDate = '', $groupBy="")
	{
		log_message('debug',  "start date: $startDate");

		$this->db->select('*');
		$this->db->from('ic_surveillance_form sf');
		$this->db->join('ic_culture_report cr', 'cr.form_id = sf.form_id', 'inner');

		if ($startDate == '') {
			$startDate = date('Y-m-d');
			$endDate = date('Y-m-d');
			$this->db->where("DATE(admission_date) BETWEEN '$startDate' AND '$endDate'", NULL, FALSE);
		} elseif ($startDate == 'none') {
		} else {
			$startDate = date('Y-m-d', strtotime($startDate));
			$endDate = date('Y-m-d', strtotime($endDate));
			$this->db->where("DATE(admission_date) BETWEEN '$startDate' AND '$endDate'", NULL, FALSE);
		}

		if(strtolower($groupBy) == "yes")
		{
			$this->db->group_by('cr.`form_id`');
		}

		$result = $this->db->get()->result_array();

		log_message('debug',  $this->db->last_query());
		return $result;
	}
}
