<?php
/**
 * GFB Service Class
 *
 * @package GFB.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! class_exists( 'APBGF_Service' ) ) {
	
	/**
	 * Checks if WooCommerce is enabled.
	 */
	class APBGF_Service {     
		/**
		 * Instance
		 *
		 * @var $instance
		 */
		private static $instance = null;

		private static $checkPostType = null;

		/**
		 * Init the Dependencies.
		 */
		private function __construct() {            

			// phpcs:ignore
			self::$checkPostType = isset( $_REQUEST['post_type'] ) && ! empty( sanitize_text_field(wp_unslash( $_REQUEST['post_type'] )) ) ? sanitize_text_field(wp_unslash( $_REQUEST['post_type'] )) : '';

			add_action('init', array( $this, 'register_service_posttype' ));

			add_action('init', array( $this, 'register_service_taxonomy' ));

			add_filter( 'manage_apbgf-service_posts_columns', array( $this, 'apbgf_modify_column_names' ) );
			add_action( 'manage_apbgf-service_posts_custom_column', array( $this, 'apbgf_add_custom_column' ), 9, 2 );
			add_action( 'apbgf_after_menu_register', array( $this, 'add_service_category_menu' ) );

			add_action( 'wp_ajax_apbgf_add_service', array( $this, 'apbgf_add_service' ) );
			add_action( 'wp_ajax_apbgf_edit_service', array( $this, 'apbgf_edit_service' ) );
			add_action( 'wp_ajax_apbgf_delete_service', array( $this, 'apbgf_delete_service' ) );
			add_action( 'wp_ajax_apbgf_show_service_data', array( $this, 'apbgf_show_service_data' ) );

			add_action( 'parent_file', array( $this, 'menu_highlight' ), 10, 1 );

			if ( 'apbgf-service' === self::$checkPostType ) {

				add_action( 'admin_footer', array( $this, 'highlight_menu' ) );

				add_filter('post_row_actions', '__return_empty_array');

				add_action('manage_posts_extra_tablenav', array( $this, 'apbgf_manage_posts_extra_tablenav' ));

				add_filter('months_dropdown_results', '__return_empty_array');

				add_filter( 'bulk_actions-edit-apbgf-service', array( $this, 'apbgf_add_bulk_delete' ) ); //__return_empty_array

				// HTML of the filter
				add_action( 'restrict_manage_posts', array( $this, 'apbgf_add_filters' ) );

				add_action( 'views_edit-apbgf-service', function ( $views ) {

					unset($views['all']);
					unset($views['publish']);
					unset($views['trash']);
					unset($views['draft']);                 

					return $views;
				});

				// the function that filters posts
				add_action( 'pre_get_posts', array( $this, 'apbgf_filter_query' ) );          
			}

			add_filter('apbgf-service-category_row_actions', function ( $actions ) {
				unset($actions['view']);
				unset($actions['edit']);
				return $actions;
			}, 90, 1);

			add_filter('manage_edit-apbgf-service-category_columns', function ( $columns ) {
				if ( isset( $columns['description'] ) ) {
					unset( $columns['description'] );   
				}
				return $columns;
			}, 99, 1);

			add_action( 'admin_enqueue_scripts', function ( $hook ) {
				$screen = get_current_screen();

				if ( isset( $screen->taxonomy ) && $screen->taxonomy === 'apbgf-service-category' ) {
					// Enqueue a dummy or existing admin style handle
					wp_register_style( 'apbgf-admin-inline-style', false );
					wp_enqueue_style( 'apbgf-admin-inline-style' );

					// Your inline CSS
					$css = '
						.term-description-wrap,
						.term-parent-wrap {
							display: none;
						}
					';

					wp_add_inline_style( 'apbgf-admin-inline-style', $css );
				}
			} );

			add_action( 'wp_ajax_apbgf_get_service_category_by_location', array( $this, 'apbgf_get_service_category_by_location' ) );
			add_action( 'wp_ajax_nopriv_apbgf_get_service_category_by_location', array( $this, 'apbgf_get_service_category_by_location' ) );

			add_action( 'wp_ajax_apbgf_get_service_by_service_category', array( $this, 'apbgf_get_service_by_service_category' ) );
			add_action( 'wp_ajax_nopriv_apbgf_get_service_by_service_category', array( $this, 'apbgf_get_service_by_service_category' ) );
		}

		public function apbgf_add_filters( $post_type ) {
			require_once APBGF_ADMIN_VIEW_PATH . 'appointment-filters.php';           
		}

		public function apbgf_filter_query( $admin_query ) {

			if ( is_admin() && $admin_query->is_main_query() ) {

				if ( is_user_logged_in() ) {

					if ( absint($admin_query->query_vars['s']) ) {
						// Set the post id value
						$admin_query->set('p', $admin_query->query_vars['s']);

						// Reset the search value
						$admin_query->set('s', '');
					}           
				}               
			}           

			return $admin_query;
		}

		public function apbgf_add_bulk_delete( $actions ) {

			unset( $actions['edit'] );
			unset( $actions['trash'] );
			$actions['delete'] = __('Delete All', 'appointment-and-booking-for-gravity-forms');

			return $actions;
		}

		public static function apbgf_get_services_by_location_noajax( $location_id = '' ) {

			if ( '' != $location_id && 'all' != $location_id ) {
				
				$post = get_post( $location_id );
				
				if ( $post && 'apbgf-location' == $post->post_type ) {
					
					$args = array(
						'post_type' => 'apbgf-service',
						'numberposts' => -1,
						'status' => 'publish',
						'meta_query' => array(
							'relation' => 'OR',
							array(
								'key' => 'location',
								'value' => $post->ID,
								'compare' => 'LIKE'
							),
							array(
								'key' => 'location',                                
								'compare' => 'NOT EXISTS'
							)
						)
					);

					$services = get_posts($args);

					if ( is_array( $services ) && count( $services ) > 0 ) {
						return $services;
					}
					
				}
			} else {

				$services = self::all_service();

				if ( is_array( $services ) && count( $services ) > 0 ) {
					return $services;
				}

			}

			return array();
		}

		public function apbgf_get_service_category_by_location() {
			check_ajax_referer( 'appointment-and-booking-for-gravity-forms', 'nonce' );

			$response = array(                
				'status'  => 'error',
				'selected_service_category_id' => ''            
			);
			// Whitelist & sanitize expected inputs only
			$form_id = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : 0;
			$post_id = isset( $_REQUEST['post_id'] ) && 'all' !== $_REQUEST['post_id'] && 'undefined' !== $_REQUEST['post_id'] && 'null' !== $_REQUEST['post_id']
				? absint( $_REQUEST['post_id'] ) : 0;

			if ( $form_id ) {
				$form  = \GFAPI::get_form( $form_id );

				if ( is_array( $form['fields'] ) && count($form['fields']) > 0 ) {
					foreach ( $form['fields'] as $field ) {
						if ( 'apbgf_appointment_calendar' == $field->type ) {
							$response['selected_service_category_id'] = $field->apbgf_service_category;
							break;
						}
					}                    
				}
			}

			if ( $post_id ) {

				$post = get_post( $post_id );

				if ( $post && 'apbgf-location' == $post->post_type ) {

					$args = array(
						'post_type' => 'apbgf-service',
						'numberposts' => -1,
						'status' => 'publish',
						'meta_query' => array(
							'relation' => 'OR',
							array(
								'key' => 'location',
								'value' => $post->ID,
								'compare' => 'LIKE'
							),
							array(
								'key' => 'location',
								'compare' => 'NOT EXISTS'
							)
						)
					);

					$services = get_posts($args);

					$services_category = array();

					foreach ($services as $service) {
						$categories = get_the_terms($service->ID, 'apbgf-service-category');

						if ($categories && !is_wp_error($categories)) {
							foreach ($categories as $category) {
								$services_category[ $category->term_id ] = $category;
							}
						}
					}

					
					$services_category = array_values($services_category);

					if ( !empty( $services_category ) ) {
						$response['data'] = array_map( function ( $category ) {
							return array(
								'ID' => $category->term_id,
								'name' => $category->name,
							);
						}, $services_category );
						$response['status'] = 'success';                        
					}
				}
			} else {
				$services = self::all_service();
				$services_category = array();
				foreach ( $services as $service ) {
					$categories = get_the_terms( $service->ID, 'apbgf-service-category' );
					if ( $categories && ! is_wp_error( $categories ) ) {
						foreach ( $categories as $category ) {
							$services_category[] = $category;
						}
					}
				}

				if ( !empty( $services_category ) ) {
					$response['data'] = array_map( function ( $category ) {
						return array(
							'ID' => $category->term_id,
							'name' => $category->name,
						);
					}, $services_category );
					$response['status'] = 'success';                    
				}
			}

			echo wp_json_encode( $response );
			wp_die();
		}

		public function apbgf_get_service_by_service_category() {

			check_ajax_referer('appointment-and-booking-for-gravity-forms', 'nonce');

			$response = array(
				'status' => 'error',
				'selected_service_id' => ''
			);
			// Whitelist & sanitize expected inputs only
			$form_id = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : 0;
			$term_id = isset( $_REQUEST['term_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['term_id'] ) ) : '';
			$location_id = isset( $_REQUEST['location_id'] ) ? absint( $_REQUEST['location_id'] ) : 0;

			if ( $form_id ) {
				$form = \GFAPI::get_form( $form_id );

				if (is_array($form['fields']) && count($form['fields']) > 0) {
					foreach ($form['fields'] as $field) {
						if ('apbgf_appointment_calendar' == $field->type) {
							$response['selected_service_id'] = $field->apbgf_service;
							break;
						}
					}
				}
			}

				

			if ( ! empty( $term_id ) && '0' !== $term_id && 'undefined' !== $term_id && 'null' !== $term_id ) {
				$term_id_array = explode( ',', $term_id );        

				$args = array(
					'post_type' => 'apbgf-service',
					'numberposts' => -1,
					'post_status' => 'publish',
					'tax_query' => array(
						array(
							'taxonomy' => 'apbgf-service-category',
							'field'    => 'term_id',
							'terms'    => $term_id_array,
						),
					),
					'meta_query' => array(
						'relation' => 'OR',
						array(
							'key'     => 'location',
							'value'   => $location_id,
							'compare' => 'LIKE',
						),
						array(
							'key'     => 'location',
							'compare' => 'NOT EXISTS',
						),
					),
				);

				$services = get_posts($args);
				
				if (is_array($services) && count($services) > 0) {
					$response['data'] = array_map(function ( $service ) {
						return array(
							'ID' => $service->ID,
							'post_title' => get_the_title($service->ID),
						);
					}, $services);
					$response['status'] = 'success';
				}
			} else {
				$services = self::all_service();

				if (is_array($services) && count($services) > 0) {
					$response['data'] = array_map(function ( $service ) {
						return array(
							'ID' => $service->ID,
							'post_title' => get_the_title($service->ID),
						);
					}, $services);
					$response['status'] = 'success';
				}
			}

			echo wp_json_encode($response);
			wp_die();
		}

		public static function all_service( $service_id = '' ) {

			if ( ! empty( $service_id ) ) {
				$get_service = get_post( $service_id );
				if ( $get_service && 'apbgf-service' == $get_service->post_type ) {                   
					return $get_service;
				}
			}

			$get_service = get_posts( array(
				'post_type' => 'apbgf-service',
				'numberposts' => -1,
				'status' => 'publish',

			));         

			return $get_service;
		}

		public static function get_service_by_id( $service_id ) {
			$response = array();
			
			if ( ! empty( $service_id ) ) {
				$service = get_post( $service_id );
				if ( $service && 'apbgf-service' == $service->post_type ) {
					$price = ! empty( get_post_meta($service->ID, 'price', true) ) ? get_post_meta($service->ID, 'price', true) : '0.00';
					$category = get_post_meta( $service->ID, 'category', true);
					$location = get_post_meta( $service->ID, 'location', true);                 

					$response['name'] = $service->post_title;
					$response['price'] = $price;
					$response['category'] = $category;
					//$response['location'] = $location;
				}
			}

			return $response;
		}

		public static function get_services_by_cat( $category_id = '' ) {

			$get_services = array();

			if ( ! empty( $category_id ) ) {                
				$get_services = get_posts( array(
					'post_type' => 'apbgf-service',
					'numberposts' => -1,
					'status' => 'publish',
					'meta_key' => 'category',
					'meta_value' => $category_id
				));         
			}

			return $get_services;
		}

		public static function all_categories( $category_id = '' ) {

			if ( ! empty( $category_id ) ) {
				$cat_name = get_the_category_by_ID( $category_id );
				if ( ! is_wp_error($cat_name) ) {
					return get_the_category_by_ID( $category_id );
				} else {
					return false;
				}
			}

			$args = array(
				'taxonomy' => 'apbgf-service-category',
				'orderby' => 'name',
				'order'   => 'ASC',
				'hide_empty' => false
			);

			return get_categories($args);
		}

		public function apbgf_delete_service() {

			check_ajax_referer( 'appointment-and-booking-for-gravity-forms', 'nonce' );

			$response = array(
				'message' => esc_html__( 'Service not deleted.', 'appointment-and-booking-for-gravity-forms' ),
				'status'  => 'error'
			);
			$post_id = isset( $_REQUEST['post_id'] ) ? absint( $_REQUEST['post_id'] ) : 0;

			if ( $post_id ) {
				$post = get_post( $post_id );               
				if ( $post && 'apbgf-service' == $post->post_type ) {
					wp_delete_post( $post->ID, true );
					$response['message'] = esc_html__( 'Service deleted successfully.', 'appointment-and-booking-for-gravity-forms' );
					$response['status'] = 'success';
				}
			}

			echo wp_json_encode( $response );
			wp_die();
		}

		public function apbgf_show_service_data() {

			check_ajax_referer( 'appointment-and-booking-for-gravity-forms', 'nonce' );

			$response = array(          
				'status'  => 'error'
			);

			// Whitelist and sanitize only expected input
			$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;

			if ( $post_id ) {

				$post = get_post( $post_id );               
				if ( $post && 'apbgf-service' == $post->post_type ) {

					$price = ! empty( get_post_meta($post->ID, 'price', true) ) ? get_post_meta($post->ID, 'price', true) : '0.00';
					$category = get_post_meta( $post->ID, 'category', true);
					$location = get_post_meta( $post->ID, 'location', true);                    

					$response['name'] = $post->post_title;
					$response['price'] = $price;
					$response['category'] = $category;
					$response['location'] = $location;
					$response['status'] = 'success';
				}
			}

			echo wp_json_encode( $response );
			wp_die();
		}

		public function apbgf_edit_service() {

			check_ajax_referer( 'appointment-and-booking-for-gravity-forms', 'nonce' );

			$response = array(
				'message' => esc_html__( 'Please fill all required fields.', 'appointment-and-booking-for-gravity-forms' ),
				'status'  => 'error'
			);

			$post_id  = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;
			$name     = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
			$category = isset( $_POST['category'] ) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : '';
			$price    = isset( $_POST['price'] ) ? sanitize_text_field( wp_unslash( $_POST['price'] ) ) : '';


			if ( ! $post_id || empty( $name ) || empty( $category ) || $category === 'no' ) {
				echo wp_json_encode( $response );
				wp_die();
			}

			$args = array(
				'ID'         => $post_id,
				'post_title' => $name,
				'post_type'  => 'apbgf-service',
				'post_status'=> 'publish',
				'meta_input' => array(
					'category' => $category,
					'price'    => $price,
				),
			);

			$apbgf_service = wp_update_post( $args, true );

			if ( ! is_wp_error( $apbgf_service ) ) {

				if ( isset( $_POST['location'] ) && is_array( $_POST['location'] ) ) {
					$locations = array_map( 'absint', $_POST['location'] );
					update_post_meta( $apbgf_service, 'location', $locations );
				} else {
					delete_post_meta( $apbgf_service, 'location' );
				}

				wp_set_post_terms( $apbgf_service, $category, 'apbgf-service-category' );

				$response['message'] = esc_html__( 'Service updated successfully.', 'appointment-and-booking-for-gravity-forms' );
				$response['status']  = 'success';

			} else {
				$response['message'] = $apbgf_service->get_error_message();
			}

			echo wp_json_encode( $response );
			wp_die();
		}


		public function apbgf_add_service() {

			check_ajax_referer( 'appointment-and-booking-for-gravity-forms', 'nonce' );

			$response = array(
				'message' => esc_html__( 'Please fill all required fields.', 'appointment-and-booking-for-gravity-forms' ),
				'status'  => 'error'                
			);

			// Sanitize expected inputs only
			$name     = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
			$category = isset( $_POST['category'] ) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : '';
			$price    = isset( $_POST['price'] ) ? sanitize_text_field( wp_unslash( $_POST['price'] ) ) : '';
			$location = isset( $_POST['location'] ) && is_array( $_POST['location'] ) ? array_map( 'absint', $_POST['location'] ) : array();
			
			if ( ! empty( $name ) && ! empty( $category ) && 'no' != $category ) {

				$args = apply_filters( 'apbgf_add_service_args', array(
					'post_title' => $name,
					'post_type' => 'apbgf-service',
					'post_status' => 'publish',
					'wp_error'    => true,
					'meta_input'  => array(
						'category' => $category,
						'price' => $price,
						//'location' => ( isset( $data['location'] ) && is_array( $data['location'] ) && count( $data['location'] ) > 0 ) ? $data['location'] : array(),
					)
				));

				$apbgf_service = wp_insert_post( $args );

				if ( ! is_wp_error( $apbgf_service ) ) {

					if ( ! empty( $location ) ) {
						update_post_meta( $apbgf_service, 'location', $location );
					}
					
					//adding category to service
					wp_set_post_terms( $apbgf_service, $category, 'apbgf-service-category');

					$response['message'] = esc_html__( 'Service created successfully.', 'appointment-and-booking-for-gravity-forms' );
					$response['status'] = 'success';

				} else {
					
					$response['message'] = $wp_user->get_error_message();

				}               

			}

			echo wp_json_encode( $response );
			wp_die();
		}

		public function apbgf_add_custom_column( $column, $postId ) {         

			$arr = RGCurrency::get_currency(GFCommon::get_currency());

			$price = ! empty( get_post_meta($postId, 'price', true) ) ? get_post_meta($postId, 'price', true) : '0.00';         
			
			switch ($column) {

				case 'id':
				echo '&nbsp;&nbsp;' . esc_attr($postId);
					break;

				case 'name':
				echo esc_attr(get_the_title($postId));
					break;

				case 'price':
				echo esc_attr( apbgf_price_format($price) ) ;
					break;              
				
				case 'actions':
				echo '<a href="#" data-type="service" data-id="apbgf-service-modal" data-post_id="' . esc_attr( $postId ) . '" class="wc-dashicons apbgf-edit"> <i class="fa fa-edit customer--icon"></i> </a>';                    
					if ( 'publish' == get_post_status($postId) ) {
						echo '<a href="#" data-type="service" data-post_id="' . esc_attr( $postId ) . '" class="wc-dashicons apbgf-delete" title="Delete"> <i class="fa fa-trash customer--icon"></i>
					</a>';
					}
					break;
			}
		}

		public function apbgf_modify_column_names( $columns ) {           

			unset($columns['date']);
			unset($columns['title']);
			unset($columns['taxonomy-apbgf-service-category']);
			// unset($columns['cb']);
			
			$columns['id'] = __('&nbsp;&nbsp;Service ID', 'appointment-and-booking-for-gravity-forms');
			$columns['name'] = __('Name', 'appointment-and-booking-for-gravity-forms');
			$columns['price'] = __('Price', 'appointment-and-booking-for-gravity-forms');         
			$columns['taxonomy-apbgf-service-category'] = __('Category', 'appointment-and-booking-for-gravity-forms');          
			$columns['actions'] = __('Actions', 'appointment-and-booking-for-gravity-forms');

			return $columns;
		}

		public function apbgf_manage_posts_extra_tablenav( $which ) {
			if ( 'top' === $which ) {
				printf('<a href="#" data-id="apbgf-service-modal" class="apbgf-add-button"><i class="fa fa-plus-circle"></i>%1$s</a>', esc_html__('Add Service', 'appointment-and-booking-for-gravity-forms') );
			}           
		}

		public function highlight_menu() {          

			//adding customer modal view file
			require_once APBGF_ADMIN_VIEW_PATH . 'modal/service-modal.php';  

			$popup_img = defined( 'APBGF_IMAGE_URL' ) ? APBGF_IMAGE_URL . 'ProPopup.svg' : '';
			$popup_url = 'https://gravitybooking.com/pricing/?utm_source=plugin&utm_medium=service';
			if ( $popup_img ) {
				?>
				<div id="apbgf-service-pro-modal" class="modal" role="dialog" aria-modal="true" aria-label="<?php esc_attr_e( 'Export', 'appointment-and-booking-for-gravity-forms' ); ?>">
					<div class="apbgf-service-pro-modal__inner" style="position:relative;max-width:640px;width:100%;margin:auto;">
						<div class="apbgf-service-pro-modal__content" style="position:relative;border-radius:8px;overflow:hidden;">
							<img src="<?php echo esc_url( $popup_img ); ?>" alt="<?php esc_attr_e( 'Upgrade to Pro', 'appointment-and-booking-for-gravity-forms' ); ?>" style="display:block;width:100%;height:auto;" />
							<a href="<?php echo esc_url( $popup_url ); ?>" target="_blank" rel="noopener noreferrer"
							   aria-label="<?php esc_attr_e( 'Buy Gravity Bookings', 'appointment-and-booking-for-gravity-forms' ); ?>"
							   style="position:absolute;left:31%;top:69%;width:38%;height:15%;background:transparent;border:none;outline:none;box-shadow:none;cursor:pointer;text-indent:-9999px;overflow:hidden;">
								<?php esc_html_e( 'Buy Gravity Bookings', 'appointment-and-booking-for-gravity-forms' ); ?>
							</a>
							<button type="button" class="modal__close" aria-label="<?php esc_attr_e( 'Close', 'appointment-and-booking-for-gravity-forms' ); ?>" style="position:absolute;top:0;right:0;width:15%;height:12%;background:transparent;border:none;outline:none;box-shadow:none;cursor:pointer;z-index:2;"></button>
						</div>
					</div>
				</div>
				<?php
			}         
			
			// Register and enqueue the menu highlight script
			wp_register_script('apbgf-menu-highlight', false, array( 'jquery' ));
			wp_enqueue_script('apbgf-menu-highlight');
			
			// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
			wp_add_inline_script('apbgf-menu-highlight', '
				jQuery(document).ready(function($) {
					$("#toplevel_page_gfb li, #toplevel_page_gfb li > a").removeClass("current");
					$("#toplevel_page_gfb a[href=\"edit.php?post_type=apbgf-service\"]").addClass("current");
					$("#toplevel_page_gfb a[href=\"edit.php?post_type=apbgf-service\"]").parent("li").addClass("current");
				});
			');
			// phpcs:enable
		}

		public function menu_highlight( $parent_file ) {
			global $current_screen;

			$taxonomy = $current_screen->taxonomy;
			if ( 'apbgf-service-category' === $taxonomy ) {
				$parent_file = 'appointment-and-booking-for-gravity-forms';
			}

			return $parent_file;
		}

		public function add_service_category_menu() {
			add_submenu_page(
				'appointment-and-booking-for-gravity-forms',
				__( 'Category - Gravity Booking', 'appointment-and-booking-for-gravity-forms' ),
				__( 'Service Categories', 'appointment-and-booking-for-gravity-forms' ),
				'manage_options',
				'edit-tags.php?taxonomy=apbgf-service-category',
				'',
				2
			);        
		}

		public function register_service_taxonomy() {

			// Add new taxonomy, make it hierarchical (like categories)
			$labels = array(
				'name'              => _x( 'Category', 'taxonomy general name', 'appointment-and-booking-for-gravity-forms' ),
				'singular_name'     => _x( 'Category', 'taxonomy singular name', 'appointment-and-booking-for-gravity-forms' ),
				'search_items'      => __( 'Search Category', 'appointment-and-booking-for-gravity-forms' ),
				'all_items'         => __( 'All Category', 'appointment-and-booking-for-gravity-forms' ),
				'parent_item'       => __( 'Parent Category', 'appointment-and-booking-for-gravity-forms' ),
				'parent_item_colon' => __( 'Parent Category:', 'appointment-and-booking-for-gravity-forms' ),
				'edit_item'         => __( 'Edit Category', 'appointment-and-booking-for-gravity-forms' ),
				'update_item'       => __( 'Update Category', 'appointment-and-booking-for-gravity-forms' ),
				'add_new_item'      => __( 'Add New Category', 'appointment-and-booking-for-gravity-forms' ),
				'new_item_name'     => __( 'New Category Name', 'appointment-and-booking-for-gravity-forms' ),
				'menu_name'         => __( 'Category', 'appointment-and-booking-for-gravity-forms' ),
			);
			
			$args = array(
				'hierarchical'      => true,
				'labels'            => $labels,
				'show_ui'           => true,
				'show_in_menu'      => true,
				'show_admin_column' => true,
				'query_var'         => true,
				'rewrite'           => array( 'slug' => 'apbgf-service-category' )                
			);

			register_taxonomy('apbgf-service-category', array( 'apbgf-service' ), $args );
		}

		public function register_service_posttype() {
			$labels = array(
				'name'                  => _x( 'Service', 'Post type general name', 'appointment-and-booking-for-gravity-forms' ),
				'singular_name'         => _x( 'Service', 'Post type singular name', 'appointment-and-booking-for-gravity-forms' ),
				'menu_name'             => _x( 'Service', 'Admin Menu text', 'appointment-and-booking-for-gravity-forms' ),
				'name_admin_bar'        => _x( 'Service', 'Add New on Toolbar', 'appointment-and-booking-for-gravity-forms' ),
				'add_new'               => __( 'Add Service', 'appointment-and-booking-for-gravity-forms' ),
				'add_new_item'          => __( 'Add New Service', 'appointment-and-booking-for-gravity-forms' ),
				'new_item'              => __( 'New Service', 'appointment-and-booking-for-gravity-forms' ),
				'edit_item'             => __( 'Edit Service', 'appointment-and-booking-for-gravity-forms' ),
				'view_item'             => __( 'View Service', 'appointment-and-booking-for-gravity-forms' ),
				'all_items'             => __( 'Services', 'appointment-and-booking-for-gravity-forms' ),
				'search_items'          => __( 'Search Service', 'appointment-and-booking-for-gravity-forms' ),
				'parent_item_colon'     => __( 'Parent Service:', 'appointment-and-booking-for-gravity-forms' ),
				'not_found'             => __( 'No Service found.', 'appointment-and-booking-for-gravity-forms' ),
				'not_found_in_trash'    => __( 'No Service found in Trash.', 'appointment-and-booking-for-gravity-forms' ),
				'featured_image'        => _x( 'Service Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'appointment-and-booking-for-gravity-forms' ),
				'set_featured_image'    => _x( 'Set Service image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'appointment-and-booking-for-gravity-forms' ),
				'remove_featured_image' => _x( 'Remove Service image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'appointment-and-booking-for-gravity-forms' ),
				'use_featured_image'    => _x( 'Use as Service image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'appointment-and-booking-for-gravity-forms' ),
				'archives'              => _x( 'Service archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'appointment-and-booking-for-gravity-forms' ),
				'insert_into_item'      => _x( 'Insert into Service', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'appointment-and-booking-for-gravity-forms' ),
				'uploaded_to_this_item' => _x( 'Uploaded to this Service', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'appointment-and-booking-for-gravity-forms' ),
				'filter_items_list'     => _x( 'Filter Service list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'appointment-and-booking-for-gravity-forms' ),
				'items_list_navigation' => _x( 'Service list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'appointment-and-booking-for-gravity-forms' ),
				'items_list'            => _x( 'Service list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'appointment-and-booking-for-gravity-forms' ),
			);

			$args = array(
				'labels'             => $labels,
				'public'             => false,
				'publicly_queryable' => false,
				'show_ui'            => true,
				'show_in_menu'       => 'appointment-and-booking-for-gravity-forms',
				'show_in_rest'       => true,
				'query_var'          => true,
				'rewrite'            => array( 'slug' => 'apbgf-service' ),
				'capability_type'    => 'post',
				// 'capabilities' => array(
				//  'create_posts' => 'do_not_allow', // Removes support for the "Add New" function ( use 'do_not_allow' instead of false for multisite set ups )
				// ),
				'has_archive'        => true,
				'hierarchical'       => false,
				'supports'           => array( 'title', 'thumbnail' ),              
			);

			register_post_type( 'apbgf-service', $args );
		}


		/**
		 * Singleton Class Instatnce.
		 */
		public static function get_instance() {
			if ( null === self::$instance ) {
				self::$instance = new APBGF_Service();
			}

			return self::$instance;
		}
	}

	APBGF_Service::get_instance();
}
