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

class Checkout extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
	}
	public function index($page = '')
	{



		$data['GeneralSettings'] = GeneralSettings();


		// print_r($data['GeneralSettings']); die();
		if (!empty($this->session->userdata('logged_in')) && $this->session->userdata('logged_in') == 1) {

			$response = getUserDetails();
			$data['userDetails'] = $response->data[0];
			$data['GetState'] = GetState();
			$data['preferredLanguage'] = getPreferredLanguage();
			$data['page'] = 'Facilities';
			$data['title'] = 'Checkout';
			$data['session_data'] = 'facilities_cart';


			$this->load->view('facilities/checkout.php', $data);
		} else {
			redirect(base_url());
		}
	}



	public function updateRequestDetailsWithoutReloadingPage()
	{

		$cart_items = $this->session->userdata('facilities_cart');
		$service_details = $this->session->userdata('service_details');
		$currencySymbol = getCurrencySymbol();
		$GeneralSettings = GeneralSettings();

		// var_dump($service_details);

		$sericeAmt = 0;
		$saleTotal = 0;
		$subTotal = 0;
		$discountedAmount = 0;
		$membershipDetails = membershipStatusCheck();
		/*

		$setting =  SettingsApi();
		$membershipDiscountAmt = $setting['result']['data'][0]['membershipSettings']['membershipDiscount'];
		$discountType = $setting['result']['data'][0]['membershipSettings']['discountType'];
		*/
		$html = '';

		// Start building HTML content

		$html .= '<div class="d-flex justify-content-between align-items-center mb-5">';

		// Count the number of items in the cart
		// $numItems = count($cart_items['ids']);
		// $html .= '<h6 class="mb-0 text-muted">' . $numItems . ' items</h6>';
		$html .= '</div>';

		$html .= '<div class="row mb-4 d-flex justify-content-between align-items-center">';
		$html .= '<div class="col-md-6 col-lg-6 col-xl-6">';
		$html .= '<h6 class="text-black fw-bold mb-0" style="color: rgb(240 18 18) !important;font-size:25px">Facilities</h6>';
		$html .= '</div>';
		$html .= '<div class="col-md-6 col-lg-6 col-xl-6" style="display:contents!important;">';
		$html .= '<h6 class="text-black fw-bold mb-0" style="color: rgb(240 18 18) !important;font-size:25px">Price</h6>';
		$html .= '</div>';
		$html .= '</div>';
		$html .= '<hr class="my-4">';

		// Loop through cart items and calculate total amount
		$payNowTotalAmount = 0;
		// var_dump($cart_items);
		foreach ($cart_items['ids'] as $key => $value) {
			/*if ($cart_items['serviceAmount'][$key] > 0) {
				$payNowTotalAmount += $cart_items['serviceAmount'][$key];
				$subTotal+= $cart_items['serviceAmount'][$key];
				if ($membershipDetails['membershipStatus'] == "ACTIVE" &&  @$cart_items['is_membership_discount'][$key] == 'YES') {

					if ($discountType == 'FLAT') {
	
						if ($cart_items['serviceAmount'][$key] > $membershipDiscountAmt) {
							$rentalSericeAmt = $cart_items['serviceAmount'][$key] - $membershipDiscountAmt;
							$oldRentalServiceAmount = $membershipDiscountAmt;
							$RentaldiscountedAmount = $membershipDiscountAmt;
						} else {
							$rentalSericeAmt = $cart_items['serviceAmount'][$key];
							$oldRentalServiceAmount = $cart_items['serviceAmount'][$key];
							$RentaldiscountedAmount = 0;
						}
					} else {
						
						$rentalSericeAmt = $cart_items['serviceAmount'][$key] - (($cart_items['serviceAmount'][$key] * $membershipDiscountAmt) / 100);
						$oldRentalServiceAmount = $cart_items['serviceAmount'][$key];
						$RentaldiscountedAmount = $cart_items['serviceAmount'][$key] - $rentalSericeAmt;
					}
				} else {
	
	
					$rentalSericeAmt = $cart_items['serviceAmount'][$key];
					$oldRentalServiceAmount = $cart_items['serviceAmount'][$key];
					$RentaldiscountedAmount = 0;
				}


			}*/
			$subTotal += $cart_items['oldServiceAmount'][$key];
			$saleTotal += $cart_items['serviceAmount'][$key];
			$discountedAmount += $cart_items['discountedAmount'][$key];

			// Add item details to HTML
			$html .= '<div class="row mb-4 d-flex justify-content-between align-items-center">';
			$html .= '<div class="col-md-6 col-lg-6 col-xl-6 text-start">';
			$html .= '<h6 class="text-black fw-bold mb-0">' . $cart_items['serviceName'][$key] . '</h6>';
			if (@$cart_items['serviceTypes'][$key]) {
				$html .= '<p style="font-size:15px!important;margin-bottom:0px!important;"><span style="font-weight: bolder;">Event Type: </span>' . @$cart_items['serviceTypes'][$key] . '</p>';
			}
			if (@$cart_items['startDate'][$key]) {
				$html .= '<p style="font-size:15px!important;margin-bottom:0px!important;"><span style="font-weight: bolder;">Event Date: </span>' . @$cart_items['startDate'][$key] . '</p>';
			}
			if (@$cart_items['start_time'][$key] || @$cart_items['end_time'][$key]) {
				$html .= '<p style="font-size:15px!important;margin-bottom:0px!important;"><span style="font-weight: bolder;">Start Time - End Time: </span>' . @$cart_items['start_time'][$key] . ' - ' . @$cart_items['end_time'][$key] . '</p>';
			}
			$html .= '</div>';
			$html .= '<div class="col-md-6 col-lg-6 col-xl-6 text-end px-0">';
			$html .= '<h6 class="mb-0 text-end"><span style="font-weight:bold!important;">' . $currencySymbol . '&nbsp;' . price_format($cart_items['oldServiceAmount'][$key], 2) . '</span></h6>';
			$html .= '</div>';
			$html .= '</div>';
		}


		// var_dump($service_details);


		if (!empty($service_details)) {
			$html .= '<hr><div class="row mb-4 d-flex justify-content-between align-items-center">';
			$html .= '<div class="col-md-6 col-lg-6 col-xl-6">';
			$html .= '<h6 class="text-black fw-bold mb-0" style="color: rgb(240 18 18) !important;font-size:25px">Service</h6>';
			$html .= '</div>';
			$html .= '<div class="col-md-6 col-lg-6 col-xl-6" style="display:contents!important;">';
			$html .= '<h6 class="text-black fw-bold mb-0" style="color: rgb(240 18 18) !important;font-size:25px"></h6>';
			$html .= '</div>';
			$html .= '</div>';
			$html .= '<hr class="my-4">';

			// Loop through cart items and calculate total amount


			/*if ($membershipDetails['membershipStatus'] == "ACTIVE" &&  @$service_details['is_membership_discount'] == 'YES') {

				if ($discountType == 'FLAT') {

					if ($service_details['serviceAmount'] > $membershipDiscountAmt) {
						$sericeAmt = $service_details['serviceAmount'] - $membershipDiscountAmt;
						$oldServiceAmount = $membershipDiscountAmt;
						$discountedAmount = $membershipDiscountAmt;
					} else {
						$sericeAmt = $service_details['serviceAmount'];
						$oldServiceAmount = $service_details['serviceAmount'];
						$discountedAmount = 0;
					}
				} else {
				
					$sericeAmt = $service_details['serviceAmount'] - (($service_details['serviceAmount'] * $membershipDiscountAmt) / 100);
					$oldServiceAmount = $service_details['serviceAmount'];
					$discountedAmount = $service_details['serviceAmount'] - $sericeAmt;
				}
			} else {


				$sericeAmt = $service_details['serviceAmount'];
				$oldServiceAmount = $service_details['serviceAmount'];
				$discountedAmount = 0;
			}*/
			$payNowTotalAmount +=  $service_details['oldServiceAmount'];
			$saleTotal +=  $service_details['serviceAmount'];
			$subTotal += $service_details['oldServiceAmount'];
			$discountedAmount += $service_details['discountedAmount'];

			// Add item details to HTML
			$html .= '<div class="row mb-4 d-flex justify-content-between align-items-center">';
			$html .= '<div class="col-md-6 col-lg-6 col-xl-6 text-start">';
			$html .= '<h6 class="text-black fw-bold mb-0">' . $service_details['serviceName'] . '</h6>';
			$html .= '<p style="font-size:15px!important;margin-bottom:0px!important;"><span style="font-weight: bolder;">Service Date: </span>' . $service_details['serviceDate'] . '</p>';
			$html .= '<p style="font-size:15px!important;margin-bottom:0px!important;"><span style="font-weight: bolder;">Service Time: </span>' . $service_details['serviceTime'] . '</p>';
			$html .= '<p style="font-size:15px!important;margin-bottom:0px!important;"><span style="font-weight: bolder;">Priest Name: </span>' . $service_details['priestName'] . '</p>';
			$html .= '</div>';
			$html .= '<div class="col-md-6 col-lg-6 col-xl-6 text-end px-0">';
			$html .= '<h6 class="mb-0 text-end"><span style="font-weight:bold!important;">' . $currencySymbol . '&nbsp;' . price_format($service_details['oldServiceAmount'], 2) . '</span></h6>';
			$html .= '<p style="font-size:15px!important;margin-bottom:0px!important;">&nbsp;</p>';
			$html .= '<p style="font-size:15px!important;margin-bottom:0px!important;">&nbsp;</p>';
			$html .= '</div>';
			$html .= '</div>';
		}



		// Calculate total amount with tax if applicable
		if (!empty($GeneralSettings['TaxPercent']) && $GeneralSettings['TaxPercent'] > 0) {
			$payNowTotalAmount = ($payNowTotalAmount * $GeneralSettings['TaxPercent']) / 100;
		}

		// $html .= '<hr class="my-4">';
		// $html .= '<div class="row mt-3">';
		// $html .= '<div class="col-md-6"><h5 class="mb-0" style="font-weight:bold!important;">Subtotal</h5></div>';
		// $html .= '<div class="col-md-6 text-end" style="padding:0!important"><span style="font-weight:bold!important;">' . $currencySymbol . '&nbsp;' . price_format($cart_items['totalPrice'], 2) . '</span></div>';
		// $html .= '</div>';

		// Add tax details if applicable
		if (!empty($GeneralSettings['TaxPercent']) && $GeneralSettings['TaxPercent'] > 0) {
			$html .= '<div class="row mt-3">';
			$html .= '<div class="col-md-10"><h5 class="mb-0" style="font-weight:bold!important;">Tax (' . $GeneralSettings['TaxPercent'] . '%)</h5></div>';
			$html .= '<div class="col-md-2 text-end"><span style="font-weight:bold!important;">' . $currencySymbol . ' ' . price_format(($cart_items['totalPrice'] * $GeneralSettings['TaxPercent']) / 100, 2) . '</span></div>';
			$html .= '</div>';
		}


		$setting =  SettingsApi();

		$membershipDiscount = $setting['result']['data'][0]['membershipSettings']['membershipDiscount'];
		$discountType = $setting['result']['data'][0]['membershipSettings']['discountType'];
		$facilitySettings = $setting['result']['data'][0]['facilitySettings'];
		$facilitySecurityAmount = @$facilitySettings['facilitySecurityAmount'];
		$facilitySecurityType = @$facilitySettings['facilitySecurityType'];
		/* Subtotal*/

		$html .= '<hr><div class="row mb-4 d-flex justify-content-between align-items-center">';
		$html .= '<div class="col-md-6 col-lg-6 col-xl-6 text-start">';
		$html .= '<h6 class="text-black fw-bold mb-0">Subtotal</h6>';
		$html .= '</div>';
		$html .= '<div class="col-md-6 col-lg-6 col-xl-6 text-end px-0">';
		$html .= '<h6 class="mb-0 text-end"><span style="font-weight:bold!important;">' . $currencySymbol . '&nbsp;' . price_format($subTotal, 2) . '</span></h6>';
		$html .= '</div>';
		$html .= '</div>';
		/* facilitySecurityAmount*/
		if ($facilitySecurityAmount > 0) {

			$saleTotal += price_format($facilitySecurityAmount, 2);


			$html .= '<hr><div class="row mb-4 d-flex justify-content-between align-items-center">';
			$html .= '<div class="col-md-6 col-lg-6 col-xl-6 text-start">';
			$html .= '<h6 class="text-black fw-bold mb-0">Facility Security Amount</h6>';
			$html .= '</div>';
			$html .= '<div class="col-md-6 col-lg-6 col-xl-6 text-end px-0">';
			$html .= '<h6 class="mb-0 text-end"><span style="font-weight:bold!important;">' . $currencySymbol . '&nbsp;' . price_format($facilitySecurityAmount, 2) . '</span></h6>';
			$html .= '</div>';
			$html .= '</div>';
		}

		/*discount*/
		if ($discountedAmount > 0) {
			$html .= '<hr><div class="row mb-4 d-flex justify-content-between align-items-center">';
			$html .= '<div class="col-md-6 col-lg-6 col-xl-6 text-start">';
			$html .= '<h6 class="text-danger fw-bold mb-0"> Membership Discount Applied ( ';

			if ($discountType == 'SERVICE LEVEL' || $discountType == 'FLAT') {
				$html .= $currencySymbol . ' ' . $discountedAmount;
			} else {
				$html .=  $membershipDiscount . '%';
			}

			$html .= ' )</h6>';
			$html .= '</div>';
			$html .= '<div class="col-md-6 col-lg-6 col-xl-6 text-end px-0">';
			$html .= '<h6 class="mb-0 text-end text-danger"><span style="font-weight:bold!important;">' . $currencySymbol . '&nbsp;' . price_format($discountedAmount, 2) . '</span></h6>';
			$html .= '</div>';
			$html .= '</div>';
		}


		// Calculate and add sale total
		// $saleTotal = (($cart_items['totalPrice'] * ($GeneralSettings['TaxPercent'] ? $GeneralSettings['TaxPercent'] : 0)) / 100) + $cart_items['totalPrice'];
		$saleTotal = (($saleTotal * ($GeneralSettings['TaxPercent'] ? $GeneralSettings['TaxPercent'] : 0)) / 100) + $saleTotal;


		// $saleTotal += $sericeAmt;

		// $subTotal+=$saleTotal;

		$html .= '<hr class="my-4">';
		$html .= '<div class="row py-3 px-0" style="background-color:#dbdbdb">';
		$html .= '<div class="col-md-8"><h4 class="text-dark" style="margin-top:auto;margin-bottom:auto;font-weight: bold!important;">Sale Total Amount</h4></div>';
		$html .= '<div class="col-md-4 px-0">';
		$html .= '<h4 class="text-dark text-end px-2" style="margin-top:auto;margin-bottom:auto;font-weight: bold!important;">' . $currencySymbol . '&nbsp;' . price_format($saleTotal, 2) . '</h4>';
		$html .= ' <input type="hidden" name="Amount" id="inputAmount" value="' . price_format($saleTotal, 2) . '">';
		$html .= '</div>';
		$html .= '</div>';


		// Return the generated HTML content
		echo $html;
	}

}
