<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
ob_start();
$etitle          = eh_crm_get_settingsmeta( 0, 'main_ticket_form_title' );
$etitle          = eh_crm_wpml_translations( $etitle, 'title', 'title' );
$selected_fields = eh_crm_get_settingsmeta( 0, 'selected_fields' );
$select_val      = array();
if ( is_array( $selected_fields ) ) {
	if ( isset( $cus_fields ) && ! empty( $cus_fields ) && is_array( $cus_fields ) ) {
		$select_val = array_intersect( $selected_fields, $cus_fields );
	}
}
$ticket_id = '';
if ( isset( $_GET['customer_ticket_num'] ) ) {
	$ticket_id = sanitize_text_field( $_GET['customer_ticket_num'] );
} elseif ( ! empty( $_GET ) ) {
	foreach ( $_GET as $query_id => $query_value ) {
		if ( strpos( $query_value, 'customer_ticket_num' ) != '' ) {
			$ticket_id = explode( 'customer_ticket_num=', $query_value )[1];
			break;
		}
	}
}
if ( $ticket_id && is_user_logged_in() ) {
	$ticket        = eh_crm_get_ticket( array( 'ticket_id' => $ticket_id ), array( 'ticket_author', 'ticket_email' ) );
	$wcurrent_user = wp_get_current_user();
	if ( $ticket[0]['ticket_email'] == $wcurrent_user->user_email || $ticket[0]['ticket_author'] == $wcurrent_user->ID ) {
		echo'<div class="eh_crm_support_main wsdesk_wrapper"> 
            <div class="loaderDirect"></div>
            <div class="eh_crm_support_main load_wsdesk_request_ticket_directly">
                <div class="ticket_table_wrapper">
                <div class="ticket_load_content"></div>
                </div>
            </div>
        </div>';
	}
} else {
	?>
<div class="eh_crm_support_main wsdesk_wrapper">
	<input type= "hidden" id="custom_fields" value ="<?php echo esc_html( json_encode( $select_val ) ); ?>"/>
	<?php echo ( '' !== $etitle ) ? '<h3>' . esc_html( $etitle ) . '</h3>' : ''; ?>
	<div class="wsdesk_wrapper"> 
	<div class="loaderDirect"></div>
	<div class="eh_crm_support_main load_wsdesk_request_directly wsdesk_new_ticket_view">
		<div class="ticket_table_wrapper"></div>
	</div>
	</div>
</div>
	<?php
}
if ( ! defined( 'WSDESK_POWERED_SUPPORT' ) ) {
	echo '<div id="check_request_powered_wsdesk" 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();
