<?php
/**
 * @package    WordPress
 * @subpackage Traveler
 * @since      1.0
 *
 * Class STAdminActivity
 *
 * Created by ShineTheme
 */
$order_id = 0;
if ( ! class_exists( 'STAdminActivity' ) ) {
	class STAdminActivity extends STAdmin {


		static $booking_page;
		static $_table_version = '1.3.2';
		protected $post_type   = 'st_activity';
		protected static $_inst;

		/**
		 *
		 *
		 * @update 1.1.3
		 * */
		function __construct() {
			add_action( 'init', [ $this, 'init_post_type' ], 8 );

			if ( ! st_check_service_available( $this->post_type ) ) {
				return;
			}

			// Add metabox
			add_action( 'current_screen', [ $this, 'init_metabox' ] );

			add_filter( 'manage_st_activity_posts_columns', [ $this, 'add_col_header' ], 10 );
			add_action( 'manage_st_activity_posts_custom_column', [ $this, 'add_col_content' ], 10, 2 );

			// ===============================================================

			self::$booking_page = admin_url( 'edit.php?post_type=st_activity&page=st_activity_booking' );

			add_action( 'admin_menu', [ $this, 'add_menu_page' ] );

			// Check booking edit and redirect
			if ( self::is_booking_page() ) {
				add_action( 'admin_enqueue_scripts', [ __CLASS__, 'add_edit_scripts' ] );
				add_action( 'admin_init', [ $this, '_do_save_booking' ] );
			}

			if ( isset( $_GET['send_mail'] ) and $_GET['send_mail'] == 'success' ) {
				self::set_message( __( 'Email sent', 'traveler' ), 'updated' );
			}

			add_action( 'wp_ajax_st_room_select_ajax', [ __CLASS__, 'st_room_select_ajax' ] );
			add_action( 'save_post', [ $this, 'meta_update_sale_price' ], 10, 4 );
			add_action( 'save_post', [ $this, 'meta_update_min_price' ], 10, 4 );
			// parent::__construct();

			add_action( 'save_post', [ $this, '_update_list_location' ], 10, 2 );
			add_action( 'save_post', [ $this, '_update_duplicate_data' ], 50, 2 );

			add_action( 'before_delete_post', [ $this, '_delete_data' ], 50 );

			add_action( 'wp_ajax_st_getInfoActivity', [ __CLASS__, '_getInfoActivity' ], 9999 );

			/**
			 *   Since 1.2.4
			 *   auto create & update table st_activity
			 */
			add_action( 'after_setup_theme', [ __CLASS__, '_check_table_activity' ] );

			/**
			 * @since 1.3.0
			 *        Bulk calendar
			 */
			add_action( 'traveler_after_form_activity_calendar', [ $this, 'custom_traveler_after_form_activity_calendar' ] );
			add_action( 'traveler_after_form_submit_activity_calendar', [ $this, 'custom_traveler_after_form_submit_activity_calendar' ] );

			add_action( 'init', [ $this, '__update_db_available_first_run' ], 99 );
			add_action( 'st_availability_cronjob', [ $this, '__cronjob_fill_availability' ] );
			add_action( 'admin_init', [ $this, '_upgradeActivityTable133' ] );
			add_action( 'admin_init', [ $this, '_upgradeColumnDiscountTypeActivityTable' ] );
		}
		public function _upgradeActivityTable133() {
			$updated = get_option( '_upgradeActivityTable133', false );
			if ( ! $updated ) {
				global $wpdb;
				$table = $wpdb->prefix . $this->post_type;
				$sql   = "Update {$table} as t inner join {$wpdb->postmeta} as m on (t.post_id = m.post_id and m.meta_key = 'is_featured') set t.is_featured = m.meta_value";
				$wpdb->query( $sql );
				update_option( '_upgradeActivityTable133', 'updated' );
			}
		}

		public function __cronjob_fill_availability( $offset = 0, $limit = -1, $day = null ) {
			global $wpdb;
			if ( ! $day ) {
				$today = new DateTime( date( 'Y-m-d' ) );
				$today->modify( '+ 6 months' );
				$day = $today->modify( '+ 1 day' );
			}
			$table     = 'st_activity_availability';
			$post_type = 'st_activity';

			$tours       = new WP_Query([
				'posts_per_page' => $limit,
				'post_type'      => $post_type,
				'offset'         => $offset,
			]);
			$insertBatch = [];
			$ids         = [];
			while ( $tours->have_posts() ) {
				$tours->the_post();
				$adult_price = get_post_meta( get_the_ID(), 'adult_price', true );
				if ( empty( $adult_price ) ) {
					$adult_price = 0;
				}
				$child_price = get_post_meta( get_the_ID(), 'child_price', true );
				if ( empty( $child_price ) ) {
					$child_price = 0;
				}
				$infant_price = get_post_meta( get_the_ID(), 'infant_price', true );
				if ( empty( $infant_price ) ) {
					$infant_price = 0;
				}
				$max_people = get_post_meta( get_the_ID(), 'max_people', true );
				if ( empty( $max_people ) ) {
					$max_people = 0;
				}
				$booking_period = get_post_meta( get_the_ID(), 'activity_booking_period', true );
				if ( empty( $booking_period ) ) {
					$booking_period = 0;
				}
				$status        = 'available';
				$insertBatch[] = $wpdb->prepare( '(%d,%d,%d,%s,%s,%s,%s,%d,%d,%d,%d)', $day->getTimestamp(), $day->getTimestamp(), get_the_ID(), $status, $adult_price, $child_price, $infant_price, 1, 0, $max_people, $booking_period );

				$ids[] = get_the_ID();
			}
			if ( ! empty( $insertBatch ) ) {
				$wpdb->query( "INSERT IGNORE INTO {$wpdb->prefix}{$table} (check_in,check_out,post_id,`status`,adult_price,child_price,infant_price,is_base,groupday,`number`,booking_period) VALUES " . implode( ",\r\n", $insertBatch ) );
			}

			wp_reset_postdata();
		}

		public function __run_fill_old_order( $key = '' ) {
			$ids = [];
			global $wpdb;
			$table      = $wpdb->prefix . 'st_activity_availability';
			$model      = ST_Order_Item_Model::inst();
			$orderItems = $model->where( "st_booking_post_type in ('st_activity')", false, true )
								->where( 'check_in_timestamp >= UNIX_TIMESTAMP(CURRENT_DATE)', false, true )
								->where( "STATUS NOT IN('canceled','trash')", false, true )->get()->result();
			if ( ! empty( $orderItems ) ) {
				foreach ( $orderItems as $data ) {
					if ( ! empty( $data['origin_id'] ) ) {
						if ( in_array( $data['id'], $ids ) ) {
							continue;
						}
						$ids[]  = $data['id'];
						$booked = $data['adult_number'] + $data['child_number'] + $data['infant_number'];

						$where = [
							'post_id'  => $data['origin_id'],
							'check_in' => $data['check_in_timestamp'],
						];
						$check = ST_Activity_Availability::inst()->where( $where )->get( 1 )->row();

						if ( $check ) {
							$sql = $wpdb->prepare( "UPDATE {$table} SET number_booked = IFNULL(number_booked, 0) + %d WHERE post_id = %d AND check_in = %s", $booked, $data['origin_id'], $data['check_in_timestamp'] );
							$wpdb->query( $sql );
						} else {
							for ( $i_d = $data['check_in_timestamp']; $i_d <= $data['check_out_timestamp']; $i_d = strtotime( '+1 day', $i_d ) ) {
								$data_insert = [
									'post_id'         => $data['origin_id'],
									'check_in'        => $i_d,
									'check_out'       => $i_d,
									'starttime'       => '',
									'count_starttime' => 1,
									'number'          => get_post_meta( $data['origin_id'], 'max_people', true ),
									'adult_price'     => get_post_meta( $data['origin_id'], 'adult_price', true ),
									'child_price'     => get_post_meta( $data['origin_id'], 'child_price', true ),
									'infant_price'    => get_post_meta( $data['origin_id'], 'infant_price', true ),
									'status'          => 'available',
									'groupday'        => 0,
									'number_booked'   => $booked,
									'booking_period'  => get_post_meta( $data['origin_id'], 'activity_booking_period', true ),
									'is_base'         => 0,
								];
								ST_Activity_Availability::inst()->insert( $data_insert );
							}
						}
					}
				}
			}
		}

		public function __update_db_available_first_run() {
			// Fill old data
			$key = 'st_activity_availability_fill_old_data';
			if ( empty( get_option( $key ) ) ) {
				$model_avail = ST_Availability_Model::inst()
													->where( 'post_type', 'st_activity' )
													->where( 'check_in >= UNIX_TIMESTAMP(CURRENT_DATE)', false, true )->get()->result();
				if ( ! empty( $model_avail ) ) {
					global $wpdb;
					$table       = $wpdb->prefix . 'st_activity_availability';
					$insertBatch = [];
					$ids         = [];
					$i           = 1;
					foreach ( $model_avail as $k => $v ) {
						$max_people = get_post_meta( $v['post_id'], 'max_people', true );
						if ( empty( $max_people ) ) {
							$max_people = 0;
						}
						$booking_period = get_post_meta( $v['post_id'], 'activity_booking_period', true );
						if ( empty( $booking_period ) ) {
							$booking_period = 0;
						}
						$insertBatch[] = $wpdb->prepare( '(%d,%s,%s,%s,%d,%d,%s,%s,%s,%s,%s,%d,%d,%d,%d)', $v['post_id'], $v['check_in'], $v['check_out'], $v['starttime'], $v['count_starttime'], $max_people, $v['price'], $v['adult_price'], $v['child_price'], $v['infant_price'], $v['status'], $v['groupday'], 0, $booking_period, 1 );
						++$i;
						$ids[] = $v['id'];
					}
					if ( ! empty( $insertBatch ) ) {
						$res = $wpdb->query( "INSERT IGNORE INTO {$table} (post_id, check_in, check_out, starttime, count_starttime, `number`, price, adult_price, child_price, infant_price, `status`, groupday, number_booked, booking_period, is_base) VALUES " . implode( ",\r\n", $insertBatch ) );
						if ( $res ) {
							$sql_delete = $wpdb->prepare( "DELETE FROM {$wpdb->prefix}st_availability WHERE post_type=%s", 'st_activity' );
							$wpdb->query( $sql_delete );
						}
					}
				}
				update_option( $key, 1 );
			}
		}

		public function custom_traveler_after_form_activity_calendar() {
			echo balanceTags( st()->load_template( 'tours/tour-calendar', false ) );
		}

		public function custom_traveler_after_form_submit_activity_calendar() {
			echo '<button type="button" id="calendar-bulk-edit" class="option-tree-ui-button button button-primary button-large btn btn-primary btn-sm" style="float: right;">' . __( 'Bulk Edit', 'traveler' ) . '</button>';
		}

		static function check_ver_working() {
			$dbhelper = new DatabaseHelper( self::$_table_version );

			return $dbhelper->check_ver_working( 'st_activity_table_version' );
		}

		static function _check_table_activity() {
			$dbhelper = new DatabaseHelper( self::$_table_version );
			$dbhelper->setTableName( 'st_activity' );
			$column = [
				'post_id'                 => [
					'type'   => 'INT',
					'length' => 11,
				],
				'multi_location'          => [
					'type' => 'text',
				],
				'id_location'             => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'address'                 => [
					'type' => 'text',
				],
				'price'                   => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'sale_price'              => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'child_price'             => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'adult_price'             => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'infant_price'            => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'min_price'               => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'type_activity'           => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'check_in'                => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'check_out'               => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'rate_review'             => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'activity_booking_period' => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'max_people'              => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'duration'                => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'is_sale_schedule'        => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'discount'                => [
					'type'   => 'varchar',
					'length' => 255,
				],
				'sale_price_from'         => [
					'type'   => 'date',
					'length' => 255,
				],
				'sale_price_to'           => [
					'type'   => 'date',
					'length' => 255,
				],
				'is_featured'             => [
					'type'   => 'varchar',
					'length' => 5,
				],
				'discount_type'           => [
					'type'   => 'varchar',
					'length' => 10,
				],
			];

			$column = apply_filters( 'st_change_column_st_activity', $column );

			$dbhelper->setDefaultColums( $column );
			$dbhelper->check_meta_table_is_working( 'st_activity_table_version' );

			return array_keys( $column );
		}

		static function _getInfoActivity() {
			$activity_id = intval( STInput::request( 'activity_id', '' ) );
			$result      = [
				'type_activity' => '',
				'max_people'    => 0,
				'adult_html'    => '',
				'child_html'    => '',
				'infant_html'   => '',
				'child_price'   => '',
				'adult_price'   => '',
				'infant_price'  => '',
			];
			$duration    = '';
			if ( get_post_type( $activity_id ) == 'st_activity' ) {
				$type_activity   = get_post_meta( $activity_id, 'type_activity', true );
				$max_people      = intval( get_post_meta( $activity_id, 'max_people', true ) );
				$max_people_html = $max_people;
				if ( $max_people < 1 ) {
					$max_people = 20;
				}

				$child_price            = get_post_meta( $activity_id, 'child_price', true );
				$adult_price            = get_post_meta( $activity_id, 'adult_price', true );
				$infant_price           = get_post_meta( $activity_id, 'infant_price', true );
				$result['child_price']  = $child_price;
				$result['adult_price']  = $adult_price;
				$result['infant_price'] = $infant_price;

				$adult_html  = '<select name="adult_number" class="form-control" style="width: 100%">';
				$child_html  = '<select name="child_number" class="form-control" style="width: 100%">';
				$infant_html = '<select name="infant_number" class="form-control" style="width: 100%">';
				for ( $i = 0; $i <= $max_people; $i++ ) {
					$adult_html  .= '<option value="' . $i . '">' . $i . '</option>';
					$child_html  .= '<option value="' . $i . '">' . $i . '</option>';
					$infant_html .= '<option value="' . $i . '">' . $i . '</option>';
				}
				$adult_html .= '</select>';
				$child_html .= '</select>';
				$child_html .= '</select>';

				if ( $type_activity && $type_activity == 'daily_activity' ) {
					$html                          = "<select name='type_activity' class='form-control form-control-admin'>
                        <option value='daily_activity'>" . __( 'Daily Activity', 'traveler' ) . '</option>
                    </select>';
					$result['type_activity']       = $html;
					$result['activity_text']       = $type_activity;
					$duration                      = get_post_meta( $activity_id, 'duration', true );
					$result['label_type_activity'] = __( 'Daily Activity', 'traveler' );
				} elseif ( $type_activity && $type_activity == 'specific_date' ) {
					$html = "<select name='type_activity' class='form-control form-control-admin'>
                        <option value='specific_date'>" . __( 'Specific Date', 'traveler' ) . '</option>
                    </select>';

					$result['type_activity']       = $html;
					$result['activity_text']       = $type_activity;
					$result['label_type_activity'] = __( 'Specific Date', 'traveler' );
				}
				$extras = get_post_meta( $activity_id, 'extra_price', true );
				if ( is_array( $extras ) && count( $extras ) ) :
					$html_extra = '<table class="table">';
					foreach ( $extras as $key => $val ) :
						$html_extra .= '
                    <tr>
                        <td width="80%">
                            <label for="' . $val['extra_name'] . '" class="ml20">' . $val['title'] . ' (' . TravelHelper::format_money( $val['extra_price'] ) . ')' . '</label>
                            <input type="hidden" name="extra_price[price][' . $val['extra_name'] . ']" value="' . $val['extra_price'] . '">
                            <input type="hidden" name="extra_price[title][' . $val['extra_name'] . ']" value="' . $val['title'] . '">
                        </td>
                        <td width="20%">
                            <select style="width: 100%" class="form-control" name="extra_price[value][' . $val['extra_name'] . ']" id="">';

						$max_item = intval( $val['extra_max_number'] );
						if ( $max_item <= 0 ) {
							$max_item = 1;
						}
						for ( $i = 0; $i <= $max_item; $i++ ) :
							$html_extra .= '<option value="' . $i . '">' . $i . '</option>';
							endfor;
						$html_extra .= '
                            </select>
                        </td>
                    </tr>';
						endforeach;
					$html_extra .= '</table>';
					endif;

				$disable_adult_name    = get_post_meta( $activity_id, 'disable_adult_name', true );
				$disable_children_name = get_post_meta( $activity_id, 'disable_children_name', true );
				$disable_infant_name   = get_post_meta( $activity_id, 'disable_infant_name', true );

				if ( $disable_adult_name == 'off' || $disable_children_name == 'off' || $disable_infant_name == 'off' ) {
					$html_required_name .= '
                        <div class="guest_name_input d-none"
                            data-placeholder="' . esc_html__( 'Guest %d name', 'traveler' ) . '"
                            data-hide-adult="' . get_post_meta( $activity_id, 'disable_adult_name', true ) . '"
                            data-hide-children="' . get_post_meta( $activity_id, 'disable_children_name', true ) . '"
                            >
                            <label><span>' . esc_html__( 'Guest Name', 'traveler' ) . '</span> <span class="required">*</span></label>
                            <div class="guest_name_control">

                            </div>
                            <script type="text/html" id="guest_name_control_item">
                                <div class="control-item mb10">
                                    <select name="guest_title[]" class="form-control">
                                    <option value="mr">' . esc_html__( 'Mr', 'traveler' ) . '</option>
                                    <option value="miss">' . esc_html__( 'Miss', 'traveler' ) . '</option>
                                    <option value="mrs">' . esc_html__( 'Mrs', 'traveler' ) . '</option>
                                    </select>
                                    <input class="form-control " placeholder="" name="guest_name[]" value="">
                                </div>
                            </script>
                        </div>
                        ';
				}
				$result['extras']             = $html_extra;
				$result['max_people']         = ( $max_people_html == 0 ) ? __( 'Unlimited', 'traveler' ) : $max_people_html;
				$result['adult_html']         = $adult_html;
				$result['child_html']         = $child_html;
				$result['infant_html']        = $infant_html;
				$result['duration']           = $duration;
				$result['required_name_html'] = $html_required_name;
			}

			echo json_encode( $result );
			die();
		}

		function _do_save_booking() {
			$section = isset( $_GET['section'] ) ? $_GET['section'] : false;
			switch ( $section ) {
				case 'edit_order_item':
					$item_id = isset( $_GET['order_item_id'] ) ? $_GET['order_item_id'] : false;
					if ( ! $item_id or get_post_type( $item_id ) != 'st_order' ) {
						return false;
					}
					if ( isset( $_POST['submit'] ) and $_POST['submit'] ) {
						$this->_save_booking( $item_id );
					}
					break;
				case 'resend_email_activity':
					$this->_resend_mail();
					break;
			}
		}

		static function _update_duplicate_data( $id, $data ) {
			if ( ! TravelHelper::checkTableDuplicate( 'st_activity' ) ) {
				return;
			}

			if ( get_post_type( $id ) == 'st_activity' ) {
				$num_rows = TravelHelper::checkIssetPost( $id, 'st_activity' );

				$location_str            = get_post_meta( $id, 'multi_location', true );
				$location_id             = '';                                                           // multi location
				$address                 = get_post_meta( $id, 'address', true );
				$price                   = get_post_meta( $id, 'price', true );                          // removed
				$type_activity           = get_post_meta( $id, 'type_activity', true );
				$check_in                = get_post_meta( $id, 'check_in', true );
				$check_out               = get_post_meta( $id, 'check_out', true );
				$activity_booking_period = get_post_meta( $id, 'activity_booking_period', true );
				$max_people              = get_post_meta( $id, 'max_people', true );
				$duration                = get_post_meta( $id, 'duration', true );
				$sale_price              = (float) get_post_meta( $id, 'price', true );
				$child_price             = get_post_meta( $id, 'child_price', true );
				$adult_price             = get_post_meta( $id, 'adult_price', true );
				$infant_price            = get_post_meta( $id, 'infant_price', true );
				$off_adult               = get_post_meta( $id, 'hide_adult_in_booking_form', true );
				$off_child               = get_post_meta( $id, 'hide_children_in_booking_form', true );
				$off_infant              = get_post_meta( $id, 'hide_infant_in_booking_form', true );
				if ( $off_adult == 'on' ) {
					$adult_price = 0;
				}
				if ( $off_child == 'on' ) {
					$child_price = 0;
				}
				if ( $off_infant == 'on' ) {
					$infant_price = 0;
				}

				$min_price        = get_post_meta( $id, 'min_price', true );
				$discount         = get_post_meta( $id, 'discount', true );
				$is_sale_schedule = get_post_meta( $id, 'is_sale_schedule', true );
				$sale_from        = get_post_meta( $id, 'sale_price_from', true );
				$sale_to          = get_post_meta( $id, 'sale_price_to', true );
				$is_featured      = get_post_meta( $id, 'is_featured', true );
				$discount_type    = get_post_meta( $id, 'discount_type', true );
				if ( $is_sale_schedule == 'on' ) {
					if ( $sale_from and $sale_from ) {
						$today     = date( 'Y-m-d' );
						$sale_from = date( 'Y-m-d', strtotime( $sale_from ) );
						$sale_to   = date( 'Y-m-d', strtotime( $sale_to ) );
						if ( ( $today >= $sale_from ) && ( $today <= $sale_to ) ) {

						} else {

							$discount = 0;
						}
					} else {
						$discount = 0;
					}
				}

				if ( $discount ) {
					if ( $discount_type == 'amount' ) {
						$sale_price = $sale_price - $discount;
					} else {
						$sale_price = $sale_price - ( $sale_price / 100 ) * $discount;
					}
				}
				$rate_review = STReview::get_avg_rate( $id ); // rate review

				if ( $num_rows == 1 ) {
					$data  = [
						'multi_location'          => $location_str,
						'id_location'             => $location_id,
						'address'                 => $address,
						'price'                   => $price,                     // removed
						'type_activity'           => $type_activity,
						'check_in'                => $check_in,
						'check_out'               => $check_out,
						'sale_price'              => $sale_price,
						'child_price'             => $child_price,
						'adult_price'             => $adult_price,
						'infant_price'            => $infant_price,
						'min_price'               => $min_price,
						'rate_review'             => $rate_review,
						'activity_booking_period' => $activity_booking_period,
						'max_people'              => $max_people,
						'duration'                => $duration,
						'discount'                => $discount,
						'sale_price_from'         => $sale_from,
						'sale_price_to'           => $sale_to,
						'is_sale_schedule'        => $is_sale_schedule,
						'is_featured'             => $is_featured,
						'discount_type'           => $discount_type,
					];
					$where = [
						'post_id' => $id,
					];
					TravelHelper::updateDuplicate( 'st_activity', $data, $where );
				} elseif ( $num_rows == 0 ) {
					$data = [
						'post_id'                 => $id,
						'multi_location'          => $location_str,
						'id_location'             => $location_id,
						'address'                 => $address,
						'price'                   => $price,
						'type_activity'           => $type_activity,
						'check_in'                => $check_in,
						'check_out'               => $check_out,
						'sale_price'              => $sale_price,
						'child_price'             => $child_price,
						'adult_price'             => $adult_price,
						'infant_price'            => $infant_price,
						'min_price'               => $min_price,
						'rate_review'             => $rate_review,
						'activity_booking_period' => $activity_booking_period,
						'max_people'              => $max_people,
						'duration'                => $duration,
						'discount'                => $discount,
						'sale_price_from'         => $sale_from,
						'sale_price_to'           => $sale_to,
						'is_sale_schedule'        => $is_sale_schedule,
						'is_featured'             => $is_featured,
						'discount_type'           => $discount_type,
					];
					TravelHelper::insertDuplicate( 'st_activity', $data );
				}
				// Update meta to st_availability when save post
				$model = ST_Activity_Availability::inst();
				$model->where( 'post_id', $id )
						->where( 'check_in >= UNIX_TIMESTAMP(CURRENT_DATE)', true, false )
						->update([
							'number'         => empty( $max_people ) ? 0 : $max_people,
							'booking_period' => empty( $activity_booking_period ) ? 0 : $activity_booking_period,
						]);

					$data_prices = $model->get_prices_by_id( $id );
				if ( ! empty( $data_prices ) ) {
					$data_price      = $data_prices[0];
					$adult_price_db  = $data_price['adult_price'];
					$child_price_db  = $data_price['child_price'];
					$infant_price_db = $data_price['infant_price'];
					if ( $adult_price_db != $adult_price or $child_price_db != $child_price or $infant_price_db != $infant_price ) {
						$model->where( [
							'post_id' => $id,
							'is_base' => 1,
						] )
							->where( 'check_in', 'UNIX_TIMESTAMP(CURRENT_DATE)', '>=' )
							->update([
								'adult_price'  => $adult_price,
								'child_price'  => $child_price,
								'infant_price' => $infant_price,
							]);
					}
				} else {
					$new_check_in  = strtotime( 'today UTC' );
					$new_check_out = strtotime( '+90 day', $new_check_in );
					for ( $i = $new_check_in; $i <= $new_check_out; $i = strtotime( '+1 day', $i ) ) {
						ST_Activity_Availability::inst()->insertOrUpdate([
                            'post_id'         => $id,
                            'check_in'        => $i,
                            'check_out'       => $i,
                            'adult_price'     => $adult_price,
                            'child_price'     => $child_price,
                            'infant_price'    => $infant_price,
                            'status'          => 'available',
                            'groupday'        => 0,
                            'is_base'         => 1,
                            'number'          => 0,
                            'booking_period'  => 0,
                            'count_starttime' => 1
                        ]);
					}
				}
			}
		}

		public function _delete_data( $post_id ) {
			if ( get_post_type( $post_id ) == 'st_activity' ) {
				global $wpdb;
				$table = $wpdb->prefix . 'st_activity';
				$rs    = TravelHelper::deleteDuplicateData( $post_id, $table );
				if ( ! $rs ) {
					return false;
				}

				return true;
			}
		}

		/**
		 * @since 1.1.7
		 **/
		function _update_list_location( $id, $data ) {
			$location = STInput::request( 'multi_location', '' );
			if ( isset( $_REQUEST['multi_location'] ) ) {
				if ( is_array( $location ) && count( $location ) ) {
					$location_str = '';
					foreach ( $location as $item ) {
						if ( empty( $location_str ) ) {
							$location_str .= $item;
						} else {
							$location_str .= ',' . $item;
						}
					}
				} else {
					$location_str = '';
				}
				update_post_meta( $id, 'multi_location', $location_str );
				update_post_meta( $id, 'id_location', '' );
			}
		}

		/**
		 *
		 *
		 *
		 * */
		function init_post_type() {
			if ( ! st_check_service_available( $this->post_type ) ) {
				return;
			}

			if ( ! function_exists( 'st_reg_post_type' ) ) {
				return;
			}
			// Activity ==============================================================

			$labels = [
				'name'                  => __( 'Activity', 'traveler' ),
				'singular_name'         => __( 'Activity', 'traveler' ),
				'menu_name'             => __( 'Activity', 'traveler' ),
				'name_admin_bar'        => __( 'Activity', 'traveler' ),
				'add_new'               => __( 'Add New', 'traveler' ),
				'add_new_item'          => __( 'Add New Activity', 'traveler' ),
				'new_item'              => __( 'New Activity', 'traveler' ),
				'edit_item'             => __( 'Edit Activity', 'traveler' ),
				'view_item'             => __( 'View Activity', 'traveler' ),
				'all_items'             => __( 'All Activity', 'traveler' ),
				'search_items'          => __( 'Search Activity', 'traveler' ),
				'parent_item_colon'     => __( 'Parent Activity:', 'traveler' ),
				'not_found'             => __( 'No Activity found.', 'traveler' ),
				'not_found_in_trash'    => __( 'No Activity found in Trash.', 'traveler' ),
				'insert_into_item'      => __( 'Insert into Activity', 'traveler' ),
				'uploaded_to_this_item' => __( 'Uploaded to this Activity', 'traveler' ),
				'featured_image'        => __( 'Feature Image', 'traveler' ),
				'set_featured_image'    => __( 'Set featured image', 'traveler' ),
			];

			$args = [
				'labels'             => $labels,
				'public'             => true,
				'publicly_queryable' => true,
				'show_ui'            => true,
				'query_var'          => true,
				'rewrite'            => [ 'slug' => get_option( 'activity_permalink', 'st-activity' ) ],
				'capability_type'    => 'post',
				'hierarchical'       => false,
				'supports'           => [ 'author', 'title', 'editor', 'excerpt', 'thumbnail', 'comments' ],
				'menu_icon'          => 'dashicons-tickets-alt-st',
			];
			st_reg_post_type( 'st_activity', $args );
		}

		/**
		 *
		 *
		 * @since 1.1.1
		 * */
		function init_metabox() {
			$screen = get_current_screen();
			if ( $screen->id != 'st_activity' ) {
				return false;
			}
			$array_elementor = [];
			$layout_single   = [
				[
					'value' => '1',
					'label' => esc_html__( 'Layout 1', 'traveler' ),
					'src'   => get_template_directory_uri() . '/v2/images/layouts/activity_detail_1_preview.jpg',
				],
				[
					'value' => '2',
					'label' => esc_html__( 'Layout 2', 'traveler' ),
					'src'   => get_template_directory_uri() . '/v2/images/layouts/activity_detail_2_preview.jpg',
				],
				[
					'value' => '3',
					'label' => esc_html__( 'Layout 3', 'traveler' ),
					'src'   => get_template_directory_uri() . '/v2/images/layouts/activity_detail_3_preview.jpg',
				],

			];
			if ( check_using_elementor() ) {
				$array_elementor = [
					[
						'value' => '4',
						'label' => esc_html__( 'Layout modern 1', 'traveler' ),
						'src'   => get_template_directory_uri() . '/v2/images/layouts/tour_detail_layout_8.jpg',
					],
					[
						'value' => '5',
						'label' => esc_html__( 'Layout modern 2', 'traveler' ),
						'src'   => get_template_directory_uri() . '/v2/images/layouts/tour_detail_layout_9.jpg',
					],
				];
				$layout_single   = array_merge( $layout_single, $array_elementor );
			}
			$layout_single   = apply_filters( 'st_layout_single_activity', $layout_single );
			$this->metabox[] = [
				'id'       => 'activity_metabox',
				'title'    => __( 'Activity Setting', 'traveler' ),
				'pages'    => [ 'st_activity' ],
				'context'  => 'normal',
				'priority' => 'high',
				'fields'   => [
					[
						'label' => __( 'Location', 'traveler' ),
						'id'    => 'location_reneral_tab',
						'type'  => 'tab',
					],
					[
						'label'     => __( 'Activity location', 'traveler' ),
						'id'        => 'multi_location', // id_location
						'type'      => 'list_item_post_type',
						'post_type' => 'location',
						'desc'      => __( 'Select one or more location for your activity', 'traveler' ),
					],
					[
						'label' => __( 'Real activity address ', 'traveler' ),
						'id'    => 'address',
						'type'  => 'address_autocomplete',
						'desc'  => __( 'Input your activity address detail', 'traveler' ),
					],
					[
						'label' => __( 'Location on map', 'traveler' ),
						'id'    => 'st_google_map',
						'type'  => 'bt_gmap',
						'std'   => 'off',
						'desc'  => __( 'Kindly input Map API in Theme Settings > Other Options . Select one location on map to see latiture and longiture', 'traveler' ),
					],
					[
						'label'    => __( 'Properties near by', 'traveler' ),
						'id'       => 'properties_near_by',
						'type'     => 'list-item',
						'desc'     => __( 'Properties near by this activity', 'traveler' ),
						'settings' => [
							[
								'id'    => 'featured_image',
								'label' => __( 'Featured Image', 'traveler' ),
								'type'  => 'upload',
							],
							[
								'id'    => 'description',
								'label' => __( 'Description', 'traveler' ),
								'type'  => 'textarea',
								'row'   => 5,
							],
							[
								'id'    => 'icon',
								'label' => __( 'Map icon', 'traveler' ),
								'type'  => 'upload',
							],
							[
								'id'    => 'map_lat',
								'label' => __( 'Map lat', 'traveler' ),
								'type'  => 'text',
							],
							[
								'id'    => 'map_lng',
								'label' => __( 'Map long', 'traveler' ),
								'type'  => 'text',
							],
						],
					],
					[
						'label' => __( 'Street view mode', 'traveler' ),
						'id'    => 'enable_street_views_google_map',
						'type'  => 'on-off',
						'desc'  => __( 'Turn on/off street view mode for this location', 'traveler' ),
						'std'   => 'off',
					],

					[
						'label' => __( 'Badges', 'traveler' ),
						'id'    => 'badge_tab',
						'type'  => 'tab',
					],
					[
						'id'        => 'list_badges',
						'label'     => __( 'Add badge', 'traveler' ),
						'desc'  => __( 'ONLY show in Single Service Page and default Modal Checkout', 'traveler' ),
						'type'      => 'list-item',
						'settings'  => [
							[
								'id'    => 'content',
								'label' => __( 'Content', 'traveler' ),
								'type'  => 'text',
							],
							[
								'id'    => 'icon',
								'label' => __( 'Icon', 'traveler' ),
								'type'  => 'upload',
							],
						],
					],

					[
						'label' => __( 'General', 'traveler' ),
						'id'    => 'room_reneral_tab',
						'type'  => 'tab',
					],
					[
						'label' => __( 'Set activity as feature', 'traveler' ),
						'id'    => 'is_featured',
						'type'  => 'on-off',
						'desc'  => __( 'Will show this activity with feature label or not', 'traveler' ),
						'std'   => 'off',
					],
					[
						'label'   => __( 'Upsell', 'traveler' ),
						'id'      => 'st_upsell',
						'type' => 'upsell_select_ajax',
						'desc' => __('Select items for Upsell (ONLY for the default Modal Checkout Success Page). Will show only items which belong to the chosen Type Service', 'traveler'),
                        'multi' => true,
                        'placeholder' => __('Search item for Upsell', 'traveler')
					],


					/**
					 * version 2.7.4
					 */
					[
						'label'   => __( 'Booking Options', 'traveler' ),
						'id'      => 'st_booking_option_type',
						'type'    => 'select',
						'choices' => [
							[
								'label' => __( 'Instant Booking', 'traveler' ),
								'value' => 'instant',
							],
							[
								'label' => __( 'Enquire Booking', 'traveler' ),
								'value' => 'enquire',
							],
							[
								'label' => __( 'Instant & Enquire Booking', 'traveler' ),
								'value' => 'instant_enquire',
							],
						],
						'std'     => 'instant',
					],

					[
						'label'     => __( 'Activity single layout', 'traveler' ),
						'id'        => 'st_custom_layout_new',
						'post_type' => 'st_layouts',
						'desc'      => __( 'Select one activity single layout', 'traveler' ),
						'type'      => 'radio-image',
						'section'   => 'layout_tab',
						'class'     => 'custom-radio-image',
						'choices'   => $layout_single,
					],

					[
						'label' => __( 'Activity gallery', 'traveler' ),
						'id'    => 'gallery',
						'type'  => 'gallery',
						'desc'  => __( 'Upload activity images to show to customers', 'traveler' ),
					],

					[
						'label' => __( 'Activity video', 'traveler' ),
						'id'    => 'video',
						'type'  => 'text',
						'desc'  => __( 'Input youtube/vimeo url here. With youtube, use original URL - not shorten URL', 'traveler' ),
					],
					[
						'label' => __( 'Turn on/off Pick up', 'traveler' ),
						'id'    => 'enable_pickup',
						'type'  => 'on-off',
						'desc'  => __( 'Turn on/off Pick up field for activity booking. Applied ONLY for the default Modal Checkout', 'traveler' ),
						'std'   => 'off',
					],
					[
						'label' => __( 'Contact information', 'traveler' ),
						'id'    => 'agent_tab',
						'type'  => 'tab',
					],

					[
						'label' => __( 'Email address', 'traveler' ),
						'id'    => 'contact_email',
						'type'  => 'text',
						'desc'  => __( 'Email address', 'traveler' ),
					],
					[
						'label' => __( 'Website', 'traveler' ),
						'id'    => 'contact_web',
						'type'  => 'text',
						'desc'  => __( 'Website address', 'traveler' ),
					],
					[
						'label' => __( 'Phone', 'traveler' ),
						'id'    => 'contact_phone',
						'type'  => 'text',
						'desc'  => __( 'Phone number', 'traveler' ),
					],
					[
						'label' => __( 'Fax number', 'traveler' ),
						'id'    => 'contact_fax',
						'type'  => 'text',
						'desc'  => __( 'Fax number', 'traveler' ),
					],

					[
						'label' => __( 'Info settings', 'traveler' ),
						'id'    => 'activity_time_number_tab',
						'type'  => 'tab',
					],
					[
						'label'   => __( 'Activity type', 'traveler' ),
						'id'      => 'type_activity',
						'type'    => 'select',
						'desc'    => __( 'Activity type', 'traveler' ),
						'choices' => [
							[
								'value' => 'specific_date',
								'label' => __( 'Specific Date', 'traveler' ),
							],
							[
								'value' => 'daily_activity',
								'label' => __( 'Daily Activity', 'traveler' ),
							],
						],
					],
					[
						'label' => __( 'Duration', 'traveler' ),
						'id'    => 'duration',
						'type'  => 'text',
						'desc'  => __( 'The total time to take each activity package', 'traveler' ),
					],

					[
						'label'        => __( 'Booking period', 'traveler' ),
						'id'           => 'activity_booking_period',
						'type'         => 'numeric-slider',
						'min_max_step' => '0,30,1',
						'std'          => 0,
						'desc'         => __( 'Booking time period prior to arrival.', 'traveler' ),
					],
					// [
					// 	'label' => __( 'Activity time', 'traveler' ),
					// 	'id'    => 'activity-time',
					// 	'type'  => 'text',
					// 	'desc'  => __( 'The departure time of an activity', 'traveler' ),
					// ],
					[
						'label' => __( 'Min number of people', 'traveler' ),
						'id'    => 'min_people',
						'type'  => 'numeric-slider',
						'desc'  => __( 'Min number of people', 'traveler' ),
						'std'   => 1,
					],
					[
						'label'        => __( 'Max number of people', 'traveler' ),
						'id'           => 'max_people',
						'type'         => 'numeric-slider',
						'desc'         => __( 'Max number of people. Enter \'0\' for unlimited', 'traveler' ),
						'min_max_step' => '0,100,1',
						'std'          => 1,
					],
					[
						'id'    => 'activity_include',
						'label' => __( 'Activity Included', 'traveler' ),
						'type'  => 'textarea',
						'rows'  => 5,
					],
					[
						'id'    => 'activity_exclude',
						'label' => __( 'Activity Excluded', 'traveler' ),
						'type'  => 'textarea',
						'rows'  => 5,
					],
					[
						'id'    => 'activity_highlight',
						'label' => __( 'Activity Highlight', 'traveler' ),
						'type'  => 'textarea',
						'rows'  => 5,
					],
					[
						'id'      => 'activity_program_style',
						'label'   => __( 'Activity program style', 'traveler' ),
						'type'    => 'select',
						'choices' => [
							[
								'label' => __( 'Image with text', 'traveler' ),
								'value' => 'style1',
							],
							[
								'label' => __( 'Background image', 'traveler' ),
								'value' => 'style2',
							],
							[
								'label' => __( 'Text with icon', 'traveler' ),
								'value' => 'style3',
							],
						],
						'std'     => 'style1',
					],
					[
						'id'        => 'activity_program',
						'label'     => __( 'Activity program', 'traveler' ),
						'type'      => 'list-item',
						'condition' => 'activity_program_style:not(style2)',
						'settings'  => [
							[
								'id'    => 'image',
								'label' => __( 'Image', 'traveler' ),
								'type'  => 'upload',
							],
							[
								'id'    => 'desc',
								'label' => __( 'Description', 'traveler' ),
								'type'  => 'wpeditor',
								'rows'  => '5',
							],
						],
					],
					[
						'id'        => 'activity_program_bgr',
						'label'     => __( 'Activity program', 'traveler' ),
						'type'      => 'list-item',
						'condition' => 'activity_program_style:is(style2)',
						'settings'  => [
							[
								'id'    => 'time',
								'label' => __( 'Sub title', 'traveler' ),
								'type'  => 'text',
							],
							[
								'id'    => 'image',
								'label' => __( 'Image', 'traveler' ),
								'type'  => 'upload',
							],
							[
								'id'    => 'desc',
								'label' => __( 'Description', 'traveler' ),
								'type'  => 'wpeditor',
								'rows'  => '5',
							],
						],
					],

					[
						'id'       => 'activity_faq',
						'label'    => __( 'Activity FAQ', 'traveler' ),
						'type'     => 'list-item',
						'settings' => [
							[
								'id'    => 'desc',
								'label' => __( 'Description', 'traveler' ),
								'type'  => 'wpeditor',
								'rows'  => '5',
							],
						],
					],
					[
						'label' => __( 'Price setting', 'traveler' ),
						'id'    => 'price_number_tab',
						'type'  => 'tab',
					],
					[
						'label'     => __( 'Adult price', 'traveler' ),
						'id'        => 'adult_price',
						'type'      => 'text',
						'desc'      => __( 'Price per adult. Input number only. No decimal or thousand separator or any other character', 'traveler' ),
						'std'       => 0,
						'condition' => 'hide_adult_in_booking_form:is(off)',
						'is_number' => true,
					],
					[
						'label'     => __( 'Fields list discount by adult number booking', 'traveler' ),
						'id'        => 'discount_by_adult',
						'type'      => 'list-item',
						'desc'      => __( 'Fields list discount by adult number booking', 'traveler' ),
						'std'       => 0,
						'settings'  => [
							[
								'id'    => 'key',
								'label' => __( 'Number of adult (From)', 'traveler' ),
								'type'  => 'text',
								'is_number' => true,
							],
							[
								'id'    => 'key_to',
								'label' => __( 'Number of adult (To)', 'traveler' ),
								'type'  => 'text',
								'is_number' => true,
							],
							[
								'id'    => 'value',
								'label' => __( 'Value of discount', 'traveler' ),
								'desc'  => __( 'Enter amount: For example: 50', 'traveler' ),
								'type'  => 'text',
								'is_number' => true,
							],
						],
						'condition' => 'hide_adult_in_booking_form:is(off)',
					],

					[
						'label'     => __( 'Child price', 'traveler' ),
						'id'        => 'child_price',
						'type'      => 'text',
						'desc'      => __( 'Price per child. Input number only. No decimal or thousand separator or any other character', 'traveler' ),
						'std'       => 0,
						'condition' => 'hide_children_in_booking_form:is(off)',
						'is_number' => true,
					],
					[
						'label'     => __( 'Fields list discount by child number booking', 'traveler' ),
						'id'        => 'discount_by_child',
						'type'      => 'list-item',
						'desc'      => __( 'Fields list discount by child number booking', 'traveler' ),
						'std'       => 0,
						'settings'  => [
							[
								'id'    => 'key',
								'label' => __( 'Number of children (From)', 'traveler' ),
								'type'  => 'text',
								'is_number' => true,
							],
							[
								'id'    => 'key_to',
								'label' => __( 'Number of children (To)', 'traveler' ),
								'type'  => 'text',
								'is_number' => true,
							],
							[
								'id'    => 'value',
								'label' => __( 'Value percent of discount', 'traveler' ),
								'desc'  => __( 'Enter amount: For example: 50', 'traveler' ),
								'type'  => 'text',
								'is_number' => true,
							],
						],
						'condition' => 'hide_children_in_booking_form:is(off)',
					],

					[
						'id'      => 'discount_by_people_type',
						'label'   => __( 'Type of discount by people', 'traveler' ),
						'type'    => 'select',
						'choices' => [
							[
								'label' => __( 'Percent', 'traveler' ),
								'value' => 'percent',
							],
							[
								'label' => __( 'Amount', 'traveler' ),
								'value' => 'amount',
							],
						],
					],
					[
						'label'     => __( 'Infant price', 'traveler' ),
						'id'        => 'infant_price',
						'type'      => 'text',
						'desc'      => __( 'Price per infant. Input number only. No decimal or thousand separator or any other character', 'traveler' ),
						'std'       => 0,
						'condition' => 'hide_infant_in_booking_form:is(off)',
						'is_number' => true,
					],
					[
						'label' => __( 'Disable adult booking', 'traveler' ),
						'id'    => 'hide_adult_in_booking_form',
						'type'  => 'on-off',
						'desc'  => __( 'Hide No of adult in booking form', 'traveler' ),
						'std'   => 'off',
					],
					[
						'label' => __( 'Disable children booking', 'traveler' ),
						'id'    => 'hide_children_in_booking_form',
						'type'  => 'on-off',
						'desc'  => __( 'Hide No of child in booking form', 'traveler' ),
						'std'   => 'off',
					],
					[
						'label' => __( 'Disable infant booking', 'traveler' ),
						'id'    => 'hide_infant_in_booking_form',
						'type'  => 'on-off',
						'desc'  => __( 'Hide No of infant in booking form', 'traveler' ),
						'std'   => 'off',
					],
					[
						'label' => esc_html__( 'Disable "Adult Name Required"', 'traveler' ),
						'type'  => 'on-off',
						'std'   => 'off',
						'id'    => 'disable_adult_name',
					],
					[
						'label' => esc_html__( 'Disable "Children Name Required"', 'traveler' ),
						'type'  => 'on-off',
						'std'   => 'off',
						'id'    => 'disable_children_name',
					],
					[
						'label' => esc_html__( 'Disable "Infant Name Required"', 'traveler' ),
						'type'  => 'on-off',
						'std'   => 'off',
						'id'    => 'disable_infant_name',
					],
					[
						'label'    => __( 'Extra price', 'traveler' ),
						'id'       => 'extra_price',
						'type'     => 'list-item',
						'settings' => [
							[
								'id'    => 'extra_name',
								'type'  => 'text',
								'std'   => 'extra_',
								'label' => __( 'Name of item', 'traveler' ),
							],
							[
								'id'    => 'extra_max_number',
								'type'  => 'text',
								'std'   => '',
								'label' => __( 'Max of number', 'traveler' ),
								'is_number' => true,
							],
							[
								'id'    => 'extra_price',
								'type'  => 'text',
								'std'   => '',
								'label' => __( 'Price', 'traveler' ),
								'desc'  => __( 'per 1 item', 'traveler' ),
								'is_number' => true,
							],
							[
								'id'    => 'extra_required',
								'type'  => 'on-off',
								'std'   => 'off',
								'label' => __( 'Required Extra', 'traveler' ),
								'desc'  => __( '', 'traveler' ),
							],
						],

					],
					[
						'label' => __( 'Discount', 'traveler' ),
						'desc'  => __( 'Discount', 'traveler' ),
						'id'    => 'discount',
						'type'  => 'text',
						'std'   => 0,
						'is_number' => true,
					],
					[
						'label'   => __( 'Type of discount', 'traveler' ),
						'id'      => 'discount_type',
						'type'    => 'select',
						'choices' => [
							[
								'label' => __( 'Percent', 'traveler' ),
								'value' => 'percent',
							],
							[
								'label' => __( 'Amount', 'traveler' ),
								'value' => 'amount',
							],
						],
					],
					[
						'label' => __( 'Sale schedule', 'traveler' ),
						'id'    => 'is_sale_schedule',
						'type'  => 'on-off',
						'std'   => 'off',
					],
					[
						'label'     => __( 'Sale price date from', 'traveler' ),
						'desc'      => __( 'Sale price date from', 'traveler' ),
						'id'        => 'sale_price_from',
						'type'      => 'date-picker',
						'condition' => 'is_sale_schedule:is(on)',
					],

					[
						'label'     => __( 'Sale price date to', 'traveler' ),
						'desc'      => __( 'Sale price date to', 'traveler' ),
						'id'        => 'sale_price_to',
						'type'      => 'date-picker',
						'condition' => 'is_sale_schedule:is(on)',
					],

					// [
					// 'label' => __( 'Best price guarantee', 'traveler' ),
					// 'id'    => 'best-price-guarantee',
					// 'type'  => 'on-off',
					// 'std'   => 'off'
					// ],
					// [
					// 'label'     => __( 'Best price guarantee text', 'traveler' ),
					// 'id'        => 'best-price-guarantee-text',
					// 'type'      => 'textarea',
					// 'rows'      => '2',
					// 'condition' => 'best-price-guarantee:is(on)',
					// ],
					[
						'label' => __( 'Activity external booking', 'traveler' ),
						'id'    => 'st_activity_external_booking',
						'type'  => 'on-off',
						'std'   => 'off',
					],
					[
						'label'     => __( 'Activity external booking', 'traveler' ),
						'id'        => 'st_activity_external_booking_link',
						'type'      => 'text',
						'std'       => '',
						'condition' => 'st_activity_external_booking:is(on)',
						'desc'      => '<em>' . __( 'Notice: Must be https://...', 'traveler' ) . '</em>',
					],

					[
						'id'      => 'deposit_payment_status',
						'label'   => __( 'Deposit payment options', 'traveler' ),
						'desc'    => __( 'You can select <code>Disallow Deposit</code>, <code>Deposit by percent</code>' ),
						'type'    => 'select',
						'choices' => [
							[
								'value' => '',
								'label' => __( 'Disallow deposit', 'traveler' ),
							],
							[
								'value' => 'percent',
								'label' => __( 'Deposit by percent', 'traveler' ),
							],
							/*
							[
								'value' => 'amount',
								'label' => __( 'Deposit by amount', 'traveler' )
							],*/
						],
					],
					[
						'label'     => __( 'Deposit percent', 'traveler' ),
						'desc'      => __( 'Leave empty for disallow deposit payment', 'traveler' ),
						'id'        => 'deposit_payment_amount',
						'type'      => 'text',
						'condition' => 'deposit_payment_status:not()',
					],

					[
						'label' => __( 'Availability', 'traveler' ),
						'id'    => 'availability_tab',
						'type'  => 'tab',
					],
					[
						'label' => __( 'Activity calendar', 'traveler' ),
						'id'    => 'st_activity_calendar',
						'type'  => 'st_activity_calendar',
					],
					[
						'label' => __( 'Cancel booking', 'traveler' ),
						'id'    => 'st_cancel_booking_tab',
						'type'  => 'tab',
					],
					[
						'label' => __( 'Allow cancelation', 'traveler' ),
						'id'    => 'st_allow_cancel',
						'type'  => 'on-off',
						'std'   => 'off',
					],
					[
						'label'     => __( 'Number of days before the arrival', 'traveler' ),
						'desc'      => __( 'Number of days before the arrival', 'traveler' ),
						'id'        => 'st_cancel_number_days',
						'type'      => 'text',
						'condition' => 'st_allow_cancel:is(on)',
						'is_number' => true,
					],
					[
						'label'        => __( 'Cancellation Fee', 'traveler' ),
						'desc'         => __( 'A percentage of money customers will be deducted if they cancel a reservation', 'traveler' ),
						'id'           => 'st_cancel_percent',
						'type'         => 'numeric-slider',
						'min_max_step' => '0,100,1',
						'condition'    => 'st_allow_cancel:is(on)',
					],
					[
						'label' => __( 'Ical Sysc', 'traveler' ),
						'id'    => 'ical_sys_tab',
						'type'  => 'tab',
					],
					[
						'label' => __( 'Ical URL', 'traveler' ),
						'id'    => 'ical_url',
						'type'  => 'ical',
						'desc'  => __( 'Enter an ical url and click Import button. All data will be updated and shown in the Availability tab', 'traveler' ),
					],
				],
			];
			$data_paypment = STPaymentGateways::get_payment_gateways();
			if ( ! empty( $data_paypment ) and is_array( $data_paypment ) ) {
				$this->metabox[0]['fields'][] = [
					'label' => __( 'Payment', 'traveler' ),
					'id'    => 'payment_detail_tab',
					'type'  => 'tab',
				];
				foreach ( $data_paypment as $k => $v ) {
					$this->metabox[0]['fields'][] = [
						'label' => $v->get_name(),
						'id'    => 'is_meta_payment_gateway_' . $k,
						'type'  => 'on-off',
						'desc'  => $v->get_name(),
						'std'   => 'on',
					];
				}
			}
			$custom_field = st()->get_option( 'st_activity_unlimited_custom_field' );
			if ( ! empty( $custom_field ) and is_array( $custom_field ) ) {
				$this->metabox[0]['fields'][] = [
					'label' => __( 'Custom fields', 'traveler' ),
					'id'    => 'custom_field_tab',
					'type'  => 'tab',
				];

				foreach ( $custom_field as $k => $v ) {
					$key                          = str_ireplace( '-', '_', 'st_custom_' . sanitize_title( $v['title'] ) );
					$this->metabox[0]['fields'][] = [
						'label' => $v['title'],
						'id'    => $key,
						'type'  => $v['type_field'],
						'desc'  => '<input value=\'[st_custom_meta key="' . $key . '"]\' type=text readonly />',
						'std'   => $v['default_field'],
					];
				}
			}

			parent::register_metabox( $this->metabox );
		}

		// from 1.2.4
		static function get_min_price( $post_id = null ) {
			// if disable == 0ff, allow add to array
			$prices    = [ 0 ];
			$post_type = get_post_type( $post_id );
			if ( $post_type == 'st_activity' ) {
				if ( ! $post_id ) {
					$post_id = get_the_ID();
				}
				$prices     = [];
				$off_adult  = get_post_meta( $post_id, 'hide_adult_in_booking_form', true );
				$off_child  = get_post_meta( $post_id, 'hide_children_in_booking_form', true );
				$off_infant = get_post_meta( $post_id, 'hide_infant_in_booking_form', true );
				if ( $off_adult != 'on' && ! empty( get_post_meta( $post_id, 'adult_price', true ) ) ) {
					$prices[] = get_post_meta( $post_id, 'adult_price', true );
				} else {
					$prices[] = 0;
				}
				if ( $off_child != 'on' && ! empty( get_post_meta( $post_id, 'child_price', true ) ) ) {
					$prices[] = get_post_meta( $post_id, 'child_price', true );
					if ( $off_infant != 'on' && ! empty( get_post_meta( $post_id, 'infant_price', true ) ) ) {
						$prices[] = get_post_meta( $post_id, 'infant_price', true );
					}
				} else {
					$prices[] = 0;
				}
				$discount = get_post_meta( $post_id, 'discount', true );
				if ( ! empty( $discount ) ) {
					return min( $prices ) * ( 100 - $discount ) / 100;
				}

				return min( $prices );
			}
		}

		// from 1.2.4
		static function meta_update_min_price( $post_id ) {
			$post_type = get_post_type( $post_id );
			if ( $post_type == 'st_activity' ) {
				$min_price = self::get_min_price( $post_id );
				update_post_meta( $post_id, 'min_price', $min_price );
			}
		}

		function meta_update_sale_price( $post_id ) {
			if ( wp_is_post_revision( $post_id ) ) {
				return;
			}
			$post_type = get_post_type( $post_id );
			if ( $post_type == 'st_activity' ) {
				$sale_price       = (float) get_post_meta( $post_id, 'price', true );
				$discount         = get_post_meta( $post_id, 'discount', true );
				$is_sale_schedule = get_post_meta( $post_id, 'is_sale_schedule', true );
				if ( $is_sale_schedule == 'on' ) {
					$sale_from = get_post_meta( $post_id, 'sale_price_from', true );
					$sale_to   = get_post_meta( $post_id, 'sale_price_to', true );
					if ( $sale_from and $sale_from ) {

						$today     = date( 'Y-m-d' );
						$sale_from = date( 'Y-m-d', strtotime( $sale_from ) );
						$sale_to   = date( 'Y-m-d', strtotime( $sale_to ) );
						if ( ( $today >= $sale_from ) && ( $today <= $sale_to ) ) {

						} else {

							$discount = 0;
						}
					} else {
						$discount = 0;
					}
				}
				if ( $discount ) {
					$sale_price = $sale_price - ( $sale_price / 100 ) * $discount;
				}
				update_post_meta( $post_id, 'sale_price', $sale_price );
			}
		}

		function _resend_mail() {
			$order_item = isset( $_GET['order_item_id'] ) ? $_GET['order_item_id'] : false;

			$test = isset( $_GET['test'] ) ? $_GET['test'] : false;
			if ( $order_item ) {

				$order = $order_item;

				if ( $test ) {
					global $order_id;
					$order_id                = $order_item;
					$id_page_email_for_admin = st()->get_option( 'email_for_admin', '' );
					$content                 = ! empty( get_post( $id_page_email_for_admin ) ) ? wp_kses_post( get_post( $id_page_email_for_admin )->post_content ) : '';
					$email                   = '';
					$email                  .= TravelHelper::_get_template_email( $email, $content );
					echo( $email );
					die;
				}

				if ( $order ) {
					STCart::send_mail_after_booking( $order );
				}
			}

			wp_safe_redirect( self::$booking_page . '&send_mail=success' );
		}

		static function st_room_select_ajax() {
			extract( wp_parse_args( $_GET, [
				'room_parent' => '',
				'post_type'   => '',
				'q'           => '',
			] ) );

			query_posts( [
				'post_type'      => $post_type,
				'posts_per_page' => 10,
				's'              => $q,
				'meta_key'       => 'room_parent',
				'meta_value'     => $room_parent,
			] );

			$r = [
				'items' => [],
			];
			while ( have_posts() ) {
				the_post();
				$r['items'][] = [
					'id'          => get_the_ID(),
					'name'        => get_the_title(),
					'description' => '',
				];
			}

			wp_reset_query();

			echo json_encode( $r );
			die;
		}

		static function add_edit_scripts() {
			wp_enqueue_script( 'activity-booking', get_template_directory_uri() . '/js/admin/activity-booking.js', [ 'jquery', 'jquery-ui-datepicker' ], null, true );
			wp_enqueue_style( 'jjquery-ui.theme.min.css', get_template_directory_uri() . '/css/admin/jquery-ui.min.css' );
		}

		static function is_booking_page() {
			if ( is_admin()
				and isset( $_GET['post_type'] )
				and $_GET['post_type'] == 'st_activity'
				and isset( $_GET['page'] )
				and $_GET['page'] = 'st_activity_booking'
			) {
				return true;
			}

			return false;
		}

		function add_menu_page() {
			// Add booking page
			add_submenu_page( 'edit.php?post_type=st_activity', __( 'Activity Booking', 'traveler' ), __( 'Activity Booking', 'traveler' ), 'manage_options', 'st_activity_booking', [ $this, '__activity_booking_page' ] );
		}

		function __activity_booking_page() {
			$section = isset( $_GET['section'] ) ? $_GET['section'] : false;

			if ( $section ) {
				switch ( $section ) {
					case 'edit_order_item':
						$this->edit_order_item();
						break;
				}
			} else {

				$action = isset( $_POST['st_action'] ) ? $_POST['st_action'] : false;
				switch ( $action ) {
					case 'delete':
						$this->_delete_items();
						break;
				}
				echo balanceTags( $this->load_view( 'activity/booking_index', false ) );
			}
		}

		function add_booking() {
			echo balanceTags( $this->load_view( 'activity/booking_edit', false, [ 'page_title' => __( 'Add new Activity Booking', 'traveler' ) ] ) );
		}

		function _delete_items() {
			if ( empty( $_POST ) or ! check_admin_referer( 'shb_action', 'shb_field' ) ) {
				// process form data, e.g. update fields
				return;
			}
			$ids = isset( $_POST['post'] ) ? $_POST['post'] : [];
			if ( ! empty( $ids ) ) {
				foreach ( $ids as $id ) {
					wp_delete_post( $id, true );
				}
			}

			STAdmin::set_message( __( 'Delete item(s) success', 'traveler' ), 'updated' );
		}

		function edit_order_item() {
			$item_id = isset( $_GET['order_item_id'] ) ? $_GET['order_item_id'] : false;
			if ( ! $item_id or get_post_type( $item_id ) != 'st_order' ) {
				return false;
			}
			// if ( isset( $_POST[ 'submit' ] ) and $_POST[ 'submit' ] ) $this->_save_booking( $item_id );

			echo balanceTags( $this->load_view( 'activity/booking_edit' ) );
		}

		function _save_booking( $order_id ) {
			if ( ! check_admin_referer( 'shb_action', 'shb_field' ) ) {
				die( 'shb_action' );
			}
			if ( $this->_check_validate() ) {

				$check_out_field = STCart::get_checkout_fields();

				if ( ! empty( $check_out_field ) ) {
					foreach ( $check_out_field as $field_name => $field_desc ) {
						if ( $field_name != 'st_note' ) {
							update_post_meta( $order_id, $field_name, STInput::post( $field_name ) );
						}
					}
				}

				$item_data = [
					'status' => $_POST['status'],
				];

				foreach ( $item_data as $key => $value ) {
					update_post_meta( $order_id, $key, $value );
				}

				if ( TravelHelper::checkTableDuplicate( 'st_activity' ) ) {
					global $wpdb;

					$table                       = $wpdb->prefix . 'st_order_item_meta';
					$table_activity_availability = $wpdb->prefix . 'st_activity_availability';

					$booked             = ( $_POST['adult_number'] + $_POST['child_number'] + $_POST['infant_number'] );
					$post_id            = st_get_order_by_order_item_id( $order_id )['origin_id'];
					$status_order       = st_get_order_by_order_item_id( $order_id )['status'];
					$check_in_timestamp = st_get_order_by_order_item_id( $order_id )['check_in_timestamp'];

					if ( $status_order != 'complete' && $_POST['status'] == 'complete' ) {
						AvailabilityHelper::syncAvailabilityAfterCompleted( $order_id );
					} elseif ( $status_order == 'complete' && $_POST[ 'status' ] != 'complete' ) {
                        $sql = $wpdb->prepare( "UPDATE {$table_activity_availability} SET number_booked = GREATEST( 0, IFNULL(number_booked, 0) - %d ) WHERE post_id = %d AND check_in = %s", $booked, $post_id, $check_in_timestamp );
                        $wpdb->query($sql);
                    }

					$data  = [
						'status'               => $_POST['status'],
						'cancel_refund_status' => 'complete',
					];

					$where = [
						'order_item_id' => $order_id,
					];
					$wpdb->update( $table, $data, $where );
				}

				STCart::send_mail_after_booking( $order_id, true );

				do_action( 'st_admin_edit_booking_status', $item_data['status'], $order_id );

				wp_safe_redirect( self::$booking_page );
			}
		}

		function _check_validate() {
			$st_first_name = STInput::request( 'st_first_name', '' );
			if ( empty( $st_first_name ) ) {
				STAdmin::set_message( __( 'The firstname field is not empty.', 'traveler' ), 'danger' );

				return false;
			}

			$st_last_name = STInput::request( 'st_last_name', '' );
			if ( empty( $st_last_name ) ) {
				STAdmin::set_message( __( 'The lastname field is not empty.', 'traveler' ), 'danger' );

				return false;
			}

			$st_email = STInput::request( 'st_email', '' );
			if ( empty( $st_email ) ) {
				STAdmin::set_message( __( 'The email field is not empty.', 'traveler' ), 'danger' );

				return false;
			}

			$st_phone = STInput::request( 'st_phone', '' );
			if ( empty( $st_phone ) ) {
				STAdmin::set_message( __( 'The phone field is not empty.', 'traveler' ), 'danger' );

				return false;
			}

			return true;
		}

		function is_able_edit() {
			$item_id = isset( $_GET['order_item_id'] ) ? $_GET['order_item_id'] : false;
			if ( ! $item_id or get_post_type( $item_id ) != 'st_order' ) {
				wp_safe_redirect( self::$booking_page );
				die;
			}

			return true;
		}

		function add_col_header( $defaults ) {
			$this->array_splice_assoc( $defaults, 2, 0, [
				'activity_type'   => __( 'Activity Type', 'traveler' ),
				'price'           => __( 'Price', 'traveler' ),
				'activity_layout' => __( 'Layout', 'traveler' ),
			] );

			return $defaults;
		}

		function array_splice_assoc( &$input, $offset, $length = 0, $replacement = [] ) {
			$tail      = array_splice( $input, $offset );
			$extracted = array_splice( $tail, 0, $length );
			$input    += $replacement + $tail;

			return $extracted;
		}

		function add_col_content( $column_name, $post_ID ) {
			if ( $column_name == 'activity_layout' ) {
				// show content of 'directors_name' column
				$layout      = get_post_meta( $post_ID, 'st_custom_layout_new', true );
				$data_layout = self::data_layout();

				echo esc_html( $data_layout[ $layout ] );
			}
			if ( $column_name == 'activity_type' ) {
				$type = get_post_meta( $post_ID, 'type_activity', true );
				switch ( $type ) {
					case 'daily_activity':
						echo __( 'Daily Activity', 'traveler' );
						break;
					case 'specific_date':
						echo __( 'Specific Date', 'traveler' );
						break;
					default:
						echo 'none';
						break;
				}
			}
			if ( $column_name == 'price' ) {
				$discount          = get_post_meta( $post_ID, 'discount', true );
				$price_adult       = get_post_meta( $post_ID, 'adult_price', true );
				$price_child       = get_post_meta( $post_ID, 'child_price', true );
				$discount_type     = get_post_meta( $post_ID, 'discount_type', true );
				$str_discount_type = ( $discount_type == 'percent' ) ? '%' : '';
				if ( ! empty( $discount ) ) {
					$is_sale_schedule = get_post_meta( $post_ID, 'is_sale_schedule', true );
					$sale_adult       = ! empty( $price_adult ) ? $price_adult - $price_adult * ( $discount / 100 ) : 0;
					$sale_child       = ! empty( $price_child ) ? $price_child - $price_child * ( $discount / 100 ) : 0;
					if ( isset( $discount_type ) && $discount_type == 'amount' ) {
						$sale_adult = ! empty( $price_adult ) ? $price_adult - $discount : 0;
						$sale_child = ! empty( $price_child ) ? $price_child - $discount : 0;
					}
					if ( isset( $sale_adult ) && intval( $sale_adult ) < 0 ) {
						$sale_adult = 0;
					}
					if ( isset( $sale_child ) && intval( $sale_child ) < 0 ) {
						$sale_child = 0;
					}
					if ( $is_sale_schedule == 'on' ) {
						$sale_from = get_post_meta( $post_ID, 'sale_price_from', true );
						$sale_from = mysql2date( 'd/m/Y', $sale_from );
						$sale_to   = get_post_meta( $post_ID, 'sale_price_to', true );
						$sale_to   = mysql2date( 'd/m/Y', $sale_to );
						echo '<span> ' . __( 'Adult Price', 'traveler' ) . ': ' . TravelHelper::format_money( $price_adult ) . '</span> <i class="fa fa-arrow-right"></i> <strong>' . TravelHelper::format_money( $sale_adult ) . '</strong><br>';
						echo '<span>' . __( 'Child Price', 'traveler' ) . ': ' . TravelHelper::format_money( $price_child ) . '</span> <i class="fa fa-arrow-right"></i> <strong>' . TravelHelper::format_money( $sale_child ) . '</strong><br>';
						if ( isset( $discount_type ) && $discount_type == 'amount' ) {
							echo '<span>' . __( 'Discount rate', 'traveler' ) . ' : ' . TravelHelper::format_money( $discount ) . '</span><br>';
						} else {
							echo '<span>' . __( 'Discount rate', 'traveler' ) . ' : ' . $discount . $str_discount_type . '</span><br>';
						}
						echo '<span> ' . $sale_from . ' <i class="fa fa-arrow-right"></i> ' . $sale_to . '</span>';
					} else {
						echo '<span> ' . __( 'Adult Price', 'traveler' ) . ': ' . TravelHelper::format_money( $price_adult ) . '</span> <i class="fa fa-arrow-right"></i> <strong>' . TravelHelper::format_money( $sale_adult ) . '</strong><br>';
						echo '<span>' . __( 'Child Price', 'traveler' ) . ': ' . TravelHelper::format_money( $price_child ) . '</span> <i class="fa fa-arrow-right"></i> <strong>' . TravelHelper::format_money( $sale_child ) . '</strong><br>';
						if ( isset( $discount_type ) && $discount_type == 'amount' ) {
							echo '<span>' . __( 'Discount rate', 'traveler' ) . ' : ' . TravelHelper::format_money( $discount ) . '</span><br>';
						} else {
							echo '<span>' . __( 'Discount rate', 'traveler' ) . ' : ' . $discount . $str_discount_type . '</span><br>';
						}
					}
				} else {
					echo '<span> ' . __( 'Adult Price', 'traveler' ) . ': ' . TravelHelper::format_money( $price_adult ) . '</span><br>';
					echo '<span>' . __( 'Child Price', 'traveler' ) . ': ' . TravelHelper::format_money( $price_child ) . '</span>';
				}
			}
		}

		public static function data_layout() {
			return [
				'1' => esc_html__('Layout 1', 'traveler'),
				'2' => esc_html__('Layout 2', 'traveler'),
				'3' => esc_html__('Layout 3', 'traveler'),
				'4' => esc_html__('Layout modern 1', 'traveler'),
				'5' => esc_html__('Layout modern 2', 'traveler'),
			];
		}

		public function _upgradeColumnDiscountTypeActivityTable() {
			$updated = get_option( '_upgrade_column_discount_type_activity_table', false );
			if ( ! $updated ) {
				global $wpdb;
				$table = $wpdb->prefix . $this->post_type;
				$sql   = "Update {$table} as t inner join {$wpdb->postmeta} as m on (t.post_id = m.post_id and m.meta_key = 'discount_type') set t.discount_type = m.meta_value";
				$wpdb->query( $sql );
				update_option( '_upgrade_column_discount_type_activity_table', true );
			}
		}

		public static function __cronjob_update_min_price( $offset, $limit = 2 ) {
			global $wpdb;
			$list_activity = new WP_Query([
				'posts_per_page' => $limit,
				'post_type'      => 'st_activity',
				'offset'         => $offset,
			]);

			if ( $list_activity->have_posts() ) {
				while ( $list_activity->have_posts() ) {
					$list_activity->the_post();
					$activity_id = get_the_ID();
					self::meta_update_min_price( $activity_id );
					self::_update_duplicate_data( $activity_id, [] );
				}
			}

			wp_reset_postdata();
		}

		static function inst() {
			if ( ! self::$_inst ) {
				self::$_inst = new self();
			}

			return self::$_inst;
		}

	}

	STAdminActivity::inst();
}
