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

	function __construct() {
		parent::__construct();
		$this->load->model('My_model');
		$this->load->model('Approval_final_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']!=3) redirect(base_url().'login');
		//echo $user_id;
		$header_data = array(
		  'username'  		=> $username,
		  'user_id'    		=> $user_id
		);
		
		$this->admin_layout->set_title("Final Approval");
		$this->admin_layout->set_description("");
		$this->admin_layout->set_data($header_data);
		
		//print $selectDsr;

		// $data = '';

		// $get_claims = $this->My_model->get_rows('claims',array('admin_id'=>$user_id));
		$get_claims = $this->Approval_final_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'; 

			


		//json_encode($data);
		$content = "admin/view_approval_final";
		$this->admin_layout->view_final($content,'',$data,true);
	
	}
	
	public function claims(){
		$user_id = $this->session->userdata('user_id');
		$data = $this->Approval_final_model->claims($_POST['selectDsr'],$user_id);
		echo json_encode(['data'=>$data]);
		// echo $user_id;
	
		
	}
	
	public function getDsr(){
		$data = $this->My_model->get_rows("admins","type ='1'","firstname ASC");
		echo json_encode($data);
	}
	public function toApprove(){
		$user_id = $this->session->userdata('user_id');
		
		$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);
		$icamid = $this->input->post('icamid',TRUE);
		$faid = $user_id;
		echo $distributor;
		// $check_refno = $this->My_model->get_value('claim_application_final','cafid',array('refno'=>$refno));
		// $this->My_model->update('claims',array('refno'=>$refno),array('status'=>'5'));
		// $this->My_model->insert('claim_status_logs',array('refno'=>$refno, 'status_id' => '5', 'remarks' => 'Approved'));
		// $this->My_model->insert('claim_alert',array('refno'=>$refno, 'admin_id' => $adminid, 'alert_date'=> $date, 'message' => '(FA) '. $claimname .' Application APPROVED'));
		// $this->My_model->insert('claim_alert',array('refno'=>$refno, 'admin_id' => $faid, 'alert_date'=> $date, 'message' => '(FA) '. $claimname .' Application APPROVED'));
		// if($check_refno){
		// 	$this->My_model->update('claim_application_final',array('cafid'=>$check_refno),array('ticketno'=>$wew.''.$randomString,'fa_id' => $faid));
		// }else{
		// 	$this->My_model->insert('claim_application_final',array('refno'=>$refno,'ticketno'=>$wew.''.$randomString,'fa_id' => $faid));
		// }
		$icam_email = $this->My_model->get_value('admins','email',array('admin_id'=>$adminid));
		$ticketno = $this->My_model->get_value('claim_application_final','ticketno',array('refno'=>$refno));
		$icam_email2 = $this->My_model->get_value('admins','email',array('admin_id'=>$icamid));
		$data_claim = array(
					'claimname' => $claimname,
					'distributor' => $distributor,
					'ticketno' => $ticketno,
					'date' => date("Y-m-d"),
					'link' => 'approval_final'
				);
		$recipient = $icam_email;
		$recipient2 = $icam_email2;
		$template = $this->load->view('emailtemplate3',$data_claim,true);
		$subject = '(FA) 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' => '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(){
		$user_id = $this->session->userdata('user_id');
		$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);
		$icamid = $this->input->post('icamid',TRUE);
		echo $distributor;
		
		// $this->My_model->update('claims',array('refno'=>$refno),array('status'=>'6'));
		// $this->My_model->insert('claim_status_logs',array('refno'=>$refno, 'status_id' => '6', 'remarks' => '(FA) '. $claimname .' Application DECLINED: '.$remark));
		// $this->My_model->insert('claim_alert',array('refno'=>$refno, 'admin_id' => $adminid, 'alert_date'=> $date, 'message' => '(FA) '. $claimname .' Application DECLINED:: '.$remark));
		// 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'=>$icamid));
		$ticketno = $this->My_model->get_value('claim_application_final','ticketno',array('refno'=>$refno));
		$data_claim = array(
					'claimname' => $claimname,
					'distributor' => $distributor,
					'ticketno' => $ticketno,
					'remark' => $remark,
					'date' => date("Y-m-d"),
					'link' => 'approval_final'
				);
		$recipient = $icam_email;
		$recipient2 = $icam_email2;
		$template = $this->load->view('emailtemplate4',$data_claim,true);
		$subject = '(FA) 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' => '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 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;
	
	
	}


}
