<?php
//error_reporting(0);
defined('BASEPATH') OR exit('No direct script access allowed');

class DashboardModel extends CI_Model{



	function maxValue($year,$company_id){
		$query="SELECT MAX(total) as maximum FROM (SELECT COUNT(`test_id`) as total FROM `hcs_test` WHERE year(STR_TO_DATE(`test_date`,'%d/%m/%Y'))=".$year." AND company_id=".$company_id." GROUP BY month(STR_TO_DATE(`test_date`,'%d/%m/%Y')),result) a";

		$result=$this->db->query($query);
		return $result->row();
	}


	function get_data_in_array($status,$month,$year,$company_id){
		$query="SELECT COUNT(`test_id`) as total,month(STR_TO_DATE(`test_date`,'%d/%m/%Y')) as month FROM `hcs_test` WHERE company_id=".$company_id." AND year(STR_TO_DATE(`test_date`,'%d/%m/%Y'))='".$year."' AND month(STR_TO_DATE(`test_date`,'%d/%m/%Y'))='".$month."' AND result='".$status."'
		GROUP BY month(STR_TO_DATE(`test_date`,'%d/%m/%Y'))
		";

		$result=$this->db->query($query);
		return $result->row();
	}	




	function push_in_array($year,$company_id){

		$positive=[];
		$negative=[];
		$invalid=[];



		for($i=1;$i<=12;$i++){
			$positiveResult=$this->get_data_in_array('Positive',$i,$year,$company_id);


			if (!empty($positiveResult)){

			if($i==$positiveResult->month){
				$positive[$i]=intval($positiveResult->total);
			}

			}
			else{
				$positive[$i]=0;
			}

			$negativeResult=$this->get_data_in_array('Negative',$i,$year,$company_id);
		

			if (!empty($negativeResult)){

			if($i==$negativeResult->month){
				$negative[$i]=intval($negativeResult->total);
			}
			}
			else{
				$negative[$i]=0;
			}

			$invalidResult=$this->get_data_in_array('Invalid',$i,$year,$company_id);
			

			if (!empty($invalidResult)){
			if($i==$invalidResult->month){
				$invalid[$i]=intval($invalidResult->total);
			}
			}
			else{
				$invalid[$i]=0;
			}
		}

		$positiveArraytoString=implode(',', $positive);
		$negativeArraytoString=implode(',', $negative);
		$invalidArraytoString=implode(',', $invalid);

		$max=$this->maxValue($year,$company_id);
		$maxVal=$max->maximum;

		$data=array(
			'positive'=>$positiveArraytoString,
			'negative'=>$negativeArraytoString,
			'invalid'=>$invalidArraytoString,
			'maxVAl'=>$maxVal
		);

		return $data;

	}




	function get_bar_chart_data($year){

		$creators = $this->get_bar_chart_parent_node_extra($year);

		return $creators;

	}


	function get_total_result($company_id){
		$this->db->select('COUNT(test_id) as total,result');
		$this->db->where('company_id',$company_id);
		$this->db->from('hcs_test');
		$this->db->group_by('result');
		$query=$this->db->get();
		return $query->result();

	}




}