<?php

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;
$attachment_extensions = gigneo_option( '_gigneo_ts_job_order_extensions', array( 'jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx', 'mp4' ) );
$attachment_max_size   = gigneo_option( '_gigneo_ts_job_order_max_size', 5 );
$attachment_limit      = gigneo_option( '_gigneo_ts_job_order_max_files', 3 );
$template_args         = array(
	'job_id'      => $job_id,
	'proposal_id' => $proposal_id,
);

if ( ! empty( $proposal_id ) ) {
	$attachments = '';
	global $wpdb;
	// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
	$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}gigneo_attachments WHERE post_id = %d AND post_type = %s ORDER BY uploaded_at DESC", $proposal_id, 'proposal' ) );
	// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
?>
<div class="gn-section gn-attachment-section">
	<div class="gn-head">
		<div class="gn-heading-area">
			<h3 class="gn-title">
				<?php
				echo esc_html__( 'Attachments', 'gigneo' );
				?>
			</h3>
			<p class="gn-description">
				<?php echo esc_html__( 'Share files that help explain the work.', 'gigneo' ); ?>
			</p>
		</div>
	</div>
	<div class="gn-body">
		<div class="gn-form-fields">
			<div class="gn-form-field gn-plupload gn-plupload-style-full">
				<div id="gn-proposal-attachment-container" class="gn-plupload-container">
					<div id="gn-proposal-attachment-drop-area" class="gn-plupload-drop-area">
						<i class="hi hi-cloud-arrow-up" aria-hidden="true"></i>
						<div class="gn-action-info">
							<p class="gn-message"><?php echo esc_html__( 'Upload your proposal attachments here.', 'gigneo' ); ?></p>
							<p class="gn-note">
								<?php
								printf(
								// translators: %1$s: Valid extensions %2$s Maximum files %3$s Maximum file size.
									esc_html__( '%1$s — Up to %2$s files (Max. %3$s MB)', 'gigneo' ),
									esc_html( strtoupper( implode( ',', $attachment_extensions ) ) ),
									esc_html( $attachment_limit ),
									esc_html( $attachment_max_size )
								);
								?>
							</p>
							<button id="gn-proposal-attachment-uploader" type="button" class="gn-upload"><?php echo esc_html__( 'Click here to upload', 'gigneo' ); ?></button>
						</div>
					</div>
					<div id="gn-proposal-attachment-list" class="gn-plupload-attachments">
						<?php if ( ! empty( $attachments ) ) { ?>
							<?php
							foreach ( $attachments as $attachment ) {
								$file_url  = gigneo_upload_dir_url( $attachment->path );
								$file_name = $attachment->name;
								?>
								<div class="gn-attachment-item gn-existing"
									id="gn-attachment-<?php echo esc_attr( $attachment->id ); ?>">
									<div class="gn-plupload-file-name"><?php echo esc_html( $file_name ); ?></div>
									<div class="gn-plupload-file-remove">
										<button type="button" class="gn-attachment-remove"
												data-attachment-id="<?php echo esc_attr( $attachment->id ); ?>"
												data-post-id="<?php echo esc_attr( $proposal_id ); ?>">
											<i class="hi hi-trash"></i>
										</button>
									</div>
								</div>
							<?php } ?>
						<?php } ?>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>