<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$proposal_id = $args['p_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", $proposal_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;
		}
	}
}
if ( empty( $attachments ) ) {
	return;
}
?>
<div class="gn-section gn-attachments-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php echo esc_html__( 'Attachments', 'gigneo' ); ?></h3>
	</div>
	<div class="gn-body">
		<ul class="gn-attachment-list">
			<?php
			foreach ( $attachments as $attachment ) {
				$file_url  = gigneo_upload_dir_url( $attachment->path );
				$file_name = $attachment->name;
				$file_size = size_format( $attachment->size, 2 );
				?>
				<li class="gn-attachment">
					<i class="hi hi-document" aria-hidden="true"></i>
					<span class="gn-info">
						<span class="gn-name"><?php echo esc_html( $file_name ); ?></span>
						<span class="gn-size"><?php echo esc_html( $file_size ); ?></span>
					</span>
					<a href="<?php echo esc_url( $file_url ); ?>" download class="gn-btn gn-btn-download">
						<i class="hi hi-arrow-down-tray"></i>
					</a>
				</li>
				<?php
			}
			?>
		</ul>
	</div>
</div>
