<?php
/*
Template Name: Storeup Cart
*/

get_header(); ?>
<div id="primary" class="pagemid">
	<div class="inner">

		<main class="content-area">
		<div class="entry-content-wrapper clearfix">
		<?php

		if ( have_posts() ) :
			while ( have_posts() ) :
				the_post();
				the_content();
			endwhile;
		endif;

		global $storeup_bk_data_id;
		$storeup_post_form       = ! empty( $_POST ) ? $_POST : false;

		if ( $storeup_post_form ) {
			$storeup_bk_name         = isset( $storeup_post_form['storeup_bk_name'] ) ? $storeup_post_form['storeup_bk_name'] : '';
			$storeup_bk_phoneno      = isset( $storeup_post_form['storeup_bk_phoneno'] ) ? $storeup_post_form['storeup_bk_phoneno'] : '';
			$storeup_bk_email        = isset( $storeup_post_form['storeup_bk_email'] ) ? $storeup_post_form['storeup_bk_email'] : '';
			$storeup_bk_address      = isset( $storeup_post_form['storeup_bk_address'] ) ? $storeup_post_form['storeup_bk_address'] : '';
			$storeup_bk_zipcode      = isset( $storeup_post_form['storeup_bk_zipcode'] ) ? $storeup_post_form['storeup_bk_zipcode'] : '';
			$storeup_bk_date         = isset( $storeup_post_form['storeup_bk_date'] ) ? $storeup_post_form['storeup_bk_date'] : '';
			$storeup_bk_location     = isset( $storeup_post_form['storeup_bk_location'] ) ? $storeup_post_form['storeup_bk_location'] : '';
			$storeup_bk_message      = isset( $storeup_post_form['storeup_bk_message'] ) ? $storeup_post_form['storeup_bk_message'] : '';
			$storeup_bk_item_id      = isset( $storeup_post_form['storeup_bk_item_id'] ) ? $storeup_post_form['storeup_bk_item_id'] : '';
			$storeup_bk_item_limit   = isset( $storeup_post_form['storeup_bk_item_limit'] ) ? $storeup_post_form['storeup_bk_item_limit'] : '';
			$storeup_bk_item_price   = isset( $storeup_post_form['storeup_bk_item_price'] ) ? $storeup_post_form['storeup_bk_item_price'] : '';
			$storeup_bk_payment_mode = isset( $storeup_post_form['storeup_bk_payment_mode'] ) ? $storeup_post_form['storeup_bk_payment_mode'] : '';

			if ( $storeup_bk_name || $storeup_bk_phoneno || $storeup_bk_email || $storeup_bk_address || $storeup_bk_zipcode || $storeup_bk_date || $storeup_bk_location || $storeup_bk_item_price || $storeup_bk_message ) {
				global $storeup_default_date;
				if ( $storeup_bk_date ) {
					$storeup_booking_date = @date( $storeup_default_date, strtotime( $storeup_bk_date ) );
				}

				/**
				* Prepare and store appointment post data and update its meta data
				*/
				$storeup_bk_data = array(
					// 'post_title' => $storeup_bk_name,
					'post_type'   => 'booking',
					'post_status' => 'pending',
				);

				global $storeup_bk_data_id,$storeup_success_templateid;
				$storeup_bk_data_id = wp_insert_post( $storeup_bk_data );

				$storeup_bk_fields = array(
					'storeup_bk_phoneno',
					'storeup_bk_email',
					'storeup_bk_address',
					'storeup_bk_zipcode',
					'storeup_bk_message',
				);

				foreach ( $storeup_bk_fields as $bk_field ) {
					if ( isset( $storeup_post_form[ $bk_field ] ) ) {
						update_post_meta( $storeup_bk_data_id, $bk_field, $storeup_post_form[ $bk_field ] );
					}
				}
				$storeup_bk_title = esc_html__( 'Order ', 'storeup' ) . '#' . $storeup_bk_data_id;
				wp_update_post( array(
					'post_type'  => 'booking',
					'ID'         => $storeup_bk_data_id,
					'post_title' => $storeup_bk_title,
				) );

				$storeup_bk_order_status = esc_html__( 'Pending', 'storeup' );

				update_post_meta( $storeup_bk_data_id, 'storeup_bk_date', strtotime( $storeup_bk_date ) );
				update_post_meta( $storeup_bk_data_id, 'storeup_bk_location', get_the_title( $storeup_bk_location ) );
				update_post_meta( $storeup_bk_data_id, 'storeup_bk_price', $storeup_bk_item_price );
				update_post_meta( $storeup_bk_data_id, 'storeup_bk_name', $storeup_bk_name );
				update_post_meta( $storeup_bk_data_id, 'storeup_bk_payment_type', $storeup_bk_payment_mode );
			}

			$storeup_bk_url  = get_permalink( $storeup_success_templateid ) . '?type=' . $storeup_bk_payment_mode . '';
			$storeup_bk_checkout_btn_txt = get_option( 'storeup_bk_checkout_btn_txt' ) ? get_option( 'storeup_bk_checkout_btn_txt' ) : esc_html__( 'Checkout', 'storeup' );

			// //Defining the template cart form action depending upon the payment mode selected
			if ( 'paypal' === $storeup_bk_payment_mode ) {
				global $storeup_paypal_cart_action_url;
				$storeup_bk_cart_action_url = $storeup_paypal_cart_action_url;
			} elseif ( 'voguepay' === $storeup_bk_payment_mode ) {
				global $storeup_voguepay_cart_action_url;
				$storeup_bk_cart_action_url = $storeup_voguepay_cart_action_url;
			} else {
				$storeup_bk_cart_action_url = $storeup_bk_url;
			}

			$out  = '<div class="storeup-bk-template-cart">';
			$out .= '<form id="storeup-bk-cart-form" action="' . esc_url( $storeup_bk_cart_action_url ) . '"  class="clearfix storeup-bk-cart-form" name="storeup_bk_cart_form" method="post" >';
			$out .= '<div class="at-checkout-wrapper">';
			$out .= '<div class="col_half">';

			if ( ! empty( $storeup_bk_name ) ) {
				$out .= '<div class="at-checkout-group">';
				$out .= '<span class="checkout-label">' . esc_html__( 'Full Name: ', 'storeup' ) . '</span>';
				$out .= '<span class="checkout-value">' . $storeup_bk_name . '</span>';
				$out .= '</div>'; // at-checkout-group
			}
			if ( ! empty( $storeup_bk_email ) ) {
				$out .= '<div class="at-checkout-group">';
				$out .= '<span class="checkout-label">' . esc_html__( 'Email: ', 'storeup' ) . '</span>';
				$out .= '<span class="checkout-value">' . $storeup_bk_email . '</span>';
				$out .= '</div>'; // at-checkout-group
			}
			if ( ! empty( $storeup_bk_address ) ) {
				$out .= '<div class="at-checkout-group">';
				$out .= '<span class="checkout-label">' . esc_html__( 'Address: ', 'storeup' ) . '</span>';
				$out .= '<span class="checkout-value">' . str_replace( '-', ' ', $storeup_bk_address ) . '</span>';
				$out .= '</div>'; // at-checkout-group
			}
			if ( ! empty( $storeup_bk_date ) ) {
				$out .= '<div class="at-checkout-group">';
				$out .= '<span class="checkout-label">' . esc_html__( 'Booking Date: ', 'storeup' ) . '</span>';
				$out .= '<span class="checkout-value">' . $storeup_bk_date . '</span>';
				$out .= '</div>'; // at-checkout-group
			}
			$out .= '</div>'; // col_half
			$out .= '<div class="col_half end">';
			if ( ! empty( $storeup_bk_phoneno ) ) {
				$out .= '<div class="at-checkout-group">';
				$out .= '<span class="checkout-label">' . esc_html__( 'Phone No: ', 'storeup' ) . '</span>';
				$out .= '<span class="checkout-value">' . $storeup_bk_phoneno . '</span>';
				$out .= '</div>'; // at-checkout-group
			}
			if ( ! empty( $storeup_bk_zipcode ) ) {
				$out .= '<div class="at-checkout-group">';
				$out .= '<span class="checkout-label">' . esc_html__( 'Zipcode: ', 'storeup' ) . '</span>';
				$out .= '<span class="checkout-value">' . $storeup_bk_zipcode . '</span>';
				$out .= '</div>'; // at-checkout-group
			}
			if ( ! empty( $storeup_bk_location ) ) {
				$out .= '<div class="at-checkout-group">';
				$out .= '<span class="checkout-label">' . esc_html__( 'Location: ', 'storeup' ) . '</span>';
				$out .= '<span class="checkout-value">' . get_the_title( $storeup_bk_location ) . '</span>';
				$out .= '</div>'; // at-checkout-group
			}
			if ( ! empty( $storeup_bk_item_price ) ) {
				$out .= '<div class="at-checkout-group">';
				$out .= '<span class="checkout-label">' . esc_html__( 'Unit & Price: ', 'storeup' ) . '</span>';
				$out .= '<span class="checkout-value">' . $storeup_bk_item_price . '</span>';
				$out .= '</div>'; // at-checkout-group
			}
			if ( ! empty( $storeup_bk_item_price ) ) {
				$out .= '<div class="at-checkout-group">';
				$out .= '<span class="checkout-label">' . esc_html__( 'Payment Type: ', 'storeup' ) . '</span>';
				$out .= '<span class="checkout-value">' . $storeup_bk_payment_mode . '</span>';
				$out .= '</div>'; // at-checkout-group
			}
			ob_start();
			if ( 'paypal' === $storeup_bk_payment_mode ) {
				$out .= do_action( 'storeup_fetch_paypal_payment_inputs' );
				$out .= ob_get_clean();
			} elseif ( 'voguepay' === $storeup_bk_payment_mode ) {
				$out .= do_action( 'storeup_fetch_voguepay_payment_inputs' );
				$out .= ob_get_clean();
			} else {
				$out .= '<input type="submit" class="storeup_bk_cart_submit" name="cart_submit" value="' . $storeup_bk_checkout_btn_txt . '">';
			}
			$out .= '<input type="hidden" name="storeup_bk_item_id" class="storeup_bk_item_number"  value="' . $storeup_bk_item_id . '">';
			$out .= '<input type="hidden" name="storeup_bk_order_id" class="storeup_bk_order_id"  value="' . $storeup_bk_data_id . '">';
			$out .= '<input type="hidden" name="storeup_bk_item_name" class="storeup_bk_item_name"  value="' . get_the_title( $storeup_bk_location ) . '">';
			if ( ! empty( $storeup_bk_item_price ) ) {
				$storeup_bk_item_price_vals = explode( '-', $storeup_bk_item_price );
				$out .= '<input type="hidden" name="storeup_bk_item_price" id="storeup_bk_item_price" value="' . str_replace( get_option('storeup_cur_sym'), '', $storeup_bk_item_price_vals[1] ) . '">';
			}
			$out .= '</div>'; // col_half end
			$out .= '</div>'; // at-checkout-wrapper
			$out .= '</form>';
			$out .= '</div>'; // storeup-bk-template-cart

			echo wp_kses_post( $out );
		}
		?>
		</div><!-- .entry-content-wrapper -->
	</main><!-- .content-area -->

	<?php if ( storeup_generator( 'storeup_sidebar_option', $post->ID ) !== 'fullwidth' ) { get_sidebar(); } ?>

</div><!-- inner -->
</div><!-- #primary.pagemid -->

<?php get_footer();
