<?php
/***** ADD WPCSR LOCATION *****/
add_action( 'wp_ajax_ajax_save_wpcsr_location', 'ajax_save_wpcsr_location' );
add_action( 'wp_ajax_nopriv_ajax_save_wpcsr_location', 'ajax_save_wpcsr_location' );
function ajax_save_wpcsr_location(){
	global $wpdb;
	$location 	= sanitize_text_field( $_REQUEST['location'] );
	$slug 		= sanitize_text_field( $_REQUEST['slug'] );
	$result = $wpdb->insert(
		$wpdb->prefix.WPCSR_DB_LOCATION,
		array(
			'location' => $location,
			'location_slug' => $slug,
		),
		array(
			'%s',
			'%s',
		)
	);
	if( $result ){
		$result = $wpdb->insert_id;
	}
	echo $result;
	wp_die();
}
/***** ADD WPCSR ZONE *****/
add_action( 'wp_ajax_ajax_save_wpcsr_zone', 'ajax_save_wpcsr_zone' );
add_action( 'wp_ajax_nopriv_ajax_save_wpcsr_zone', 'ajax_save_wpcsr_zone' );
function ajax_save_wpcsr_zone(){
	global $wpdb;
	$zone 			= sanitize_text_field( $_REQUEST['zone'] );
	$from 			= sanitize_text_field( $_REQUEST['from'] );
	$zone_location 	= sanitize_text_field( $_REQUEST['zone_location'] );
	$zone_location 	= array_filter( explode(',', maybe_unserialize( $zone_location  ) ) );
	$location 		= maybe_serialize( array_map('trim', $zone_location ) );
	$rate = $_REQUEST['rate'];
	$result = $wpdb->insert(
		$wpdb->prefix.WPCSR_DB_ZONE,
		array(
			'label' => $zone,
			'origin' => $from,
			'zone_location' => $location,
		),
		array(
			'%s',
			'%d',
			'%s',
		)
	);
	echo $result;
	die();
}
/***** ADD WPCSR SHIPMENT RATE *****/
add_action( 'wp_ajax_ajax_save_wpcsr', 'ajax_save_wpcsr' );
add_action( 'wp_ajax_nopriv_ajax_save_wpcsr', 'ajax_save_wpcsr' );
function ajax_save_wpcsr(){
	global $wpdb;
	$rate_data 			= $_REQUEST['rate_data'];
	$rate_data_value 	= array();
	$rate_data_format 	= array();
	foreach( $rate_data as $data ){
		$meta_key 					= $data['name'];
		if( 'user_group' === $meta_key && empty( $data['value']  ) ){
			$meta_value 			= serialize( array() );
		}else{
			$meta_value 			= is_array( $data['value'] ) ? serialize( $data['value'] ) : $data['value'];
		}
		$rate_data_value[$meta_key] = $meta_value;
		$rate_data_format[] 		= '%s';
	}
	$result = $wpdb->insert(
		$wpdb->prefix.WPCSR_DB_RATE,
		$rate_data_value,
		$rate_data_format
	);
	die();
}
/***** ADD WPCSR DELIVERY *****/
add_action( 'wp_ajax_save_wpcsr_delivery', 'save_wpcsr_delivery' );
add_action( 'wp_ajax_nopriv_save_wpcsr_delivery', 'save_wpcsr_delivery' );
function save_wpcsr_delivery(){
	global $wpdb;
	$title 			= sanitize_text_field( $_REQUEST['title'] );
	$description 	= sanitize_text_field( $_REQUEST['description'] );
	$result = $wpdb->insert(
		$wpdb->prefix.WPCSR_DB_DELIVERY,
		array(
			'title' => $title,
			'description' => $description,
		),
		array(
			'%s',
			'%s',
		)
	);
	if( $result ){
		$result = $wpdb->insert_id;
	}
	echo $result;
	wp_die();
}
/***** DELETE WPCSR DATA *****/ 
add_action( 'wp_ajax_ajax_delete_wpcsr', 'ajax_delete_wpcsr' );
add_action( 'wp_ajax_nopriv_ajax_delete_wpcsr', 'ajax_delete_wpcsr' );
function ajax_delete_wpcsr(){
	global $wpdb;
	$id 		= $_REQUEST['id'];
	$section 	= $_REQUEST['section'];
	if($section == 'location'){
		$wpdb->delete( $wpdb->prefix . WPCSR_DB_LOCATION, array( 'location_id' => $id ) );
	}elseif($section == 'zone'){
		$wpdb->delete( $wpdb->prefix . WPCSR_DB_ZONE, array( 'zone_id' => $id ) );
	}elseif($section == 'rate'){
		$wpdb->delete( $wpdb->prefix . WPCSR_DB_RATE, array( 'id' => $id ) );
	}elseif($section == 'pickup-rate'){
		$wpdb->delete( $wpdb->prefix . WPCSR_DB_PICKUP, array( 'id' => $id ) );
	}elseif($section == 'delivery'){
		$wpdb->delete( $wpdb->prefix . WPCSR_DB_DELIVERY, array( 'id' => $id ) );
	}elseif($section == 'user-group'){
		$wpdb->delete( $wpdb->prefix . WPCSR_DB_USER_GROUP, array( 'user_group_id' => $id ) );
	}
	die();
}
/***** BULK WPCSR DATA *****/
add_action( 'wp_ajax_ajax_rate_bulk', 'ajax_rate_bulk_callback' );
add_action( 'wp_ajax_nopriv_ajax_rate_bulk', 'ajax_rate_bulk_callback' );
function ajax_rate_bulk_callback(){
	global $wpdb;
	$ids 		= $_REQUEST['id'];
	$section 	= $_REQUEST['section'];
	$table_name = '';
	$id_label = 'id';
	if($section == 'location'){
		$table_name = $wpdb->prefix .WPCSR_DB_LOCATION;
		$id_label = 'location_id';
	}elseif($section == 'zone'){
		$table_name = $wpdb->prefix .WPCSR_DB_ZONE;
		$id_label = 'zone_id';
	}elseif($section == 'user-group'){
		$table_name = $wpdb->prefix .WPCSR_DB_USER_GROUP;
		$id_label = 'user_group_id';
	}elseif($section == 'rate'){
		$table_name = $wpdb->prefix .WPCSR_DB_RATE;
	}elseif($section == 'pickup-rate'){
		$table_name = $wpdb->prefix .WPCSR_DB_PICKUP;
	}elseif($section == 'delivery'){
		$table_name = $wpdb->prefix .WPCSR_DB_DELIVERY;
	}
	foreach ($ids as $array => $id) {
		$wpdb->delete( $table_name, array( $id_label => $id ) );
	}
}
/***** UPDATE WPCSR LOCATION *****/
add_action( 'wp_ajax_ajax_edit_wpcsr', 'ajax_edit_wpcsr' );
add_action( 'wp_ajax_nopriv_ajax_edit_wpcsr', 'ajax_edit_wpcsr' );
function ajax_edit_wpcsr(){
	global $wpdb;
	$id 	= $_REQUEST['id'];
	$name 	= sanitize_text_field( $_REQUEST['location'] );
	$slug 	= sanitize_text_field( $_REQUEST['slug'] );
	$wpdb->update(
				$wpdb->prefix . WPCSR_DB_LOCATION,
				array(
					'location' => $name,
					'location_slug' => $slug,
				),
				array( 'location_id' => $id ),
				array(
					'%s',
					'%s',
				),
				array( '%d' )
			);
	die();
}
/***** UPDATE WPCSR ZONE *****/
add_action( 'wp_ajax_ajax_update_wpcsr_zone', 'ajax_update_wpcsr_zone' );
add_action( 'wp_ajax_nopriv_ajax_update_wpcsr_zone', 'ajax_update_wpcsr_zone' );
function ajax_update_wpcsr_zone(){
	global $wpdb;
	$id 			= sanitize_text_field( $_REQUEST['id'] );
	$zone 			= sanitize_text_field( $_REQUEST['zone'] );
	$from 			= sanitize_text_field( $_REQUEST['from'] );
	$zone_location 	= sanitize_text_field( $_REQUEST['zone_location'] );
	$zone_location 	= array_filter( explode(',', maybe_unserialize( $zone_location  ) ) );
	$location 		= maybe_serialize( array_map('trim', $zone_location ) );
	$rate 			= $_REQUEST['rate'];
	$result  		= $wpdb->update(
				$wpdb->prefix . WPCSR_DB_ZONE,
				array(
					'label' 	=> $zone,
					'origin' 	=> $from,
					'zone_location' => $location,
				),
				array( 'zone_id' => $id ),
				array(
					'%s',
					'%s',
					'%s',
				),
				array( '%d' )
			);
	wp_die();
}
/***** UPDATE WPCSR USER GROUP *****/
add_action( 'wp_ajax_ajax_update_user_group', 'ajax_update_user_group' );
add_action( 'wp_ajax_nopriv_ajax_update_user_group', 'ajax_update_user_group' );
function ajax_update_user_group(){
	global $wpdb;
	$id 			= sanitize_text_field( $_REQUEST['id'] );
	$label 			= sanitize_text_field( $_REQUEST['label'] );
	$description	= sanitize_text_field( $_REQUEST['description'] );
	$users			= serialize( $_REQUEST['users'] );
	$result = $wpdb->update(
				$wpdb->prefix . WPCSR_DB_USER_GROUP,
				array(
					'label' 	=> $label,
					'description' 	=> $description,
					'users' => $users,
				),
				array( 'user_group_id' => $id ),
				array(
					'%s',
					'%s',
					'%s',
				),
				array( '%d' )
			);
	wp_die();
}
/***** UPDATE WPCSR LOCATION *****/
add_action( 'wp_ajax_ajax_update_wpcsr_location', 'ajax_update_wpcsr_location' );
add_action( 'wp_ajax_nopriv_ajax_update_wpcsr_location', 'ajax_update_wpcsr_location' );
function ajax_update_wpcsr_location(){
	global $wpdb;
	$id 			= $_REQUEST['id'];
	$location 		= sanitize_text_field( $_REQUEST['location'] );
	$location_slug 	= sanitize_text_field( $_REQUEST['slug'] );
	$result  	= $wpdb->update(
				$wpdb->prefix . WPCSR_DB_LOCATION,
				array(
					'location' 	=> $location,
					'location_slug' => $location_slug,
				),
				array( 'location_id' => $id ),
				array(
					'%s',
					'%s',
				),
				array( '%d' )
			);
	wp_die();
}
/***** UPDATE WPCSR SHIPMENT RATE *****/
add_action( 'wp_ajax_wpcsr_update_rate', 'ajax_update_wpcsr_rate' );
add_action( 'wp_ajax_nopriv_wpcsr_update_rate', 'ajax_update_wpcsr_rate' );
function ajax_update_wpcsr_rate(){
	global $wpdb;
	$id 				= $_POST['id'];
	$formData 			= $_POST['formData'];
	$rate_fields  		= wpcsr_rate_fields();
	$data_holder 		= array();
	$formatted_data 	= array();
	$array_data 		= array();
	foreach ($formData as $data ) {
		$column_name 	= $data['name'];
		$column_value 	= $data['value'];
		$data_type 		= $rate_fields[$column_name]['data_type'];
		$field_type 	= $rate_fields[$column_name]['field_type'];
		if( !array_key_exists( $column_name, $rate_fields ) ){
			continue;
		}
		if( $field_type == 'checkbox' || $data_type == 'array' ){
			$array_data[ $column_name ][] = $column_value;
			continue;
		}

		if( $data_type == 'float' ){
			$data_holder[] = '%f';
		}elseif( $data_type == 'int' ){
			$data_holder[] = '%d';
		}else{
			$data_holder[] = '%s';
		}

		$formatted_data[ $column_name ] = $column_value;
	}
	if( !empty( $array_data ) ){
		foreach ($array_data as $key => $value) {
			$data_type = $rate_fields[$key]['data_type'];
			if( $data_type == 'array'  ){
				$formatted_data[$key] = maybe_serialize( $value );
			}else{
				$formatted_data[$key] = implode(',', $value);
			}
			if( $data_type == 'float' ){
				$data_holder[] = '%f';
			}elseif( $data_type == 'int' ){
				$data_holder[] = '%d';
			}else{
				$data_holder[] = '%s';
			}
		}
	}
	// if user user_group is empty - set value to empty array
	if( !array_key_exists('user_group', $formatted_data) ){
		$formatted_data['user_group'] = maybe_serialize( array() );
	}
	// if user piece_type is empty - set value to empty string
	if( !array_key_exists('piece_type', $formatted_data) ){
		$formatted_data['piece_type'] = '';
	}
	$wpdb->update(
		$wpdb->prefix.WPCSR_DB_RATE,
		$formatted_data,
		array( 'id' => $id ),
		$data_holder,
		array( '%d' )
	);
	wp_die();
}
/***** UPDATE WPCSR DELIVERY TYPE *****/
add_action( 'wp_ajax_wpcsr_update_delivery', 'ajax_update_wpcsr_update_delivery' );
add_action( 'wp_ajax_nopriv_wpcsr_update_delivery', 'ajax_update_wpcsr_update_delivery' );
function ajax_update_wpcsr_update_delivery(){
	global $wpdb;
	$id 		= (int)$_POST['id'];
	$title 		= sanitize_text_field( $_POST['title'] );
	$description 	= sanitize_text_field( $_POST['description'] );
	$wpdb->update(
		$wpdb->prefix . WPCSR_DB_DELIVERY,
		array(
			'title' => $title,
			'description' => $description,
		),
		array( 'id' => $id ),
		array(
			'%s',
			'%s'
		)
	);
	wp_die();
}
/***** WPCSR LOCATION MODAL *****/
add_action( 'wp_ajax_get_location_data', 'location_data_ajax_get_wpcsr' );
add_action( 'wp_ajax_nopriv_get_location_data', 'location_data_ajax_get_wpcsr' );
function location_data_ajax_get_wpcsr(){
	global $wpdb;
	$id 	= $_REQUEST['id'];
	$location 	= get_wpcsr_location_by_id( $id );
	ob_start();
	?>
	<table class="form-table wpcsr_update_table">
		<tr>
			<td><label><?php esc_html_e('Location Name', 'wpcargo-shipment-rate' ); ?></label></td>
			<td><input type="text" name="wpcsr_location" id="update_location" class="wpcsr_detail wpcsr_location" value="<?php echo $location->location; ?>" required="required" /></td>
		</tr>
		<tr>
			<td><label><?php esc_html_e('Slug', 'wpcargo-shipment-rate' ); ?></label></td>
			<td><input type="text" name="wpcsr_location_slug" id="update_location_slug" class="wpcsr_detail wpcsr_location_slug" value="<?php echo $location->location_slug; ?>" readonly="readonly" /></td>
		</tr>
		<tr>
			<td></td>
			<td>
				<input type="hidden" id="wpcsr_location_id" name="update_location_id" value="<?php echo $id; ?>">
				<input type="submit" id="wpcsr_edit_location" class="wpcsr_btn wpcsr_submit" value="<?php esc_html_e('Update Location', 'wpcargo-shipment-rate' ); ?>" />
			</td>
		</tr>
	</table>
	<?php
	$output = ob_get_clean();
	echo $output;
	wp_die();
}
/***** WPCSR LOCATION MODAL *****/
add_action( 'wp_ajax_get_delivery_data', 'delivery_data_ajax_get_wpcsr' );
add_action( 'wp_ajax_nopriv_get_delivery_data', 'delivery_data_ajax_get_wpcsr' );
function delivery_data_ajax_get_wpcsr(){
	global $wpdb;
	$id 		= $_REQUEST['id'];
	$delivery 	= get_wpcsr_delivery_by_id( $id );
	ob_start();
	?>
	<table style="width: 100%;" class="form-table wpcsr_update_table">
		<tr>
			<td><label><?php esc_html_e('Title', 'wpcargo-shipment-rate' ); ?></label></td>
			<td>
				<input type="text" id="title" name="title" value="<?php echo $delivery->title; ?>" required="required" />
			</td>
		</tr>
		<tr>
			<td><label><?php esc_html_e('Description', 'wpcargo-shipment-rate' ); ?></label></td>
			<td>
				<textarea id="description" name="description" cols="30" rows="10"><?php echo $delivery->description; ?></textarea>
			</td>
		</tr>
		<tr>
			<td></td>
			<td>
				<input type="hidden" id="wpcsr_delivery_id" name="update_delivery_id" value="<?php echo $delivery->id; ?>">
				<input type="submit" id="wpcsr_edit_location" class="wpcsr_btn wpcsr_submit" value="<?php esc_html_e('Update Delivery', 'wpcargo-shipment-rate' ); ?>" />
			</td>
		</tr>
	</table>
	<?php
	$output = ob_get_clean();
	echo $output;
	wp_die();
}
/***** WPCSR ZONE MODAL *****/
add_action( 'wp_ajax_get_zone_data', 'zone_data_ajax_get_wpcsr' );
add_action( 'wp_ajax_nopriv_get_zone_data', 'zone_data_ajax_get_wpcsr' );
function zone_data_ajax_get_wpcsr(){
	global $wpdb;
	$id 	= $_REQUEST['id'];
	$zone 	= get_wpcsr_zone_by_id( $id );
	$_location = array_filter( maybe_unserialize( $zone->zone_location ) );
	ob_start();
	?>
	<table class="form-table wpcsr_update_table">
		<tr>
			<td><label><?php esc_html_e('Zone Name', 'wpcargo-shipment-rate' ); ?></label></td>
			<td><input type="text" id="update_zone" class="wpcsr_detail wpcsr_zone" name="wpcsr_zone" value="<?php echo $zone->label; ?>" required="required" /></td>
		</tr>
		<tr>
			<td valign="top"><label><?php esc_html_e('From', 'wpcargo-shipment-rate' ); ?></label></td>
			<td>
				<?php if( !empty( get_wpcsr_location() ) ): ?>
					<select id="update_zone_from" class="wpcsr_from wpcsr_zone" name="wpcsr_zone_from">
					<?php foreach(get_wpcsr_location() as $location): ?>
						<option value="<?php echo $location->location_id;?>" <?php echo ($zone->origin==$location->location_id) ? 'selected' : ''; ?> ><?php echo $location->location;?></option>
					<?php endforeach?>
					</select>
				<?php else: ?>
					<?php esc_html_e('No Location found.', 'wpcargo-shipment-rate' ); ?>
				<?php endif; ?>
			</td>
		</tr>
		<tr>
			<td valign="top"><label><?php esc_html_e('Locations', 'wpcargo-shipment-rate' ); ?></label></td>
			<td>
				<?php if( !empty( get_wpcsr_location() ) ): ?>
					<select id="update_zone_location" class="update_zone_location wpcsr_zone" name="update_zone_location" multiple>
					<?php foreach(get_wpcsr_location() as $location): ?>
						<option value="<?php echo $location->location_id;?>" <?php echo ( in_array( $location->location_id, $_location ) ) ? 'selected' : ''; ?> ><?php echo $location->location;?></option>
					<?php endforeach?>
					</select>
				<?php else: ?>
					<?php esc_html_e('No Location found.', 'wpcargo-shipment-rate' ); ?>
				<?php endif; ?>
			</td>
		</tr>
		<tr>
			<td></td>
			<td>
				<input type="hidden" id="wpcsr_zone_id" name="update_zone_id" value="<?php echo $id; ?>">
				<input type="submit" id="wpcsr_edit_zone" class="wpcsr_btn button button-primary button-large wpcsr_submit" value="<?php esc_html_e('Update Zone', 'wpcargo-shipment-rate' ); ?>" />
			</td>
		</tr>
	</table>
	<?php
	$output = ob_get_clean();
	echo $output;
	wp_die();
}
/***** WPCSR USER GROUP MODAL *****/
add_action( 'wp_ajax_get_user_group_data', 'get_user_group_data_callback' );
add_action( 'wp_ajax_nopriv_get_user_group_data', 'get_user_group_data_callback' );
function get_user_group_data_callback(){
	global $wpdb;
	$id 	= $_REQUEST['id'];
	$user_group = get_wpcsr_user_group_by_id( $id );
	$users = $user_group->users ? unserialize( $user_group->users ) : array();
	ob_start();
	?>
	<table class="form-table wpcsr_add_table">
		<tr>
			<td><label><?php esc_html_e( 'Label', 'wpcargo-shipment-rate' ); ?></label></td>
			<td><input type="text" name="wpcsr_ug_label" id="wpcsr_ug_label_edit" class="wpcsr_detail wpcsr_ug_label" value="<?php echo $user_group->label; ?>" /></td>
		</tr>
		<tr>
			<td><label><?php esc_html_e('Description', 'wpcargo-shipment-rate' ); ?></label></td>
			<td><textarea rows="6" name="wpcsr_ug_desc" id="wpcsr_ug_desc_edit" class="wpcsr_detail wpcsr_ug_desc"><?php echo $user_group->description; ?></textarea></td>
		</tr>
		<tr>
			<td><label><?php esc_html_e('Users', 'wpcargo-shipment-rate' ); ?></label></td>
			<td>
				<select name="wpcsr_ug_users_edit" id="wpcsr_ug_users_edit" class="wpcsr_detail wpcsr_ug_users_edit" multiple>
					<?php foreach( wpcfe_get_clients() as $user_id => $user_name ):?>
						<option value="<?php echo $user_id;?>" <?php echo ( in_array( $user_id, $users ) ) ? 'selected' : ''; ?>><?php echo $user_name; ?></option>
					<?php endforeach;?>
				</select>
			</td>
		</tr>
		<tr>
			<td><input type="hidden" id="wpcsr_ug_id_edit" value="<?php echo $id; ?>"></td>
			<td><input type="submit" id="wpcsr_submit" class="wpcsr_ug_btn button button-primary button-large wpcsr_submit" value="<?php esc_html_e('Update User Group', 'wpcargo-shipment-rate' ); ?>" /></td>
		</tr>
	</table>
	<?php
	$output = ob_get_clean();
	echo $output;
	wp_die();
}
/***** WPCSR SHIPMENT RATE MODAL *****/
add_action( 'wp_ajax_get_rate_data', 'rate_data_ajax_get_wpcsr' );
add_action( 'wp_ajax_nopriv_get_rate_data', 'rate_data_ajax_get_wpcsr' );
function rate_data_ajax_get_wpcsr(){
	global $wpdb;
	$id 			= $_REQUEST['id'];
	$rate 			= get_wpcsr_rate_by_id( $id );
	$deliveries 	= get_wpcsr_deliveries();
	$user_groups	= unserialize( $rate->user_group );
	?>
	<table class="form-table wpcsr_update_table">
		<?php foreach( wpcsr_rate_fields() as $wpcsr_rate_fields ): ?>
			<tr>
				<td><label><?php echo $wpcsr_rate_fields['label']; ?></label></td>
				<td>
					<?php
						if( $wpcsr_rate_fields['field_type'] == 'text' ){
							$meta_key = $wpcsr_rate_fields['field_key'];
							$placeholder = "";
							if( $wpcsr_rate_fields['field_key'] == 'logo_url' ){
								$placeholder = "https://mydomain.com/mylogo.png";
							}
							echo sprintf(
								'<input type="text" name="%s" id="wpcsr_update_%s" class="update_wpcsr_detail wpcsr_%s" value="%s" placeholder="%s"  />',
								$wpcsr_rate_fields['field_key'],
								$wpcsr_rate_fields['field_key'],
								$wpcsr_rate_fields['field_key'],
								$rate->$meta_key,
								$placeholder
							);
							if( $wpcsr_rate_fields['field_key'] == 'logo_url' ){
								?>
									<a href="#" class="wpcsr-select-logo button button-small button-secondary" style="margin-top:8px;"><?php _e('Add Logo', 'wpcargo-shipment-rate' ); ?></a>
								<?php
							}
						}elseif( $wpcsr_rate_fields['field_type'] == 'select' ){
							$multiple = ( $wpcsr_rate_fields['field_key'] == 'user_group' ) ? 'multiple' : '';
							$meta_key = $wpcsr_rate_fields['field_key'];
							if( is_serialized( $rate->$meta_key ) ){
								$value = unserialize( $rate->$meta_key );
							}else{
								$value = $rate->$meta_key;
							}
							echo sprintf(
								'<select name="%s" id="wpcsr_update_%s" class="update_wpcsr_detail wpcsr_%s" %s>',
								$wpcsr_rate_fields['field_key'],
								$wpcsr_rate_fields['field_key'],
								$wpcsr_rate_fields['field_key'],
								$multiple
							);
							foreach( $wpcsr_rate_fields['options'] as $data ){
								if( $wpcsr_rate_fields['field_key'] == 'zone_id' ){
									$data_value = $data->zone_id;
									$data_label = $data->label;
								}elseif( $wpcsr_rate_fields['field_key'] == 'delivery_id' ){
									$data_value = $data->id;
									$data_label = $data->title;
								}elseif( $wpcsr_rate_fields['field_key'] == 'user_group' ){
									$data_value = $data->user_group_id;
									$data_label = $data->label;
								}
								$selected = '';
								if( is_array( $value ) ){
									$selected = in_array( $data_value, $value) ? 'selected' : '';
								}else{
									$selected = $data_value == $value ? 'selected' : '';
								}
								echo sprintf(
									'<option value="%s" %s>%s</option>',
									$data_value,
									$selected,
									$data_label
								);
							}
							echo '</select>';
						}elseif( $wpcsr_rate_fields['field_type'] == 'radio' ){
							foreach( $wpcsr_rate_fields['options'] as $value => $label ){
								$meta_key = $wpcsr_rate_fields['field_key'];
								$checked = ( $rate->$meta_key == $value ) ? 'checked' : '';
								echo sprintf(
									'<label><input type="radio" name="%s" value="%s" class="wpcsr_%s" %s>%s</label>',
									$wpcsr_rate_fields['field_key'],
									$value,
									$wpcsr_rate_fields['field_key'],
									$checked,
									$label
								);
							}
						}elseif( $wpcsr_rate_fields['field_type'] == 'checkbox' ){
							$meta_key 			= $wpcsr_rate_fields['field_key'];
							$selected_option 	= array_filter( array_map( 'trim', explode(",", $rate->$meta_key  ) ) );
							foreach( $wpcsr_rate_fields['options'] as $value => $label ){
								$checked = in_array( $label, $selected_option ) ? 'checked' : '' ;
								echo sprintf(
									'<label><input type="checkbox" name="%s" value="%s" class="wpcsr_detail wpcsr_%s" %s>%s</label><br>',
									$wpcsr_rate_fields['field_key'],
									$label,
									$wpcsr_rate_fields['field_key'],
									$checked,
									$label
								);
							}
						}elseif( $wpcsr_rate_fields['field_type'] == 'textarea' ){
							$meta_key = $wpcsr_rate_fields['field_key'];
							echo sprintf(
								'<textarea rows="6" name="%s" id="wpcsr_update_%s" class="update_wpcsr_detail wpcsr_%s" value="">%s</textarea>',
								$wpcsr_rate_fields['field_key'],
								$wpcsr_rate_fields['field_key'],
								$wpcsr_rate_fields['field_key'],
								$rate->$meta_key
							);
						}
					?>
				</td>
			</tr>
		<?php endforeach; ?>
		<tr>
			<td></td>
			<td>
				<input type="hidden" id="rateID" name="rateID" value="<?php echo $rate->id; ?>">
				<input type="submit" id="wpcsr_submit" class="wpcsr_btn wpcsr_submit" value="<?php esc_html_e('Update Shipment Rate', 'wpcargo-shipment-rate' ); ?>" />
			</td>
		</tr>
	</table>
	<?php
	$output = ob_get_clean();
	echo $output;
	wp_die();
}
// Update Pickup rate modal
add_action( 'wp_ajax_get_pickuprate_data', 'pickup_rate_data_ajax_get_wpcsr' );
add_action( 'wp_ajax_nopriv_get_pickuprate_data', 'pickup_rate_data_ajax_get_wpcsr' );
function pickup_rate_data_ajax_get_wpcsr(){
	global $wpdb;
	$id 		= $_REQUEST['id'];
	$warehouses = get_option('wpcsr_warehouses');
    $locations  = get_wpcsr_location();
	$rate 		= get_wpcsr_pickup_rates_by_id($id);
	ob_start();
	?>
	<table class="form-table wpcsr_add_table">
		<tr>
			<th><label for="name"><?php esc_html_e('Pickup Name', 'wpcargo-shipment-rate' ); ?>:</label></th>
			<td>
				<input type="text" id="name" name="name" value="<?php echo $rate->name; ?>" placeholder="<?php esc_html_e('Pickup Name', 'wpcargo-shipment-rate' ); ?>" required="required">
			</td>
		</tr>
		<tr>
			<th><label for="warehouse"><?php esc_html_e('Warehouse', 'wpcargo-shipment-rate' ); ?>:</label></th>
			<td>
				<select id="warehouse" name="warehouse" required="required">
					<option value=""><?php esc_html_e('Select Warehouse', 'wpcargo-shipment-rate' ); ?></option>
					<?php
						foreach ( $warehouses as $warehouse ) {
							?><option value="<?php echo  $warehouse['name']; ?>" <?php selected( $rate->warehouse, $warehouse['name']); ?>><?php echo  $warehouse['name']; ?></option><?php
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<th><label for="pickup_zone"><?php esc_html_e('Pickup Zone', 'wpcargo-shipment-rate' ); ?>:</label></th>
			<td>
				<select id="pickup_zone" name="pickup_zone" required="required">
					<option value=""><?php esc_html_e('Select pickup zone', 'wpcargo-shipment-rate' ); ?></option>
					<?php
						foreach ( $locations as $location ) {
							?><option value="<?php echo  $location->location; ?>" <?php selected( $rate->zone, $location->location ); ?>><?php echo  $location->location; ?></option><?php
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<th><label ><?php esc_html_e('Weight Range', 'wpcargo-shipment-rate' ); ?>:</label></th>
			<td>
				<input type="text" id="min_weight" class="number" name="min_weight" value="<?php echo $rate->min_weight; ?>" placeholder="<?php esc_html_e('Min. Weight', 'wpcargo-shipment-rate' ); ?>" required="required"> - <input type="text" id="max_weight" class="number" name="max_weight" value="<?php echo $rate->max_weight; ?>" placeholder="<?php esc_html_e('Max. Weight', 'wpcargo-shipment-rate' ); ?>" required="required">
			</td>
		</tr>
		<tr>
			<th><label for="price" ><?php esc_html_e('Price', 'wpcargo-shipment-rate' ); ?>:</label></th>
			<td>
				<input type="text" id="price" class="number" name="price" value="<?php echo $rate->price; ?>" placeholder="<?php esc_html_e('Price', 'wpcargo-shipment-rate' ); ?>" required="required">
			</td>
		</tr>
		<tr>
			<th>&nbsp;</th>
			<td> <input type="submit" class="button button-primary button-large" name="submit-pickup-rate" value="<?php esc_html_e('Save Rate', 'wpcargo-shipment-rate' ); ?>"></td>
		</tr>
	</table>
	<input type="hidden" id="pickupID" name="pickupID" value="<?php echo $rate->id; ?>" >
	<?php
	$output = ob_get_clean();
	echo $output;
	wp_die();
}
add_action( 'wp_ajax_wpcsr_update_pickuprate', 'ajax_update_wpcsr_update_pickuprate' );
add_action( 'wp_ajax_nopriv_wpcsr_update_pickuprate', 'ajax_update_wpcsr_update_pickuprate' );
function ajax_update_wpcsr_update_pickuprate(){
	global $wpdb;
	$pickupID 	= (int)sanitize_text_field( $_POST['pickupID'] );
	$pickupName = sanitize_text_field( $_POST['pickupName'] );
	$warehouse 	= sanitize_text_field( $_POST['warehouse'] );
	$pickupZone = sanitize_text_field( $_POST['pickupZone'] );
	$minWeight 	= sanitize_text_field( $_POST['minWeight'] );
	$maxWeight 	= sanitize_text_field( $_POST['maxWeight'] );
	$price 		= sanitize_text_field( $_POST['price'] );
	$update = $wpdb->update(
		$wpdb->prefix . WPCSR_DB_PICKUP,
		array(
			'name' => $pickupName,
			'warehouse' => $warehouse,
			'zone' => $pickupZone,
			'min_weight' => $minWeight,
			'max_weight' => $maxWeight,
			'price' => $price,
		),
		array( 'id' => $pickupID ),
		array( '%s', '%s', '%s', '%f', '%f', '%f' )
	);
	wp_die( $update );
}
// Add new Pickup rate
add_action( 'wp_ajax_nopriv_save_pickup_rate', 'wpcsr_save_pickup_rate_callback' );
add_action( 'wp_ajax_save_pickup_rate', 'wpcsr_save_pickup_rate_callback' );
function wpcsr_save_pickup_rate_callback(){
	global $wpdb;
	$pickup_label 	= sanitize_text_field( $_POST['pickupLabel'] );
	$warehouse 		= sanitize_text_field( $_POST['warehouse'] );
	$location 		= sanitize_text_field( $_POST['location'] );
	$min_weight 	= sanitize_text_field( $_POST['minWeight'] );
	$max_weight 	= sanitize_text_field( $_POST['maxWeight'] );
	$price 			= sanitize_text_field( $_POST['price'] );
	$result = $wpdb->insert(
		$wpdb->prefix.WPCSR_DB_PICKUP,
		array(
			'name' => $pickup_label,
			'warehouse' => $warehouse,
			'zone' => $location,
			'min_weight' => $min_weight,
			'max_weight' => $max_weight,
			'price' => $price,
		),
		array( '%s', '%s', '%s', '%f', '%f', '%f' )
	);
	if( $result ){
		$result = $wpdb->insert_id;
	}
	echo $result;
	wp_die();
}
add_action( 'wp_ajax_nopriv_save_user_group', 'wpcsr_save_user_group_callback' );
add_action( 'wp_ajax_save_user_group', 'wpcsr_save_user_group_callback' );
function wpcsr_save_user_group_callback(){
	global $wpdb;
	$label 	= sanitize_text_field( $_POST['label'] );
	$description 		= sanitize_text_field( $_POST['description'] );
	$wpcsr_ug_users 	= serialize( $_POST['wpcsr_ug_users'] );
	$result = $wpdb->insert(
		$wpdb->prefix.WPCSR_DB_USER_GROUP,
		array(
			'label' => $label,
			'description' => $description,
			'users' => $wpcsr_ug_users,
		),
		array( '%s', '%s', '%s' )
	);
	if( $result ){
		$result = $wpdb->insert_id;
	}
	echo $result;
	wp_die();
}
add_action( 'wp_ajax_nopriv_wpcsr_get_pickuprate', 'wpcsr_get_pickuprate_callback' );
add_action( 'wp_ajax_wpcsr_get_pickuprate', 'wpcsr_get_pickuprate_callback' );
function wpcsr_get_pickuprate_callback(){
	$zone 		 = $_POST['zone'];
	$weight 	 = $_POST['weight'];
	$pickup_zone = wpcsr_get_pickup_zone( $zone );
	$result  	 = array(
		'price' => 0,
		'message' => $zone.' '.esc_html__('Zone out of range for pickup. Please contact support.', 'wpcargo-shipment-rate')
	);
	if( $pickup_zone ){
		$rate = wpcsr_get_pickup_rate( $pickup_zone, $weight );
		if( !$rate ){
			$result  	 = array(
				'price' => 0,
				'message' => esc_html__('Cannot calculate weight in zone', 'wpcargo-shipment-rate').' '.$zone.'. '.$weight.wpcsr_weight_unit().esc_html__(' not in range. Please contact support.', 'wpcargo-shipment-rate')
			);
		}else{
			$result  	 = array(
				'price' => $rate,
				'message' => esc_html__('Rate found.', 'wpcargo-shipment-rate')
			);
		}
	}
	echo json_encode($result);
	wp_die( );
}