<?php
defined('BASEPATH') or exit('No direct script access allowed');

class All_chick_controller extends CI_Controller
{

	public function index()
	{
		// $this->load->model('WCS_model','chick');
		$data['allData'] = $this->WCS_model->get_data('wcs_chicken');
		// var_dump($data_page);

		$data['getData'] = $this->WCS_model->get_join(
			
			'wcs_chicken',
			'',
			array(
				'ck_id'=>'1'
			),
			array(
				'wcs_behavior' => 'wcs_behavior.bhv_ck_id=wcs_chicken.ck_id', 
				'wcs_movement' =>'wcs_movement.mov_ck_id = wcs_chicken.ck_id', 
				'wcs_treatment' => 'wcs_treatment.tm_ck_id =wcs_chicken.ck_id'
			)

		)->result();
		
		if($data['getData'][0]->bhv_name=="ปกติ"){
			$data['countStatusGood'] = "1";
			$data['countStatusBad'] = "0";
		}
		if($data['getData'][0]->bhv_name=="ผิดปกติ"){
			$data['countStatusBad'] = "1";
			$data['countStatusGood'] = "0";
			$this->line_notification();

		}

		// var_dump(count( $data['allData']));
		$data['date_present'] = date("Y-m-d");

		$data['countAll'] = count( $data['allData']);
		$data['page_title'] = '';
		$data['page_content'] = $this->load->view('all_chick', $data, TRUE);
		$this->load->view('main', $data);
	}

	public function test_a()
	{

		// $data_page['getData'] = $this->WCS_model->get_data('wcs_chicken');

		$data['page_title'] = '';
		$data['page_content'] = $this->load->view('test', '', TRUE);
		$this->load->view('main', $data);
	}

	public function chick_information($chick_id)
	{
		// $this->load->model('WCS_model','chick');
	    // $data['chick_info'] = $this->WCS_model->get_data('wcs_chicken');
		// '<pre>';
		// 	echo "<br>";echo "<br>";
		// 	print_r($data['chick_info']);
		// 	echo "<br>";echo "<br>";
		// '</pre>';
		// $data['chick_info'] = $this->chick->dwd($chick_id)->result();
		$data['chick_info'] = $this->WCS_model->get_join(
			
			'wcs_chicken',
			'',
			array(
				'ck_id'=>$chick_id
			),
			array(
				'wcs_behavior' => 'wcs_behavior.bhv_ck_id=wcs_chicken.ck_id', 
				'wcs_movement' =>'wcs_movement.mov_ck_id = wcs_chicken.ck_id', 
				'wcs_treatment' => 'wcs_treatment.tm_ck_id =wcs_chicken.ck_id'
			)
		//         JOIN {$this->wcs_db}.wcs_behavior ON  = $chick_id 
        // JOIN {$this->wcs_db}.wcs_movement ON = $chick_id 
        // JOIN {$this->wcs_db}.wcs_treatment ON 
		)->result();

		$data['page_title'] = 'Chicken'." No. ".$chick_id;
		$data['page_content'] = $this->load->view('p', $data, TRUE);
		$this->load->view('main', $data);
	}

	public function line_notification() {
    
		ini_set('display_errors', 1);
		ini_set('display_startup_errors', 1);
		error_reporting(E_ALL);
		date_default_timezone_set("Asia/Bangkok");
	  
		//Date and TIme Variables
		$dateNow = date("d-m-Y");
		$timeNow =  date('H:i');
	  
		//Set Link for send to msg.
		$link = "http://localhost/wcs/index.php/All_chick_controller";
	  
		//Line Notify Token
		$sToken = "8ACvAfcfPbKh00por4HNhaSdDADIk3rKZXuugIlVuOo";
	  
		//Message send to Line Notify
		$sMessage = "\n". $dateNow . " #".$timeNow ."\nพบความผิดปกติ กรุณาตรวจสอบลิงก์ : \n". $link;
	  
		  $chOne = curl_init(); 
		  curl_setopt( $chOne, CURLOPT_URL, "https://notify-api.line.me/api/notify"); 
		  curl_setopt( $chOne, CURLOPT_SSL_VERIFYHOST, 0); 
		  curl_setopt( $chOne, CURLOPT_SSL_VERIFYPEER, 0); 
		  curl_setopt( $chOne, CURLOPT_POST, 1); 
		  curl_setopt( $chOne, CURLOPT_POSTFIELDS, "message=".$sMessage); 
		  $headers = array( 'Content-type: application/x-www-form-urlencoded', 'Authorization: Bearer '.$sToken.'', );
		  curl_setopt($chOne, CURLOPT_HTTPHEADER, $headers); 
		  curl_setopt( $chOne, CURLOPT_RETURNTRANSFER, 1); 
		  $result = curl_exec( $chOne ); 
	  
		  //Result error 
		  if(curl_error($chOne)) 
		  { 
			  echo 'error:' . curl_error($chOne); 
		  } 
		  else { 
			  $result_ = json_decode($result, true); 
		  } 
		  curl_close( $chOne ); 
		}

}
