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

$p_id              = $args['p_id'] ?? '';
$redirect_url      = $args['redirect_url'] ?? '';
$profile_id        = gigneo_active_profile_id();
$available_credits = gigneo_available_credits( $profile_id );
$creation_type     = gigneo_option( '_gigneo_ts_job_creation_type', 'paid' );
$creation_credits  = gigneo_option( '_gigneo_ts_job_creation_credits', '10' );
$manual_approval   = gigneo_option( '_gigneo_ts_job_manually_approve' );
$privacy_page_id   = gigneo_option( '_gigneo_ts_job_privacy_policy' );
$terms_page_id     = gigneo_option( '_gigneo_ts_job_terms_and_condition' );
$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' );
$pm_status         = gigneo_post_meta( $p_id, '_gigneo_pm_status' );
$credit_invoice_id = gigneo_post_meta( $p_id, '_gigneo_pm_credit_invoice_id' );
?>
<!--Publish Job -->
<div id="gn-job-publish" class="gn-modal-wrapper gn-job-publish-modal">
	<div class="gn-modal-dialog">
		<div class="gn-modal-content">
			<div class="gn-modal-info">
				<div class="gn-modal-header">
					<h4 class="gn-title"><?php echo ( empty( $pm_status ) && $manual_approval ) ? esc_html__( 'Submit for Review', 'gigneo' ) : esc_html__( 'Publish Job', 'gigneo' ); ?></h4>
					<p class="gn-description"><?php echo ( empty( $pm_status ) && $manual_approval ) ? esc_html__( 'Once the admin approve, it will be visible.', 'gigneo' ) : esc_html__( 'You’re about to publish this job.', 'gigneo' ); ?></p>
					<button type="button" data-gn-modal-close="gn-job-publish" class="gn-modal-close">
						<i class="hi hi-x-mark" aria-hidden="true"></i>
					</button>
				</div>
				<form id="gn-job-publish-form" class="gn-theme-form gn-job-publish-form">
					<div class="gn-modal-body gn-custom-scrollbar">
						<?php if ( 'paid' === $creation_type && empty( $credit_invoice_id ) ) { ?>
							<div class="gn-credits-info">
								<div class="gn-heading-area">
									<h6 class="gn-title">
										<i class="hi hi-wallet" aria-hidden="true"></i>
										<?php
										printf(
											// translators: %s credits
											esc_html__( '%s Credits required', 'gigneo' ),
											esc_html( $creation_credits )
										);
										?>
									</h6>
									<p class="gn-description">
										<?php
										printf(
											// translators: %s credits
											esc_html__( '%s credits will be deducted form your wallet to publish this job.', 'gigneo' ),
											esc_html( $creation_credits )
										);
										?>
									</p>
								</div>
								<div class="gn-total-credits">
									<span class="gn-label"><?php echo esc_html__( 'Available credits', 'gigneo' ); ?></span>
									<span class="gn-value"><?php echo esc_html( $available_credits ); ?></span>
								</div>
								<?php if ( $available_credits > intval( $creation_credits ) ) { ?>
									<div class="gn-remaining-credits">
										<span class="gn-label"><?php echo esc_html__( 'Remaining balance', 'gigneo' ); ?></span>
										<span class="gn-value"><?php echo esc_html( $available_credits - $creation_credits ); ?></span>
									</div>
								<?php } ?>
							</div>
						<?php } ?>
						<fieldset class="gn-form-fields">
							<div class="gn-form-field">
								<div class="gn-form-field-checkbox">
									<input id="gn-term-and-privacy" type="checkbox" class="gn-required-field" name="term_and_privacy" />
									<label for="gn-term-and-privacy">
									<?php
										printf(
										// translators: 1. Terms and Condition URL, 2. Terms and Condition Title, 3. Privacy Policy URL, 4. Privacy Policy Title.
											wp_kses_post( __( 'I agree to the <a href="%1$s">%2$s</a> & <a href="%3$s">%4$s</a>', 'gigneo' ) ),
											esc_url( $terms_url ),
											esc_html( $terms_title ),
											esc_url( $privacy_url ),
											esc_html( $privacy_title )
										);
										?>
										</label>
								</div>
							</div>
						</fieldset>
					</div>
					<div class="gn-modal-footer">
						<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-job-publish"><?php echo esc_html__( 'Close', 'gigneo' ); ?></button>
						<button type="submit" data-gn-redirect-url="<?php echo esc_url( $redirect_url ); ?>" class="gn-btn gn-btn-primary" data-gn-type="publish">
							<?php
							if ( empty( $pm_status ) && $manual_approval ) {
								echo esc_html__( 'Submit', 'gigneo' );
							} else {
								echo esc_html__( 'Publish', 'gigneo' );
							}
							?>
						</button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
