<?php
use Gigneo\Modules;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$job_id           = ! empty( $args['p_id'] ) ? $args['p_id'] : 0;
$proposal_id      = ! empty( $args['proposal_id'] ) ? $args['proposal_id'] : 0;
$privacy_page_id  = gigneo_option( '_gigneo_ts_template_privacy_policy' );
$terms_page_id    = gigneo_option( '_gigneo_ts_template_terms_and_condition' );
$apply_type       = gigneo_option( '_gigneo_ts_job_apply_type' );
$apply_credits    = gigneo_option( '_gigneo_ts_job_apply_credits' );
$profile_id       = gigneo_active_profile_id();
$profile_type     = gigneo_profile_type( $profile_id );
$terms_url        = $terms_page_id ? esc_url( get_permalink( $terms_page_id ) ) : '#';
$privacy_url      = $privacy_page_id ? esc_url( get_permalink( $privacy_page_id ) ) : '#';
$terms_title      = $terms_page_id ? esc_html( get_the_title( $terms_page_id ) ) : esc_html__( 'Terms and Condition', 'gigneo' );
$privacy_title    = $privacy_page_id ? esc_html( get_the_title( $privacy_page_id ) ) : esc_html__( 'Privacy Policy', 'gigneo' );
$job_status       = get_post_status( $job_id );
$job_post_type    = get_post_type( $job_id );
$freelancer_label = gigneo_freelancer_singular();

if ( is_user_logged_in() && 'freelancer' !== $profile_type ) {
	$not_found_args = array(
		'title'       => sprintf(
			// translators: %s: Freelancer label.
			esc_html__( 'Only %1$s can submit proposal!', 'gigneo' ),
			strtolower( $freelancer_label )
		),
		'description' => sprintf(
			// translators: %s: Freelancer label.
			esc_html__( 'Create or use a %1$s to place orders.', 'gigneo' ),
			strtolower( $freelancer_label )
		),
	);
	gigneo_not_found( 'hire', $not_found_args );
	return;
}

if ( 'gigneo_job' !== $job_post_type ) {
		$not_found_args = array(
			'title'       => esc_html__( 'Job not found', 'gigneo' ),
			'description' => esc_html__( 'The job you are looking for does not exist.', 'gigneo' ),
		);
		gigneo_not_found( 'hire', $not_found_args );
		return;
}

if ( 'publish' !== $job_status ) {
	$not_found_args = array(
		'title'       => esc_html__( 'Job is not available', 'gigneo' ),
		'description' => esc_html__( 'The job you are looking for is not available.', 'gigneo' ),
	);
	gigneo_not_found( 'hire', $not_found_args );

	return;
}

if ( empty( $proposal_id ) ) {
	$existing_proposals = get_posts(
		array(
			'post_type'      => 'gigneo_proposal',
			'posts_per_page' => 1,
			'post_status'    => 'any',
                // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
			'meta_query'     => array(
				'relation' => 'AND',
				array(
					'key'   => '_gigneo_pm_job_id',
					'value' => $job_id,
				),
				array(
					'key'   => '_gigneo_pm_profile_id',
					'value' => $profile_id,
				),
			),
                // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
		)
	);
	if ( ! empty( $existing_proposals ) ) {
		$existing_proposal = $existing_proposals[0];
		if ( 'publish' === $existing_proposal->post_status ) {
			$proposal_id = $existing_proposal->ID;
		}
	}
}

$proposal_profile_id = gigneo_post_meta( $proposal_id, '_gigneo_pm_profile_id' );

$template_args = array(
	'p_id'        => $job_id,
	'profile_id'  => $profile_id,
	'proposal_id' => (int) $proposal_profile_id === $profile_id ? $proposal_id : '',
);
?>
<div class="gn-hire gn-hire-job gn-hire-job-fixed">
	<div class="gn-hire-details">
		<form id="gn-hire-job-form" class="gn-hire-job-form">
			<div class="gn-main">
				<?php
				//Job Terms
				gigneo_load_template( 'single/hire/job/parts/terms', true, $template_args );

				//Cover Letter
				gigneo_load_template( 'single/hire/job/parts/letter', true, $template_args );

				//Attachments
				gigneo_load_template( 'single/hire/job/parts/attachments', true, $template_args );
				//Requirements
				if ( in_array( 'intake_form', gigneo_active_modules(), true ) ) {
					gigneo_load_template( 'single/hire/job/parts/questions', true, $template_args );
				}

				if ( $proposal_id && $proposal_profile_id === $profile_id ) {
					?>
					<input type="hidden" name="proposal_id" value="<?php echo esc_attr( $proposal_id ); ?>"/>
					<button type="submit" class="gn-btn gn-btn-primary">
						<?php echo esc_html__( 'Update proposal', 'gigneo' ); ?>
					</button>
					<?php
				} else {
					?>
					<div class="gn-apply-footer">
						<button type="button" class="gn-btn gn-btn-primary" data-gn-modal-toggle="gn-proposal-publish">
							<?php echo esc_html__( 'Submit proposal', 'gigneo' ); ?>
						</button>
					</div>
					<?php
				}
				?>
			</div>
			<div class="gn-aside">
				<?php
				//Summary
				gigneo_load_template( 'single/hire/job/parts/info', true, $template_args );
				?>
			</div>
			<?php
			if ( ( ! empty( $profile_id ) && 'freelancer' === $profile_type )
					||
					( $proposal_id && $proposal_profile_id === $profile_id ) ) {
				gigneo_load_template( 'single/hire/job/parts/submit-proposal', true, $template_args );
			}
			?>
		</form>
	</div>
</div>