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

class MembershipForm extends MY_Controller
{

	public function __construct()
	{
		parent::__construct();
		$currency = '';

		$this->$currency =  GeneralSettings();
	}

	public function index()
	{


		$data['page'] = 'MEMBERSHIP FORM';
		$data['title'] = 'MEMBERSHIP FORM';
		$this->load->view('membership-form/index', $data);
	}

	public function CheckoutMembership()
	{

		$userdata = getUserDetails();



		$membershipType = $this->input->post('membershipType');
		$membershipId = $this->input->post('membershipId');
		$transactionId = $this->input->post('transactionId');

		$membershipAmount = $this->input->post('membershipAmount');
		$membershipDetail = $this->input->post('membershipDetail');
		$paymentStatus = $this->input->post('paymentStatus');

		$paymentSource = $this->input->post('paymentSource');
		$membershipName = $this->input->post('membershipName');
		$inDays = $this->input->post('inDays');


		if ($paymentStatus == 'COMPLETED') {
			$status = 'ACTIVE';
		} else {

			$status = 'INACTIVE';
		}

		$data_json = array(
			"clientId" => ApiBaseUrl()['clientID'],
			"dataJson" => array(
				"membershipAmount" => $membershipAmount,
				"membershipDetail" => $membershipDetail,
				"planName" => $membershipName,
				"inDays" => $inDays,
				"planType" => $membershipType,
				"paymentStatus" => $paymentStatus,
				"memberId" => $userdata->data[0]->_id,
				"Name" => $userdata->data[0]->refDataName,
				"Mobile" => $userdata->data[0]->phone,
				"Email" => $userdata->data[0]->email,
				"planId" => $membershipId,
				"transactionId" => $transactionId,
				"status" => $status,
				"paymentSource" => $paymentSource,
				"moduleName" => "Membership Directory",
				"memberTypes" => "MEMBERSHIP",
				"aspectType" => "membership",
				"recCreDate" => date('m/d/Y'),
				"recCreTime" => date('h:i A')
			)
		);



		// print_r($data_json);
		// die();

		$jsonData = json_encode($data_json);
		// print_r($jsonData);
		// die();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

		curl_setopt($ch, CURLOPT_URL, ApiBaseUrl()['url'] . 'api/home/addMemberShip');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);


		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);


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

		$result = curl_exec($ch);
		$response = json_decode($result, true);

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

		echo json_encode($response);
	}
}
