<?php
if (! class_exists ( 'DTBookingReservationForm' ) ) {

    class DTBookingReservationForm extends DTBaseBookingSC {

        function __construct() {

            add_shortcode( 'dt_sc_reservation_form', array( $this, 'dt_sc_reservation_form' ) );

			add_filter( 'vc_autocomplete_dt_sc_reservation_form_serviceids_callback', array ( $this, 'dt_booking_vc_autocomplete_serviceids_field_search' ), 10, 1 );
			add_filter( 'vc_autocomplete_dt_sc_reservation_form_serviceids_render', array ( $this, 'dt_booking_vc_autocomplete_serviceids_field_render' ), 10, 1 );

			add_filter( 'vc_autocomplete_dt_sc_reservation_form_staffids_callback', array ( $this, 'dt_booking_vc_autocomplete_staffids_field_search' ), 10, 1 );
			add_filter( 'vc_autocomplete_dt_sc_reservation_form_staffids_render', array ( $this, 'dt_booking_vc_autocomplete_staffids_field_render' ), 10, 1 );
        }

		function dt_sc_reservation_form($attrs, $content = null) {
			extract(shortcode_atts(array(
				'title' => esc_html__('Appointment', 'dt-reservation-plugin'),
				'serviceids' => '',
				'staffids' => '', 
			), $attrs)); 

			$out = '';

			$url = get_page_link( cs_get_option('appointment-pageid') );
			$url = isset($url) ? $url : '';

			if($url != '') {

				$out = '<div class="dt-sc-appointment-wrapper">';
	
					$out .= '<h2>'.$title.'</h2>';
		
					$out .= '<form class="dt-sc-reservation-form dt-appointment-form" name="reservation-schedule-form" method="post" action="'.$url.'">';
		
					$out .= '<div class="column dt-sc-one-half first">
								<p>Available Services</p>
								<select name="services" id="services" class="dt-select-service">
								  <option value="">'. __('Type of Service','dt-reservation-plugin').'</option>';
									  if($serviceids != '') {
										  $serviceids_arr = explode(',', $serviceids);
										  $cp_services = get_posts( array('post_type'=>'dt_service', 'posts_per_page'=>'-1', 'post__in' => $serviceids_arr, 'suppress_filters' => false ));
									  } else {
										  $cp_services = get_posts( array('post_type'=>'dt_service', 'posts_per_page'=>'-1', 'suppress_filters' => false ) );
									  }

									  if( $cp_services ){ 
										  foreach( $cp_services as $cp_service ){
											  $id = $cp_service->ID;
											  $title = $cp_service->post_title;

											  $service_settings = get_post_meta($id, '_custom_settings', true);
											  $service_settings = is_array ( $service_settings ) ? $service_settings : array ();

											  $out .= "<option value='{$id}'>{$title}";
												  if( array_key_exists('service-price', $service_settings) ):
												  	  $out .= ' - '.dt_booking_get_formatted_price( $service_settings['service-price'] );
												  endif;
											  $out .= "</option>";
										  }
									  }
					$out .= '</select></div>';

					$out .= '<div class="column dt-sc-one-half">
								<p>Staffs</p>
								<select name="staff" id="staff" class="dt-select-staff">
									<option value="">'.__('Name of Person','dt-reservation-plugin').'</option>';
										if($staffids != '') {
											$staffids_arr = explode(',', $staffids);
											$cp_staffs = get_posts( array('post_type'=>'dt_person', 'posts_per_page'=>'-1', 'post__in' => $staffids_arr ) );
										} else {
											$cp_staffs = get_posts( array('post_type'=>'dt_person', 'posts_per_page'=>'-1' ) );
										}
										if( $cp_staffs ){
											foreach( $cp_staffs as $cp_staff ){
												$id = $cp_staff->ID;
												$title = $cp_staff->post_title;

												$person_settings = get_post_meta($id, '_custom_settings', true);
												$person_settings = is_array ( $person_settings ) ? $person_settings : array ();

												$out .= '<option value="'.$id.'">'.$title;
													if( array_key_exists('person-price', $person_settings) ):
														$out .= ' - '.dt_booking_get_formatted_price( $person_settings['person-price'] );
													endif;
												$out .= '</option>';
											}
										}
					$out .= '</select></div>';
					$out .= '<div class="dt-sc-hr-invisible-small"> </div>';
       				$out .= '<div class="dt-sc-clear"> </div>';
					$out .= '<div class="column dt-sc-one-third first">
								<p>I am available on</p>
								<p class="dt-appoint-date"><span class="far fa-calendar-alt"></span><input type="text" id="datepicker" name="date" placeholder="'.__('Preferred Date','dt-reservation-plugin').'" readonly="readonly" /></p>
							 </div>';
							 
					$out .= '<div class="column dt-sc-one-third">';
            		$out .= '<p>Start:</p>';
				
                $time_format = get_option( 'time_format' ); 
                $fetch_start_time = isset($_REQUEST['start-time']) ? date($time_format, strtotime($_REQUEST['start-time'])) : date($time_format, strtotime('8:00 am'));
                $fetch_end_time = isset($_REQUEST['end-time']) ? date($time_format, strtotime($_REQUEST['end-time'])) : date($time_format, strtotime('11:00 pm'));
                $selected = 'selected="selected"';
          		$out .= '<select name="start-time" id="start-time" class="start-time">';
                $out .= '<option value="00:00">12:00 am</option> ';   
                $out .= '<option value="01:00" >1:00 am</option>';
                $out .= '<option value="02:00" >2:00 am</option>';
                $out .= '<option value="03:00" >3:00 am</option>';
                $out .= '<option value="04:00">4:00 am</option>';
                $out .= '<option value="05:00">5:00 am</option>';
                $out .= '<option value="06:00">6:00 am</option>';
                $out .= '<option value="07:00">7:00 am</option>';
                $out .= '<option value="08:00">8:00 am</option>';
                $out .= '<option value="09:00">9:00 am</option>';
                $out .= '<option value="10:00">10:00 am</option>';
                $out .= '<option value="11:00">11:00 am</option>';
                $out .= '<option value="12:00">12:00 pm</option>';
                $out .= '<option value="13:00">1:00 pm</option>';
                $out .= '<option value="14:00">2:00 pm</option>';
                $out .= '<option value="15:00">3:00 pm</option>';
                $out .= '<option value="16:00">4:00 pm</option>';
                $out .= '<option value="17:00">5:00 pm</option>';
                $out .= '<option value="18:00">6:00 pm</option>';
                $out .= '<option value="19:00">7:00 pm</option>';
                $out .= '<option value="20:00">8:00 pm</option>';
                $out .= '<option value="21:00">9:00 pm</option>';
                $out .= '<option value="22:00">10:00 pm</option>';
                $out .= '<option value="23:00">11:00 pm</option>';
            $out .= '</select>';
        $out .= '</div>';

        $out .= '<div class="column dt-sc-one-third">';
            $out .= '<p>End :</p>';
            $out .= '<select name="end-time" id="end-time" class="end-time">';
                $out .= '<option value="09:00">9:00 am</option> ';   
                $out .= '<option value="10:00">10:00 am</option>'; 
                $out .= '<option value="11:00">11:00 am</option>';
                $out .= '<option value="12:00">12:00 pm</option>';
                $out .= '<option value="13:00">1:00 pm</option>'; 
                $out .= '<option value="14:00">2:00 pm</option>';
                $out .= '<option value="15:00">3:00 pm</option>';
                $out .= '<option value="16:00">4:00 pm</option>';
                $out .= '<option value="17:00">5:00 pm</option>';  
                $out .= '<option value="18:00">6:00 pm</option>';
                $out .= '<option value="19:00">7:00 pm</option>';
                $out .= '<option value="20:00">8:00 pm</option>';
                $out .= '<option value="21:00">9:00 pm</option>';  
                $out .= '<option value="22:00">10:00 pm</option>';
                $out .= '<option value="23:00">11:00 pm</option>';
                $out .= '<option value="23:59">12:00 am</option>';
            $out .= '</select>';
        $out .= '</div>';
		$out .= '<div class="dt-sc-hr-invisible-small"> </div>';
       				$out .= '<div class="dt-sc-clear"> </div>';
					$out .= '<button class="dt-sc-button filled medium show-time-shortcode" value="'.__('Show Time', 'dt-reservation-plugin').'" type="submit">'.__('Book Now', 'dt-reservation-plugin').'</button>';
		
					$out .= '<input type="hidden" id="staffids" name="staffids" value="'.$staffids.'" /><input type="hidden" id="serviceids" name="serviceids" value="'.$serviceids.'" />';
		
					$out .= '</form>';
	
				$out .= '</div>';
			} else {
				$out .= '<div class="dt-sc-info-box">'.__('Please create Reservation template page in order to make this shortcode work properly!', 'dt-reservation-plugin').'</div>';
			}

			return $out;
		}
    }
}

new DTBookingReservationForm();