<?php 
// if ( ! defined('BASEPATH')) exit('No direct script access allowed');
defined('BASEPATH') OR exit('No direct script access allowed');
class Approval extends CI_Controller {
	public $distributor_id;

	function __construct() {
		parent::__construct();
		$this->load->model('My_model');
		$this->load->model('Approval_model');
		$this->load->library('admin_layout');
		$this->load->helper('jsondata_helper');
		$this->load->helper('email_helper');	
	}

	function index() {
		if(!$username=$this->session->userdata('logged_in')) redirect(base_url().'login');
		$user_id = $this->session->userdata('user_id');
		$data['type'] = $this->session->userdata('type');
		if($data['type']!=2) redirect(base_url().'login');	

			
		$header_data = array(
		  'username'  		=> $username,
		  'user_id'    		=> $user_id
		);
		
		$this->admin_layout->set_title("Approval");
		$this->admin_layout->set_description("");
		$this->admin_layout->set_data($header_data);
		// $data = '';

		// $get_claims = $this->My_model->get_rows('claims',array('admin_id'=>$user_id));
	
		$get_claims = $this->Approval_model->getclaims($user_id);
		

		
		// echo $this->db->last_query();
		// $claims= array();
		// foreach($get_claims as $row){
		// 	$row_array['claim_id'] = $row->claim_id;
		// 	$row_array['name'] = $row->name;
		// 	array_push($claims,$row_array);
		// }

		// $data['claims'] = json_encode($get_claims);
		// $data['header'] = json_fields($get_claims); 
		// $data['rows'] = json_encode($get_claims);

		// $options = array(     
		// 	'hideFirstCol'    => 'Y',
		// 	'displayCounter'  => 0, //0= No;1=ASC; -1=DESC
		//   ); 
	  
		// $data['options'] = json_encode($options);  


		$data['claims'] = $get_claims;
		$data['err_claim'] = 'No Claims available'; 


		
		$content = "admin/view_approval";
		$this->admin_layout->view($content,'',$data,true);
	
	}
	public function getfa(){
		$data = $this->My_model->get_rows("admins","type ='3'","firstname ASC");
		//echo $this->db->last_query();
		// $data["fa"]=json_encode($res);
		echo json_encode($data);
		
	}
	public function toApprove(){
		
		$wew = (rand(0,50000));
		//echo $wew;
		$char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$randomString = '';
		for ($i = 0; $i < 4; $i++) {
	        $randomString .= $char[rand(0, strlen($char) - 1)];
			}
		//echo $randomString;
		
		$date = date("Y-m-d H:i:s");
		$refno = $this->input->post('refno',TRUE);
		$adminid = $this->input->post('adminid',TRUE);
		$claimname = $this->input->post('claimname',TRUE);
		$distributor = $this->input->post('distributor',TRUE);
		$faid = $this->input->post('selectfa',TRUE);
		//$faid = $this->My_model->get_value('admins','admin_id',array('type'=>'3'));
		//echo $faid;
		$check_refno = $this->My_model->get_value('claim_application_final','cafid',array('refno'=>$refno));
		$this->My_model->update('claims',array('refno'=>$refno),array('status'=>'3'));
		$this->My_model->insert('claim_status_logs',array('refno'=>$refno, 'status_id' => '3', 'remarks' => 'For Final Approval'));
		$this->My_model->insert('claim_alert',array('refno'=>$refno, 'admin_id' => $adminid, 'alert_date'=> $date, 'message' => '(ICAM) '. $claimname .' Application APPROVED: For Final Approval'));
		$this->My_model->insert('claim_alert',array('refno'=>$refno, 'admin_id' => $faid, 'alert_date'=> $date, 'message' => '(ICAM) '. $claimname .' Application APPROVED: For Final Approval'));
		if($check_refno){
			$this->My_model->update('claim_application_final',array('cafid'=>$check_refno),array('fa_id' => $faid));
		}else{
			$this->My_model->insert('claim_application_final',array('refno'=>$refno,'ticketno'=>$wew.''.$randomString,'fa_id' => $faid));
		}
		
		//echo $faid;
		//echo $this->db->last_query();
		$icam_email = $this->My_model->get_value('admins','email',array('admin_id'=>$adminid));
		$icam_email2 = $this->My_model->get_value('admins','email',array('admin_id'=>$faid));
		$data_claim = array(
					'claimname' => $claimname,
					'distributor' => $distributor,
					'date' => date("Y-m-d"),
					'link' => 'approval_final'
				);
		$recipient = $icam_email;
		$recipient2 = $icam_email2;
		$template = $this->load->view('emailtemplate1',$data_claim,true);
		$subject = '(ICAM) Claim Approved';
		$headers = 'From: XPlus Support <support@xplus.ph>';
		$data_ebox = array(
					'emailtime'   => date("Y-m-d H:i:s"),
					'recipientname' => $recipient,
					'recipient' => $recipient,
					'header' => 'for FA Approval',
					'subject' => $subject,
					'admin_id' => $refno,
					'msgtype'	=> 'S',
					'message'	=> $template,
					'sent'	=> '1'
					);
		//echo $template;
		if(email($recipient,$template,$subject)){
					echo 'success'; 
					$this->My_model->insert('ebox',$data_ebox);
				}else{
					echo 'email not sent!';
				}
		if(email($recipient2,$template,$subject)){
					echo 'success'; 
					$this->My_model->insert('ebox',$data_ebox);
				}else{
					echo 'email not sent!';
				}
		
	}
	public function toDecline(){
		$date = date("Y-m-d H:i:s");
		$refno = $this->input->post('refno',TRUE);
		$adminid = $this->input->post('adminid',TRUE);
		$claimname = $this->input->post('claimname',TRUE);
		$remark = $this->input->post('remark',TRUE);
		$distributor = $this->input->post('distributor',TRUE);
		$this->My_model->update('claims',array('refno'=>$refno),array('status'=>'4'));
		$this->My_model->insert('claim_status_logs',array('refno'=>$refno, 'status_id' => '4', 'remarks' => '(ICAM) '. $claimname .' Application DECLINED: '.$remark));
		$this->My_model->insert('claim_alert',array('refno'=>$refno, 'admin_id' => $adminid, 'alert_date'=> $date, 'message' => '(ICAM) '. $claimname .' Application DECLINED: '.$remark));
		//echo $this->db->last_query();

		$icam_email = $this->My_model->get_value('admins','email',array('admin_id'=>$adminid));
		$data_claim = array(
					'claimname' => $claimname,
					'distributor' => $distributor,
					'date' => date("Y-m-d"),
					'remark' => $remark,
					'link' => 'manage_claim_dst'
				);
		$recipient = $icam_email;
		
		$template = $this->load->view('emailtemplate2',$data_claim,true);
		$subject = '(ICAM) Claim Declined';
		$headers = 'From: XPlus Support <support@xplus.ph>';
		$data_ebox = array(
					'emailtime'   => date("Y-m-d H:i:s"),
					'recipientname' => $recipient,
					'recipient' => $recipient,
					'header' => 'ICAM Declined Claimed',
					'subject' => $subject,
					'admin_id' => $refno,
					'msgtype'	=> 'S',
					'message'	=> $template,
					'sent'	=> '1'
					);
		if(email($recipient,$template,$subject)){
					echo 'success'; 
					$this->My_model->insert('ebox',$data_ebox);
				}else{
					echo 'email not sent!';
				}
	}
		public function getDsr(){
		$data = $this->My_model->get_rows("admins","type ='1'","firstname ASC");
		echo json_encode($data);
	}
	public function claims(){
		
		$data = $this->Approval_model->claims($_POST['selectDsr']);
		// $selectDsr = $this->input->post('selectDsr',TRUE);
		// $data = $this->claims->getclaims($selectDsr );
		echo json_encode(['data'=>$data]);
		echo ($_POST['selectDsr']);
		//echo json_encode($data);
		//echo $this->db->last_query();
	
		
	}


	public function get_docus(){
	
		$refno = $this->input->post('refno',TRUE);	
		
		// echo "refno:".$refno;
		
		// $res = $this->My_model->get_rows('claim_docus',array('refno'=>$refno));
		$res = $this->Approval_model->getdocus($refno);
		//echo $this->db->last_query();
		$result = json_encode($res);

		// if(!$res) {
		//   echo json_encode(array("SQL" => $this->db->last_query()));
		// }
	
		// else 
		echo $result;
	
	
	}


}
