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

class FacilityPayonline extends MY_Controller
{

	public function __construct()
	{
		parent::__construct();
	}


	public function index()
	{
		$data['page'] 		= 'Home';
		$data['pageTitle'] 	= 'Log in';

		// print_r(getUserDetails()->data[0]->_id); 

		$userid = $this->input->get('memberId');




		$data['servicesDetail'] = array();
		if ($this->session->userdata('logged_in') == 1) {


			if ($userid == $this->session->userdata('id')) {
				// if ($userid == '6684ffe4c8067653550ec178') {


				$tokenNo = $this->input->get('tokenNo');

				$response = getFacilityRequestByTokenNo($tokenNo);
				// var_dump($response);
				if (isset($response) && $response->statusCode == 1 && !empty($response->data)) {
					$data['servicesDetail'] = json_decode(json_encode($response->data), true);
					$data['paymentDetails'] = json_decode(json_encode($response->businessData), true);
					// echo "<pre>";
					// var_dump($data['paymentDetails']['paymentsData']);die();
					if ($data['paymentDetails']['paymentStatus'] == 'COMPLETED') {
						echo 'Payment Already Done.';
						echo 'Go Back to<a href="' . base_url('services') . '">  Home</a>';
					} else {
						$data['header_data'] = getWebSyncsData('Header Settings', 'headerSettings', '');
						$this->load->view('facility-payonline/login', $data);
					}
				} else {
					echo 'Sorry! This Service in not available.';
				}
			} else {
				echo 'Sorry! This service is not requested by you.<br>';
				echo 'Go Back to<a href="' . base_url('services') . '">  Home</a>';
			}
		} else {
			$data['header_data'] = getWebSyncsData('Header Settings', 'headerSettings', '');
			$this->load->view('facility-payonline/login', $data);
		}
	}

	public function updateFacilityTransactionStatus($id = '', $invoiceNo = '', $PaymentStatus = '')
	{

		if (!empty($this->input->post('transaction_id'))) {
			$paymentDescription = json_encode($this->input->post('paymentDescription'));
			$transactionId = $this->input->post('transaction_id');
			$PaymentStatus = $this->input->post('status');
		} else {
			$transactionId = '';
		}

		if (!empty($this->session->userdata('temp_order_details'))) {
			$invoiceNo = $this->session->userdata('temp_order_details')['invoiceNo'];
			$id = $this->session->userdata('temp_order_details')['_id'];
		}
		$pendingAmount = $this->input->post('pendingAmount');
		$advanceAmount = $this->input->post('advanceAmount');
		$totalAmount = $this->input->post('totalAmount');

		$ch = curl_init();

		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_URL, ApiBaseUrl()['url'] . updateFacilityTransactionStatus()['url']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, "{         
			\"productId\": \"" . ApiBaseUrl()['productID'] . "\",
			\"clientId\": \"" . ApiBaseUrl()['clientID'] . "\",  

			\"_id\":\"" . $id . "\",      
			\"invoiceNo\":\"" . $invoiceNo . "\",      
			\"paymentStatus\": \"" . $PaymentStatus . "\",      
			\"transactionId\": \"" . $transactionId . "\",
			\"advanceAmount\": \"" . $advanceAmount . "\",
			\"pendingAmount\": \"" . $pendingAmount . "\",
			\"totalAmount\": \"" . $totalAmount . "\"

		}     
		");
		$token = $this->session->userdata('token');

		$headers = array();

		$headers[] = 'Authorization: Bearer ' . $token . '';

		$headers[] = 'Content-Type: application/json';
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

		$result = curl_exec($ch);
		if (curl_errno($ch)) {
			echo 'Error:' . curl_error($ch);
		}
		curl_close($ch);


		if (!empty($this->input->post('transaction_id')) && ($PaymentStatus == 'COMPLETED' || $PaymentStatus == 'PARTIALLY_COMPLETED')) {
			// print_r($result);
			$response = json_decode($result);

			$this->session->unset_userdata('temp_order_details');
			$this->session->unset_userdata('service_cart');

			$data['orderId'] = $id;
			$data['envoiceNo'] = $response->envoiceNo;
			$data['statusCode'] = $response->statusCode;
			$data['message'] = $response->message;
			$updatedResponse = json_encode($data);
			print_r($updatedResponse);
		} else if (!empty($this->input->post('transaction_id')) && $PaymentStatus == 'FAILED') {
			// print_r($result);
			$response = json_decode($result);

			// $this->session->unset_userdata('temp_events_order_details');
			// $this->session->unset_userdata('events_cart');

			$data['orderId'] = $id;
			$data['envoiceNo'] = $response->envoiceNo;
			$data['statusCode'] = $response->statusCode;
			$data['message'] = $transactionId;
			$updatedResponse = json_encode($data);
			print_r($updatedResponse);
		} else {

			return $response = json_decode($result);
		}
	}
}
