<?php
if (!defined('ABSPATH')) {
	exit; // Exit if accessed directly
}

$cv_file = civi_get_option('civi-cv-type');
$cv_max_file_size = civi_get_option('civi_image_max_file_size', '1000kb');
$text = '<i class="far fa-arrow-from-bottom large"></i> ' . esc_attr(sprintf(esc_html__('Upload CV (%s)', 'civi-framework'), $cv_file));
$upload_nonce = wp_create_nonce('civi_thumbnail_allow_upload');
$url = CIVI_AJAX_URL .  '?action=civi_thumbnail_upload_ajax&nonce=' . esc_attr($upload_nonce);

wp_enqueue_script('plupload');
wp_enqueue_script('jquery-validate');
wp_enqueue_script(CIVI_PLUGIN_PREFIX . 'upload-cv');
wp_localize_script(
	CIVI_PLUGIN_PREFIX . 'upload-cv',
	'civi_upload_cv_vars',
	array(
		'ajax_url'    => CIVI_AJAX_URL,
		'title'   => esc_html__('Valid file formats', 'civi-framework'),
		'cv_file' => $cv_file,
		'cv_max_file_size' => $cv_max_file_size,
		'upload_nonce' => $upload_nonce,
		'url' => $url,
		'text' => $text,
	)
);

global $current_user;
$user_id = $current_user->ID;
$candidate_id =  $fileUrl = '';
if (in_array('civi_user_candidate', (array)$current_user->roles)) {
	$args_candidate = array(
		'post_type' => 'candidate',
		'author' => $user_id,
	);
	$query = new WP_Query($args_candidate);
	$candidate_id = $query->post->ID;
}
$jobs_id = get_the_ID();
$jobs_select_apply = !empty(get_post_meta($jobs_id, CIVI_METABOX_PREFIX . 'jobs_select_apply')) ? get_post_meta($jobs_id, CIVI_METABOX_PREFIX . 'jobs_select_apply')[0] : '';

$candidate_phone = $candidate_email = '';
if (!empty($candidate_id)) {
	$candidate_resume = get_post_meta($candidate_id, CIVI_METABOX_PREFIX . 'candidate_resume_id_list', false);
	$candidate_resume = !empty($candidate_resume) ? $candidate_resume[0] : '';
	$fileName = basename(get_attached_file($candidate_resume));
	$candidate_email = !empty(get_post_meta($candidate_id, CIVI_METABOX_PREFIX . 'candidate_email')) ? get_post_meta($candidate_id, CIVI_METABOX_PREFIX . 'candidate_email')[0] : '';
	$candidate_phone = !empty(get_post_meta($candidate_id, CIVI_METABOX_PREFIX . 'candidate_phone')) ? get_post_meta($candidate_id, CIVI_METABOX_PREFIX . 'candidate_phone')[0] : '';
	if (!empty(wp_get_attachment_url($candidate_resume))) {
		$fileUrl = wp_get_attachment_url($candidate_resume);
	}
}

$show_field_jobs_apply = civi_get_option( 'show_field_jobs_apply' );
$candidate_meta_data = get_post_custom($candidate_id);
$candidate_current_position = isset($candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_current_position']) ? $candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_current_position'][0] : '';
$candidate_categories = isset($candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_categories']) ? $candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_categories'][0] : '';
$candidate_dob = isset($candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_dob']) ? $candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_dob'][0] : '';
$candidate_age = isset($candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_age']) ? $candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_age'][0] : '';
$candidate_gender = isset($candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_gender']) ? $candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_gender'][0] : '';
$candidate_languages = isset($candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_languages']) ? $candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_languages'][0] : '';
$candidate_qualification = isset($candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_qualification']) ? $candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_qualification'][0] : '';
$candidate_yoe = isset($candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_yoe']) ? $candidate_meta_data[CIVI_METABOX_PREFIX . 'candidate_yoe'][0] : '';
if(empty($show_field_jobs_apply)){
    $col = 'col-md-12';
    $max_width = '';
} else {
    $col = 'col-md-6';
    $max_width = '550px';
}
?>

<form action="#" method="post" class="form-popup form-popup-apply" id="civi_form_apply_jobs" enctype="multipart/form-data">
	<div class="bg-overlay"></div>
	<div class="apply-popup custom-scrollbar" style="max-width: <?php echo $max_width; ?>">
		<a href="#" class="btn-close"><i class="far fa-times"></i></a>
		<h5><?php esc_html_e('Apply for this job', 'civi-framework') ?></h5>
		<div class="row">
			<div class="form-group <?php echo $col; ?>">
				<label for="apply_email"><?php esc_html_e('Email address', 'civi-framework') ?></label>
				<input type="email" id="apply_email" name="apply_emaill" placeholder="<?php esc_attr_e('Enter email', 'civi-framework') ?>" value="<?php echo esc_attr($candidate_email) ?>">
			</div>
			<div class="form-group <?php echo $col; ?>">
                <?php 
                    $default_phone_number = civi_get_option('default_phone_number');
                ?>
				<label for="apply_phone"><?php esc_html_e('Phone', 'civi-framework') ?></label>
				<div class="tel-group">
					<select name="prefix_code" class="civi-select2 prefix-code">
						<?php
						$prefix_code = phone_prefix_code();
							foreach ($prefix_code as $key => $value) {
								echo '<option value="' . $key . '" data-dial-code="' . $value['code'] . '" '.( $default_phone_number == $key ? 'selected':'').'>' . $value['name'] . ' (' . $value['code'] . ')</option>';
							}
						?>
					</select>
					<input type="tel" id="apply_phone" name="apply_phone" placeholder="<?php esc_attr_e('Enter phone', 'civi-framework') ?>" value="<?php echo esc_attr($candidate_phone) ?>">
				</div>
			</div>

            <?php if(!empty($show_field_jobs_apply)) : ?>
                <?php if (in_array('position', $show_field_jobs_apply)) : ?>
                    <div class="form-group col-md-6">
                        <label for="candidate_current_position"><?php esc_html_e('Current Position', 'civi-framework') ?></label>
                        <input class="point-mark" type="text" id="candidate_current_position" name="candidate_current_position" value="<?php echo esc_attr($candidate_current_position); ?>" placeholder="<?php esc_attr_e('Ex: UI/UX Designer', 'civi-framework'); ?>">
                    </div>
                <?php endif; ?>
                <?php if (in_array('categories', $show_field_jobs_apply)) : ?>
                    <div class="form-group col-md-6">
                        <label for="candidate_categories"><?php esc_html_e('Categories', 'civi-framework') ?></label>
                        <div class="select2-field">
                            <select class="point-mark civi-select2" name="candidate_categories" id="candidate_categories">
                                <?php civi_get_taxonomy_by_post_id($candidate_id, 'candidate_categories', true); ?>
                            </select>
                        </div>
                    </div>
                <?php endif; ?>
                <?php if (in_array('date', $show_field_jobs_apply)) : ?>
                    <div class="form-group col-md-6">
                        <label for="candidate_dob"><?php esc_html_e('Date of Birth', 'civi-framework') ?></label>
                        <input class="point-mark" type="date" id="candidate_dob" name="candidate_dob" value="<?php echo esc_attr($candidate_dob); ?>">
                    </div>
                <?php endif; ?>
                <?php if (in_array('age', $show_field_jobs_apply)) : ?>
                    <div class="form-group col-md-6">
                        <label for="candidate_age"><?php esc_html_e('Age', 'civi-framework') ?></label>
                        <div class="select2-field">
                            <select class="point-mark civi-select2" name="candidate_age" id="candidate_age">
                                <?php civi_get_taxonomy_by_post_id($candidate_id, 'candidate_ages', true); ?>
                            </select>
                        </div>
                    </div>
                <?php endif; ?>
                <?php if (in_array('gender', $show_field_jobs_apply)) : ?>
                    <div class="form-group col-md-6">
                        <label for="candidate_gender"><?php esc_html_e('Gender', 'civi-framework') ?></label>
                        <div class="select2-field">
                            <select class="point-mark civi-select2" name="candidate_gender" id="candidate_gender">
                                <?php civi_get_taxonomy_by_post_id($candidate_id, 'candidate_gender', true); ?>
                            </select>
                        </div>
                    </div>
                <?php endif; ?>
                <?php if (in_array('languages', $show_field_jobs_apply)) : ?>
                    <div class="form-group col-md-6">
                        <label for="candidate_languages"><?php esc_html_e('Languages', 'civi-framework') ?></label>
                        <div class="select2-field">
                            <select class="civi-select2 point-mark" name="candidate_languages" id="candidate_languages">
                                <?php civi_get_taxonomy_by_post_id($candidate_id, 'candidate_languages', true); ?>
                            </select>
                        </div>
                    </div>
                <?php endif; ?>
                <?php if (in_array('qualification', $show_field_jobs_apply)) : ?>
                    <div class="form-group col-md-6">
                        <label for="candidate_qualification"><?php esc_html_e('Qualification', 'civi-framework') ?></label>
                        <div class="select2-field">
                            <select class="point-mark civi-select2" name="candidate_qualification" id="candidate_qualification">
                                <?php civi_get_taxonomy_by_post_id($candidate_id, 'candidate_qualification', true); ?>
                            </select>
                        </div>
                    </div>
                <?php endif; ?>
                <?php if (in_array('experience', $show_field_jobs_apply)) : ?>
                    <div class="form-group col-md-6">
                        <label for="candidate_yoe"><?php esc_html_e('Years of Experience', 'civi-framework') ?></label>
                        <div class="select2-field">
                            <select class="point-mark civi-select2" name="candidate_yoe" id="candidate_yoe">
                                <?php
                                $args = array(
                                    'orderby'    => 'name',
                                    'parent'     => 0,
                                    'hide_empty' => false,
                                    'meta_key' => 'candidate_experience_order',
                                    'orderby' => 'meta_value_num',
                                    'meta_type' => 'DATE',
                                );

                                $post_id = $candidate_id;
                                $taxonomy_name = 'candidate_yoe';
                                $terms = get_terms( $taxonomy_name, $args );
                                $result = array();

                                foreach ( $terms as $term ) {
                                    $term_children            = get_terms( $taxonomy_name, array(
                                        'parent'     => $term->term_id,
                                        'hide_empty' => false
                                    ) );
                                    $result[ $term->term_id ] = array();
                                    foreach ( $term_children as $child ) {
                                        $child_level_2                               = get_terms( $taxonomy_name, array(
                                            'parent'     => $child->term_id,
                                            'hide_empty' => false
                                        ) );
                                        $result[ $term->term_id ][ $child->term_id ] = array();
                                        foreach ( $child_level_2 as $grandchild ) {
                                            $result[ $term->term_id ][ $child->term_id ][ $grandchild->term_id ] = array();
                                        }
                                    }
                                }

                                $target_by_id   = array();
                                $tax_terms      = get_the_terms( $post_id, $taxonomy_name );
                                if ( ! empty( $tax_terms ) ) {
                                    foreach ( $tax_terms as $tax_term ) {
                                        $target_by_id[] = $tax_term->term_id;
                                    }
                                }
                                if ( $target_by_id == 0 || empty( $target_by_id ) ) {
                                    echo '<option value="" selected>' . esc_html__( 'Select an option', 'civi-framework' ) . '</option>';
                                } else {
                                    echo '<option value="">' . esc_html__( 'Select an option', 'civi-framework' ) . '</option>';
                                }
                                civi_get_taxonomy_target_by_id( $result, $target_by_id, $taxonomy_name );
                                ?>
                            </select>
                        </div>
                    </div>
                <?php endif; ?>
            <?php endif; ?>

			<div class="form-group col-md-12">
				<label for="apply_message"><?php esc_html_e('Message', 'civi-framework') ?></label>
				<textarea id="apply_message" name="apply_message" rows="4" cols="50"></textarea>
			</div>
			<div class="form-group col-md-12 civi-upload-cv">
				<div class="form-field">
					<div id="cv_errors_log" class="errors-log"></div>
					<div id="civi_cv_plupload_container" class="file-upload-block preview">
						<div class="civi_cv_file civi_add-cv">
							<p id="civi_drop_cv">
								<?php if (!empty($fileName)) { ?>
									<button type="button" id="civi_select_cv">
										<i class="far fa-arrow-from-bottom large"></i>
										<?php esc_html_e($fileName); ?>
									</button>
								<?php } else { ?>
									<button type="button" id="civi_select_cv">
										<i class="far fa-arrow-from-bottom large"></i>
										<?php echo esc_attr(sprintf(esc_html__('Upload CV (%s)', 'civi-framework'), $cv_file)); ?>
									</button>
								<?php } ?>
							</p>
						</div>
						<input type="hidden" class="cv_url form-control" name="jobs_cv_url" value="<?php echo esc_attr($fileUrl) ?>" id="cv_url">
						<input type="hidden" class="type_apply form-control" name="type_apply" value="<?php esc_html_e($jobs_select_apply); ?>" id="type_apply">
					</div>
				</div>
			</div>
		</div>
		<div class="message_error"></div>
		<div class="button-warpper">
			<a href="#" class="civi-button button-outline button-block button-cancel"><?php esc_html_e('Cancel', 'civi-framework'); ?></a>
			<button type="submit" class="civi-button button-block btn-submit-apply-jobs" id="btn-apply-jobs-<?php echo $jobs_id ?>" data-jobs_id="<?php echo $jobs_id ?>" data-candidate_id="<?php echo $candidate_id ?>">
				<?php esc_html_e('Apply Jobs', 'civi-framework'); ?>
				<span class="btn-loading"><i class="fal fa-spinner fa-spin large"></i></span>
			</button>
		</div>
	</div>
</form>
