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

$p_id                  = $args['p_id'] ?? 0;
$job_id                = gigneo_post_meta( $p_id, '_gigneo_pm_job_id' );
$freelancer_profile_id = gigneo_post_meta( $p_id, '_gigneo_pm_profile_id' );
$employer_profile_id   = gigneo_post_meta( $p_id, '_gigneo_pm_employer_profile_id' );
$budget                = gigneo_post_meta( $p_id, '_gigneo_pm_budget' );
$duration              = gigneo_post_meta( $p_id, '_gigneo_pm_duration' );
$duration_type         = gigneo_post_meta( $p_id, '_gigneo_pm_duration_type' );
$cover_letter          = gigneo_post_meta( $p_id, '_gigneo_pm_cover_letter' );
$proposal_status       = get_post_status( $p_id );
$date_format           = get_option( 'date_format' );
$time_format           = get_option( 'time_format' );
$date_time_format      = sprintf( '%s %s', $date_format, $time_format );
$proposal_time         = get_post_time( $date_time_format, true, $p_id );
$status_class          = gigneo_status_class( $proposal_status );
$status_label          = gigneo_status_label( $proposal_status );
$is_shortlisted        = ( 'shortlisted' === $proposal_status );


$author_profile_name   = gigneo_profile_name( $freelancer_profile_id );
$employer_profile_name = gigneo_profile_name( $employer_profile_id );

global $wpdb;
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$attachments_raw = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}gigneo_attachments  WHERE post_id = %d AND post_type = %s ORDER BY uploaded_at DESC", $p_id, 'proposal' ) );
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$attachments = array();
if ( ! empty( $attachments_raw ) ) {
	foreach ( $attachments_raw as $attachment ) {
		$meta             = ! empty( $attachment->meta ) ? maybe_unserialize( $attachment->meta ) : array();
		$requirement_type = $meta['type'] ?? '';
		if ( 'requirement' !== $requirement_type ) {
			$attachments[] = $attachment;
		}
	}
}
?>
<div id="gn-proposal-overview-modal-<?php echo esc_attr( $p_id ); ?>" class="gn-modal-wrapper gn-proposal-overview-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 esc_html__( 'Proposal overview', 'gigneo' ); ?></h4>
					<p class="gn-description"><?php echo esc_html__( 'You can view proposal details here.', 'gigneo' ); ?></p>
					<button type="button" data-gn-modal-close="gn-proposal-overview-modal-<?php echo esc_attr( $p_id ); ?>" class="gn-modal-close">
						<i class="hi hi-x-mark" aria-hidden="true"></i>
					</button>
				</div>
				<div class="gn-modal-body gn-custom-scrollbar">
					<div class="gn-proposal-overview-section">
						<div class="gn-heading-area">
							<h5 class="gn-title"><?php echo esc_html__( 'Proposal Details', 'gigneo' ); ?></h5>
							<span class="gn-date">
							<?php
							printf(
									// translators: %s is the proposal submission date and time.
								esc_html__( 'Submitted at %s', 'gigneo' ),
								esc_html( gigneo_datetime( $proposal_time ) )
							);
							?>
							</span>
						</div>
						<div class="gn-proposal-info">
							<div class="gn-proposal-title">
								<a href="<?php echo esc_url( get_edit_post_link( $job_id ) ); ?>" target="_blank">
									<?php echo esc_html( get_the_title( $job_id ) ); ?>
								</a>
								<span class="gn-status-tag <?php echo esc_attr( $status_class ); ?>">
								<?php
								if ( $is_shortlisted ) {
									echo esc_html__( 'Shortlisted', 'gigneo' );
								} else {
									echo esc_html( $status_label );
								}
								?>
								</span>
							</div>
							<div class="gn-info-grid-wrap">
								<div class="gn-info-grid">
									<h5 class="gn-label"><?php echo esc_html( gigneo_freelancer_singular() ); ?></h5>
									<p class="gn-value">
										<?php if ( ! empty( $freelancer_profile_id ) ) { ?>
											<a href="<?php echo esc_url( get_edit_post_link( $freelancer_profile_id ) ); ?>" target="_blank">
												<?php echo esc_html( $author_profile_name ); ?>
												<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
											</a>
										<?php } else { ?>
											<?php echo esc_html( $author_profile_name ); ?>
										<?php } ?>
									</p>
								</div>
								<div class="gn-info-grid">
									<h5 class="gn-label"><?php echo esc_html( gigneo_employer_singular() ); ?></h5>
									<p class="gn-value">
										<?php if ( ! empty( $employer_profile_id ) ) { ?>
											<a href="<?php echo esc_url( get_edit_post_link( $employer_profile_id ) ); ?>" target="_blank">
												<?php echo esc_html( $employer_profile_name ); ?>
												<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
											</a>
										<?php } else { ?>
											<?php echo esc_html( $employer_profile_name ); ?>
										<?php } ?>
									</p>
								</div>
								<div class="gn-info-grid">
									<h5 class="gn-label"><?php echo esc_html__( 'Bid Amount', 'gigneo' ); ?></h5>
									<p class="gn-value"><?php echo esc_html( gigneo_price_format( $budget, true ) ); ?></p>
								</div>
								<div class="gn-info-grid">
									<h5 class="gn-label"><?php echo esc_html__( 'Duration', 'gigneo' ); ?></h5>
									<p class="gn-value"><?php echo esc_html( gigneo_delivery_time( $duration, $duration_type ) ); ?></p>
								</div>
							</div>
							<?php if ( ! empty( $cover_letter ) ) { ?>
								<div class="gn-proposal-description">
									<h5 class="gn-label"><?php echo esc_html__( 'Cover letter', 'gigneo' ); ?></h5>
									<pre class="gn-pre"><?php echo wp_kses( $cover_letter, gigneo_allowed_html_tags() ); ?></pre>
								</div>
							<?php } ?>
							<?php if ( ! empty( $attachments ) ) { ?>
								<div class="gn-proposal-attachments">
									<h5 class="gn-label"><?php echo esc_html__( 'Attachments', 'gigneo' ); ?></h5>
									<ul class="gn-attachment-list">
										<?php
										foreach ( $attachments as $attachment ) {
											$attachment_url  = gigneo_upload_dir_url( $attachment->path );
											$attachment_name = $attachment->name;
											if ( ! empty( $attachment_url ) ) {
												?>
											<li>
												<a href="<?php echo esc_url( $attachment_url ); ?>" target="_blank" download>
													<i class="hi hi-paper-clip" aria-hidden="true"></i>
													<span class="gn-name"><?php echo esc_html( $attachment_name ); ?></span>
													<i class="hi hi-arrow-down-tray" aria-hidden="true"></i>
												</a>
											</li>
												<?php
											}
										}
										?>
									</ul>
								</div>
							<?php } ?>
							<?php
							gigneo_load_template(
								'admin/parts/proposal/questions',
								true,
								array(
									'p_id'   => $p_id,
									'job_id' => $job_id,
								)
							);
							?>
						</div>
					</div>
				</div>
				<div class="gn-modal-footer">
					<button type="button" class="gn-btn gn-btn-outline-grey" data-gn-modal-close="gn-proposal-overview-modal-<?php echo esc_attr( $p_id ); ?>"><?php echo esc_html__( 'Close', 'gigneo' ); ?></button>
				</div>
			</div>
		</div>
	</div>
</div>
