<?php

/**
 * Add the field to the frontend submission
 * 
 * @since 1.8.0
 */
function conikal_add_salary_field( $fields ) {
    $fields['job']['job_salary'] = array(
        'label'       => __( 'Salary ($)', 'campoal' ),
        'type'        => 'text',
        'required'    => true,
        'placeholder' => 'e.g. 20000',
        'priority'    => 7
    );
    return $fields;
}
add_filter( 'submit_job_form_fields', 'conikal_add_salary_field' );


/**
 * Add the field to the backend edit page
 * 
 * @since 1.8.0
 */
function conikal_admin_add_salary_field( $fields ) {
    $fields['_job_salary'] = array(
      'label'       => __( 'Salary ($)', 'campoal' ),
      'type'        => 'text',
      'placeholder' => 'e.g. 20000',
      'description' => ''
    );
    return $fields;
}
add_filter( 'job_manager_job_listing_data_fields', 'conikal_admin_add_salary_field' );

/**
 * Display the field on frontend page
 * 
 * @since 1.8.0
 */
function conikal_display_job_salary_data() {
    global $post;
  
    $salary = get_post_meta( $post->ID, '_job_salary', true );
  
    if ( $salary ) {
        echo sprintf(esc_html__('$ %s', 'campoal'), $salary);
    } else {
        echo esc_html__( 'Negotiate', 'campoal' );
    }
}
// add_action( 'single_job_listing_meta_end', 'conikal_display_job_salary_data' );


/**
 * Display job category on frontend
 * 
 * @since 1.8.0
 */
function conikal_display_job_category_data($data = false) {
    global $post;
  
    $job_category = get_the_terms($post->ID, 'job_listing_category');

    if ($data) {
        return $job_category;
    } else {
        if ( !empty($job_category) ) {
            foreach ($job_category as $id => $category) {
                if (isset($category->term_id)) { ?>
                <a href="<?php echo esc_url(get_term_link($category->term_id)) ?>">
                    <?php echo esc_html($category->name); ?>
                </a>
                <?php echo esc_html((count($job_category) == ($id + 1) ? '' : ', ')) ?>
            <?php }
            }
        } else {
            return false;
        }   
    }
}