<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

ob_start();
if ( isset( $display ) && ( 'form' === $display || 'form_only' === $display ) ) {
	echo '<div class="eh_crm_support_main wsdesk_wrapper">';
}
$raiser_default        = eh_crm_get_settingsmeta( 0, 'ticket_raiser' );
$login_redirect_url    = eh_crm_get_settingsmeta( 0, 'login_redirect_url' );
$logout_redirect_url   = eh_crm_get_settingsmeta( 0, 'logout_redirect_url' );
$register_redirect_url = eh_crm_get_settingsmeta( 0, 'register_redirect_url' );
$login_url             = eh_crm_get_settingsmeta( '0', 'login_url' );
$reg_url               = eh_crm_get_settingsmeta( '0', 'reg_url' );

if ( is_user_logged_in() ) {
	$purchase_credit_redirect_url = eh_crm_get_settingsmeta( '0', 'purchase_credit_redirect_url' );
	$set_credit_limit             = eh_crm_get_settingsmeta( '0', 'set_credit_limit' );
	if ( ! $set_credit_limit ) {
		$set_credit_limit = 0;
	}
	if ( EH_CRM_PAY_FOR_SUPPORT_STATUS ) {
		$user_id                   = 'wsdesk_subscription_method' . get_current_user_id();
		$user_pay_for_support_data = get_option( strval( $user_id ) );
		$main_score                = get_user_meta( Parent_Child_Accounts::has_parent( get_current_user_id() ), Pfs_metakey_constants::MAIN_SCORE, true );
		if ( false == $main_score || null == $main_score || '' == $main_score ) {
			$main_score = 0;
		}
		if ( $main_score < $set_credit_limit ) {
			echo '<div class="form-elements"><span>' . esc_html__( 'You do not have enough credits to raise a ticket.', 'wsdesk' ) . '</span><br><a class="btn btn-primary" href="' . esc_url( $purchase_credit_redirect_url ) . '">' . esc_html__( 'Purchase Credits', 'wsdesk' ) . '</a></div>';
			return ob_get_clean();
		}
	}
}

if ( $register_redirect_url ) {
	$register_redirect_url = 'redirect_to=' . urlencode( $register_redirect_url );
}
if ( $login_redirect_url ) {
	$login_redirect_url = 'redirect_to=' . urlencode( $login_redirect_url );
}
if ( 'registered' == $raiser_default ) {
	if ( ! is_user_logged_in() ) {
		$login_url = eh_crm_append_query_string_to_url( $login_url ? $login_url : wp_login_url(), $login_redirect_url );
		$reg_url   = eh_crm_append_query_string_to_url( $reg_url ? $reg_url : wp_registration_url(), $register_redirect_url );

		echo '<div class="form-elements log-in"><span>' . esc_html( get_existing_ticket_login_label() ) . '</span><br><a class="btn btn-primary" href="' . esc_url( $login_url ) . '">' . esc_html__( 'Login', 'wsdesk' ) . '</a></div>';
		echo '<div class="form-elements sign-up"><span>' . esc_html( get_existing_ticket_register_label() ) . '</span><br><a class="btn btn-primary" href="' . esc_url( $reg_url ) . '">' . esc_html__( 'Register', 'wsdesk' ) . '</a></div>';
		return ob_get_clean();
	}
} elseif ( 'guest' == $raiser_default
) {
	if ( is_user_logged_in() ) {
		echo '<div class="form-elements"><span>' . esc_html__( 'You must Logout to Raise Ticket', 'wsdesk' ) . '</span><br><a class="btn btn-primary" href="' . esc_url( wp_logout_url() ) . '">' . esc_html__( 'Logout', 'wsdesk' ) . '</a></div>';
		return ob_get_clean();
	}
}
$args            = array( 'type' => 'field' );
$fields          = array( 'slug', 'title', 'settings_id' );
$avail_fields    = eh_crm_get_settings( $args, $fields );
$selected_fields = eh_crm_get_settingsmeta( 0, 'selected_fields' );

if ( empty( $selected_fields ) ) {
	$selected_fields = array( 'request_email', 'request_title', 'request_description' );
}
if ( isset( $cus_fields ) && ! empty( $cus_fields ) && is_array( $cus_fields ) ) {
	$select_val = array();
	foreach ( $cus_fields as $value ) {
		if ( in_array( $value, $selected_fields ) ) {
			array_push( $select_val, $value );
		}
	}
	$selected_fields = $select_val;
}
if ( ! in_array( 'request_description', $selected_fields ) ) {
	array_unshift( $selected_fields, 'request_description' );
}
if ( ! in_array( 'request_title', $selected_fields ) ) {
	array_unshift( $selected_fields, 'request_title' );
}
if ( ! in_array( 'request_email', $selected_fields ) ) {
	array_unshift( $selected_fields, 'request_email' );
}
$input_width = eh_crm_get_settingsmeta( 0, 'input_width' );
$utitle      = eh_crm_get_settingsmeta( 0, 'new_ticket_form_title' );

$utitle = eh_crm_wpml_translations( $utitle, 'title', 'title' );

$submit = eh_crm_get_settingsmeta( 0, 'submit_ticket_button' );

if ( ! $submit ) {
	$submit = __( 'Submit Request', 'wsdesk' );
}

$submit = eh_crm_wpml_translations( $submit, 'submit', 'submit' );

$reset = eh_crm_get_settingsmeta( 0, 'reset_ticket_button' );

if ( ! $reset ) {
	$reset = __( 'Reset Request', 'wsdesk' );
}

$reset = eh_crm_wpml_translations( $reset, 'reset', 'reset' );

$existing = eh_crm_get_settingsmeta( 0, 'existing_ticket_button' );
if ( ! $existing ) {
	$existing = __( 'Check your Existing Request', 'wsdesk' );
}

$existing = eh_crm_wpml_translations( $existing, 'existing', 'existing' );
if ( isset( $display ) && ( 'form' === $display ) ) {
	echo '
            <div class="support_option_choose">
                <a href="' . esc_url( eh_get_url_by_shortcode( '[wsdesk_support display="check_request"' ) ) . '"  target="_blank" data-loading-text="' . esc_html__( 'Loading your Request...', 'wsdesk' ) . '" class="btn btn-primary eh_crm_check_request" role="button">
                    ' . esc_html( $existing ) . '
                </a>
        </div>';
}
echo '<div class="main_new_suppot_request_form">';
echo '<style>
                .support_form
                {
                    width: ' . esc_html( $input_width ) . '% !important;
                }
                </style> <form class="support_form" id="eh_crm_ticket_form">';
echo ( '' !== $utitle ) ? '<h3>' . esc_html( $utitle ) . '</h3>' : '';
$urole = '';
if ( is_user_logged_in() ) {
	if ( isset( wp_get_current_user()->roles ) ) {
		$wwp_roles = array( 'administrator', 'WSDesk_Agents', 'WSDesk_Supervisor' );
		foreach ( $wwp_roles as $wp_role ) {

			$urole_index = array_search( $wp_role, wp_get_current_user()->roles );
			if ( $urole_index ) {
				$urole = wp_get_current_user()->roles[ $urole_index ];
				break;
			}
		}
	}
}
for ( $i = 0; $i < count( $selected_fields ); $i++ ) {
	for ( $j = 0; $j < count( $avail_fields ); $j++ ) {
		$current_meta = eh_crm_get_settingsmeta( $avail_fields[ $j ]['settings_id'] );
		if ( $avail_fields[ $j ]['slug'] === $selected_fields[ $i ] && ( ! isset( $current_meta['field_visible'] ) || 'yes' === $current_meta['field_visible'] || 'ip' == $current_meta['field_type'] || in_array( $avail_fields[ $j ]['slug'], array( 'request_email', 'request_title', 'request_description' ) ) ) ) {
			echo '<div class="form-elements">';
			if ( 'WSDesk_Agents' == $urole ) {
				 $required = ( isset( $current_meta['field_require_agent'] ) ? $current_meta['field_require_agent'] : '' );
			} elseif ( 'WSDesk_Supervisor' == $urole || 'administrator' == $urole ) {
				$required = 'no';
			} else {
				$required = ( isset( $current_meta['field_require'] ) ? $current_meta['field_require'] : '' );
			}

			$required = ( 'yes' === $required || in_array( $avail_fields[ $j ]['slug'], array( 'request_email', 'request_title', 'request_description' ) ) ) ? 'required' : '';
			if ( 'ip' == $current_meta['field_type'] ) {
				echo '<input type="hidden" value="' . esc_html( isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( $_SERVER['REMOTE_ADDR'] ) : null ) . '" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" class="input_element form-control">';
			} else {
				$field_title = eh_crm_wpml_translations( $avail_fields[ $j ]['title'], $avail_fields[ $j ]['settings_id'] . '_field_title', $avail_fields[ $j ]['settings_id'] . '_field_title' );
				if ( 'woo_order_id' == $selected_fields[ $i ] ) {
					if ( is_user_logged_in() ) {
						echo '<span>' . esc_html( $field_title ) . ' </span>';
					} else {
						$required = '';
					}
				} else {
					echo '<span>' . esc_html( $field_title ) . ' </span>';
				}
			}
			echo ( 'required' === $required ) ? '<span class="input_required">*</span>' : ' <br>';
			$default_values = ( isset( $current_meta['field_default'] ) ? $current_meta['field_default'] : '' );
			switch ( $current_meta['field_type'] ) {
				case 'text':
					$default_values = eh_crm_wpml_translations( $default_values, $avail_fields[ $j ]['settings_id'] . '_field_default', $avail_fields[ $j ]['settings_id'] . '_field_default' );

					$field_placeholder = eh_crm_wpml_translations( $current_meta['field_placeholder'], $avail_fields[ $j ]['settings_id'] . '_field_placeholder', $avail_fields[ $j ]['settings_id'] . '_field_placeholder' );
					echo '<input type="text" autocomplete="off" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" value="' . esc_html( $default_values ) . '" class="input_element form-control" placeholder="' . esc_html( $field_placeholder ) . '" ' . esc_html( $required ) . '>';
					break;
				case 'email':
					$email = '';
					if ( is_user_logged_in() && 'request_email' == $avail_fields[ $j ]['slug'] ) {
						$uid            = get_current_user_id();
						$user           = new WP_User( $uid );
						$default_values = $user->user_email;
					}
					$field_placeholder = eh_crm_wpml_translations( $current_meta['field_placeholder'], $avail_fields[ $j ]['settings_id'] . '_field_placeholder', $avail_fields[ $j ]['settings_id'] . '_field_placeholder' );
					echo '<input type="email" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" value="' . esc_html( $default_values ) . '" class="input_element form-control" placeholder="' . esc_html( $field_placeholder ) . '"' . esc_html( $required ) . '>';
					break;
				case 'phone':
					echo '<br><span><strong>+</strong><input type="number" oninput="this.value = !!this.value && Math.abs(this.value) >= 0 ? Math.abs(this.value) : null" min="0" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" value="' . esc_html( $default_values ) . '" class="input_element form-control" placeholder="' . esc_html( $current_meta )['field_placeholder'] . '"' . esc_html( $required ) . ' style="display:  inline; width: 99% !important"></span>';
					break;
				case 'number':
					echo '<input type="number" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" value="' . esc_html( $default_values ) . '" class="input_element form-control" placeholder="' . esc_html( $current_meta['field_placeholder'] ) . '"' . esc_html( $required ) . '>';
					break;
				case 'password':
					echo '<input type="password" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" value="' . esc_html( $default_values ) . '" class="input_element form-control" placeholder="' . esc_html( $current_meta['field_placeholder'] ) . '"' . esc_html( $required ) . '>';
					break;
				case 'select':
					$field_values          = $current_meta['field_values'];
					$customer_temp_altered = array();
					$return                = array();
					if ( 'woo_order_id' == $selected_fields[ $i ] ) {
						$uid = get_current_user_id();
						if ( 0 != $uid ) {
							$customer_orders = wc_get_orders( array( 'customer_id' => $uid ) );
							if ( count( $customer_orders ) > 0 ) {
								foreach ( $customer_orders as $key => $customer_order ) {
									array_push( $customer_temp_altered, trim( str_replace( ' ', '', $customer_order->get_order_number() ) ) );
								}
								for ( $g = 0;$g < count( $customer_temp_altered );$g++ ) {
									$return[ $g ] = $customer_temp_altered[ $g ];
								}
								echo '<select class="input_element form-control" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" ' . esc_html( $required ) . '>';
								echo '<option value="">' . ( isset( $current_meta['field_placeholder'] ) ? esc_html( $current_meta['field_placeholder'] ) : '-' ) . '</option>';
								foreach ( $return as $key => $val ) {
									 $select_default = '';
									if ( $default_values === $val ) {
										$select_default = 'selected';
									}
									$field_values_tranlsated = eh_crm_wpml_translations( $val, $avail_fields[ $j ]['settings_id'] . '_' . esc_html( $key ) . '_field_values', $avail_fields[ $j ]['settings_id'] . '_' . esc_html( $key ) . '_field_values' );
									echo '<option value="' . esc_html( $val ) . '" ' . esc_html( $select_default ) . '>' . esc_html( $field_values_tranlsated ) . '</option>';
								}
								echo '</select>';
								break;
							}
						}
					} else {
						$field_values = $current_meta['field_values'];
						$field_keys   = array_keys( $field_values );
						echo '<select class="input_element form-control" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" ' . esc_html( $required ) . '>';
						echo '<option value="">' . ( isset( $current_meta['field_placeholder'] ) ? esc_html( $current_meta['field_placeholder'] ) : '-' ) . '</option>';

						$field_order = isset( $current_meta['field_order'] ) ? array_values( array_unique( $current_meta['field_order'] ) ) : ( isset( $field_keys ) ? $field_keys : array() );
						$field_order = array_intersect( $field_order, array_keys( $current_meta['field_values'] ) );
						foreach ( $field_order as $value ) {
							$key            = $value;
							$select_default = '';
							if ( $default_values === $key ) {
								$select_default = 'selected';
							}
							$field_values_tranlsated = eh_crm_wpml_translations( $field_values[ $key ], $avail_fields[ $j ]['settings_id'] . '_' . esc_html( $key ) . '_field_values', $avail_fields[ $j ]['settings_id'] . '_' . esc_html( $key ) . '_field_values' );
							echo '<option value="' . esc_html( $key ) . '" ' . esc_html( $select_default ) . '>' . esc_html( $field_values_tranlsated ) . '</option>';
						}
						echo '</select>';
						break;
					}
					break;
				case 'radio':
					$field_values = $current_meta['field_values'];
					echo '<span style="vertical-align: middle;display: block;">';
					foreach ( $field_values as $key => $value ) {
						$radio_default = '';
						if ( $default_values === $key ) {
							$radio_default = 'checked';
						}
						$field_values_tranlsated = eh_crm_wpml_translations( $value, $avail_fields[ $j ]['settings_id'] . '_' . esc_html( $key ) . '_field_values', $avail_fields[ $j ]['settings_id'] . '_' . esc_html( $key ) . '_field_values' );
						echo '<input type="radio" class="form-control" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" style="margin-top: 0;" value="' . esc_html( $key ) . '" ' . esc_html( $radio_default ) . ' ' . esc_html( $required ) . '>' . esc_html( $field_values_tranlsated ) . '<br>';
					}
					echo '</span>';
					break;
				case 'checkbox':
					$field_values = $current_meta['field_values'];
					echo '<span style="vertical-align: middle;display: block;">';
					foreach ( $field_values as $key => $value ) {
						$check_default = '';
						if ( $default_values === $key ) {
							$check_default = 'checked';
						}
						$field_values_tranlsated = eh_crm_wpml_translations( $value, $avail_fields[ $j ]['settings_id'] . '_' . esc_html( $key ) . '_field_values', $avail_fields[ $j ]['settings_id'] . '_' . esc_html( $key ) . '_field_values' );
						echo '<input type="checkbox" data-slug="' . esc_html( $selected_fields[ $i ] ) . '" data-required="' . esc_html( $required ) . '" class="form-control toggle-checkbox-required ' . esc_html( $selected_fields[ $i ] ) . '" name="' . esc_html( $selected_fields[ $i ] ) . '[]" id="' . esc_html( $selected_fields[ $i ] ) . '[]" style="margin-top: 0;" value="' . esc_html( $key ) . '" ' . esc_html( $check_default ) . ' ' . esc_html( $required ) . '> ' . esc_html( $field_values_tranlsated ) . '<br>';
					}
					echo '</span>';
					break;
				case 'textarea':
					$default_values = eh_crm_wpml_translations( $default_values, $avail_fields[ $j ]['settings_id'] . '_field_default', $avail_fields[ $j ]['settings_id'] . '_field_default' );
					echo '<textarea name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" class="input_element form-control" ' . esc_html( $required ) . '>' . esc_html( $default_values ) . '</textarea>';
					break;
				case 'date':
					echo '<input type="text" autocomplete="off" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" class="input_element form-control trigger_date_jq" placeholder="' . esc_html( $current_meta['field_placeholder'] ) . '" ' . esc_html( $required ) . '>';
					break;
				case 'file':
					$file_type = ( 'multiple' === $current_meta['file_type'] ) ? 'multiple' : '';
					if ( 'multiple' === $file_type ) {
						$multiple_required = $required;
						$required          = '';
						?>
							<div class="ticket_multiple_attachments_container <?php echo esc_html( $multiple_required ); ?>">
							<div class="multiple_attachments_previewer">
								<div class="multiple_attachments_previewer_item hidden">
									<span class="file_name">file_name.jpg</span>
									<span class="glyphicon glyphicon-remove remove"></span>
								</div>
							</div>
							<div class="multiple_attachments_drop_zone">
							</div>
							<div class="multiple_attachments_actions">
								<button class="btn btn-primary multiple_attachments_browse_files_trigger" type="button" name="multiple_attachments_browse_files_trigger">
									<i class="glyphicon glyphicon-plus"></i>
					<?php echo esc_html_e( 'Browse files', 'wsdesk' ); ?>
								</button>
							</div>
						</div>
					<?php
					}
					echo '<input type="file" name="' . esc_html( $selected_fields[ $i ] ) . '" id="' . esc_html( $selected_fields[ $i ] ) . '" ' . esc_html( $file_type ) . ' class="input_element ' . ( 'multiple' === $file_type ? ' hidden ' : '' ) . 'form-control ticket_attachment" ' . esc_html( $required ) . ' style="height: auto;">';
					break;

				case 'google_captcha':
					echo '<div class="g-recaptcha" data-sitekey="' . esc_html( $current_meta['field_site_key'] ) . '"></div><div class="captcha-error"></div>';
					break;
				case 'pfs_order_product':
					if ( class_exists( 'WSDESK_Pay_for_Support_Subscription' ) === false ) {
						break;
					}
					echo '<select name="pfs_order_product" id="pfs_order_product" class="input_element form-control "' . esc_html( $required ) . '>';
					$pfs_user_id = get_current_user_id();
					if ( 0 != $pfs_user_id ) {
						$current_product_names = WSDESK_Pay_for_Support_Subscription::pfs_get_product_names( $pfs_user_id );
						if ( count( $current_product_names ) > 0 ) {
							echo '<option value="">' . esc_html__( 'Please select a product for the ticket', 'wsdesk' ) . '</option>';
							foreach ( $current_product_names as $products ) {
								echo '<option value="' . esc_html( $products['order_id'] ) . ',' . esc_html( $products['product_id'] ) . '">' . esc_html( $products['product_name'] ) . ' (order id:' . esc_html( $products['order_id'] ) . ')</option>';
							}
						}
					}
					echo '</select>';
					if ( ! is_user_logged_in() ) {
						echo '<p style="margin:0px;font-size:0.9em">' . esc_html__( 'Please login to see your orders here', 'wsdesk' ) . '</p>';
					}
					break;
			}
			$field_description = eh_crm_wpml_translations( $current_meta['field_description'], $avail_fields[ $j ]['settings_id'] . '_field_description', $avail_fields[ $j ]['settings_id'] . '_field_description' );

			if ( 'woo_order_id' == $selected_fields[ $i ] ) {
				$uid = get_current_user_id();
				if ( 0 != $uid ) {
					echo '<small>' . esc_html( $field_description ) . '</small>';
				}
			} else {
				echo '<small>' . esc_html( $field_description ) . '</small>';
			}

			if ( eh_crm_get_settingsmeta( 0, 'auto_suggestion' ) == 'enable' && 'request_title' == $selected_fields[ $i ] ) {
				echo '<div class="auto_suggestion_posts"></div>';
			}
			echo '</div><br>';
		}
	}
}
echo '<button  type="submit" id="crm_form_submit" class="btn btn-primary" data-loading-text="' . esc_html__( 'Submitting...', 'wsdesk' ) . '">' . esc_html( $submit ) . '</button>';
echo '<button  type="reset" class="btn btn-primary wsdesk_crm_reset_button_show_hide" style="margin-left:5px;">' . esc_html( $reset ) . '</button></form>';
echo '</div>';
if ( ! defined( 'WSDESK_POWERED_SUPPORT' ) ) {
	echo '<div class="powered_wsdesk"><span>' . esc_html__( 'Powered by', 'wsdesk' ) . '</span> <a href="https://elextensions.com/plugin/wsdesk-wordpress-support-desk-plugin/" target="_blank" rel="nofollow">WSDesk</a></div>';
}
return ob_get_clean();
