<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
$order_id               = ! empty( $args['order_id'] ) ? absint( $args['order_id'] ) : 0;
$profile_id             = ! empty( $args['profile_id'] ) ? absint( $args['profile_id'] ) : 0;
$participant_profile_id = ! empty( $args['participant_profile_id'] ) ? absint( $args['participant_profile_id'] ) : 0;
$profile_type           = ! empty( $args['profile_type'] ) ? $args['profile_type'] : 0;

$order_status         = get_post_status( $order_id );
$job_order_extensions = gigneo_option( '_gigneo_ts_job_order_extensions', array( 'jpg', 'jpeg', 'png', 'doc' ) );
$job_order_extensions = is_array( $job_order_extensions ) ? $job_order_extensions : array( 'jpg', 'jpeg', 'png', 'doc' );
$job_order_max_files  = gigneo_option( '_gigneo_ts_job_order_max_files', '3' );
$job_order_max_size   = gigneo_option( '_gigneo_ts_job_order_max_size', '1' );

$date_format = get_option( 'date_format' );
$time_format = get_option( 'time_format' );

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", $order_id, 'order' ) );
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$my_files = count(
	array_filter(
		$attachments,
		function ( $att ) use ( $profile_id ) {
			return isset( $att->profile_id ) && intval( $att->profile_id ) === $profile_id;
		}
	)
);

$participant_files = count(
	array_filter(
		$attachments,
		function ( $att ) use ( $participant_profile_id ) {
			return isset( $att->profile_id ) && intval( $att->profile_id ) === $participant_profile_id;
		}
	)
);

$attachment_types = array(
	'upload'      => esc_html__( 'Upload', 'gigneo' ),
	'requirement' => esc_html__( 'Requirement', 'gigneo' ),
	'deliverable' => esc_html__( 'Deliverable', 'gigneo' ),
);
?>
<div class="gn-order-attachments-wrapper">
	<?php if ( in_array( $order_status, array( 'publish', 'delivered' ), true ) ) { ?>
		<div id="gn-order-attachments-upload" class="gn-order-section gn-order-attachments-upload">
			<div class="gn-head">
				<h3 class="gn-title"><?php echo esc_html__( 'Upload files', 'gigneo' ); ?></h3>
			</div>
			<div class="gn-body">
				<div class="gn-form-field gn-plupload gn-plupload-style-full">
					<div id="gn-job-order-attachment-container" class="gn-plupload-container">
						<div id="gn-job-order-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 order 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( ',', $job_order_extensions ) ) ),
									esc_html( $job_order_max_files ),
									esc_html( $job_order_max_size )
								);
								?>
								</p>
								<button id="gn-job-order-attachment-uploader" type="button" class="gn-upload"><?php echo esc_html__( 'Click here to upload', 'gigneo' ); ?></button>
							</div>
						</div>
						<div id="gn-job-order-attachment-list" class="gn-plupload-attachments"></div>
						<div class="gn-actions">
							<button type="button" id="gn-job-order-attachments-cancel" class="gn-btn gn-btn-sm gn-btn-outline-grey"><?php echo esc_html__( 'Cancel', 'gigneo' ); ?></button>
							<button type="button" id="gn-job-order-attachments-upload" class="gn-btn gn-btn-sm gn-btn-primary" data-gn-order-id="<?php echo esc_attr( $order_id ); ?>"><?php echo esc_html__( 'Upload', 'gigneo' ); ?></button>
						</div>
					</div>
				</div>
			</div>
		</div>
	<?php } ?>
	<div id="gn-order-attachments" class="gn-order-section gn-order-attachments">
		<div class="gn-head">
			<div class="gn-heading-info">
				<h3 class="gn-title"><?php echo esc_html__( 'Attachments', 'gigneo' ); ?></h3>
			</div>
			<ul class="gn-attachment-filters">
				<li data-gn-filter="all" class="gn-active">
					<?php echo esc_html__( 'All', 'gigneo' ); ?>
					<span class="gn-count"><?php echo esc_html( count( $attachments ) ); ?></span>
				</li>
				<li data-gn-filter="my-files">
					<?php echo esc_html__( 'My files', 'gigneo' ); ?>
					<span class="gn-count"><?php echo esc_html( $my_files ); ?></span>
				</li>
				<li data-gn-filter="participant-files">
					<?php
					printf(
						// translators: %s is profile type
						esc_html__( 'By %s', 'gigneo' ),
						esc_html( 'freelancer' === $profile_type ? gigneo_employer_singular() : gigneo_freelancer_singular() )
					);
					?>
					<span class="gn-count"><?php echo esc_html( $participant_files ); ?></span>
				</li>
			</ul>
		</div>
		<div class="gn-body">
			<ul class="gn-attachment-list">
				<?php
				if ( ! empty( $attachments ) ) {
					foreach ( $attachments as $attachment ) {
						$attachment_path = gigneo_upload_dir_path( $attachment->path );
						$attachment_url  = gigneo_upload_dir_url( $attachment->path );
						if ( file_exists( $attachment_path ) ) {
							$attachment_meta       = ! empty( $attachment->meta ) ? maybe_unserialize( $attachment->meta ) : array();
							$attachment_type       = ! empty( $attachment_meta['type'] ) ? $attachment_meta['type'] : '';
							$attachment_type_label = ! empty( $attachment_types[ $attachment_type ] ) ? $attachment_types[ $attachment_type ] : '';
							?>
						<li class="gn-attachment gn-<?php echo esc_attr( absint( $attachment->profile_id ) === $profile_id ? 'my-files' : 'participant-files' ); ?>">
								<span class="gn-icon">
									<i class="hi hi-document-text" aria-hidden="true"></i>
								</span>
							<div class="gn-info">
								<h5 class="gn-title">
									<?php echo esc_html( $attachment->name ); ?>
									<?php
									if ( ! empty( $attachment_type_label ) ) {
										?>
										<span class="gn-status-tag gn-<?php echo esc_attr( $attachment_type ); ?>"><?php echo esc_html( $attachment_type_label ); ?></span>
									<?php } ?>
								</h5>
								<div class="gn-details">
										<?php
										if ( ! empty( $attachment->profile_id ) ) {
											$attachment_profile_image_id = get_post_thumbnail_id( $attachment->profile_id );
											?>
										<div class="gn-author">
											<figure class="gn-avatar">
												<?php gigneo_attachment_image_src( $attachment_profile_image_id, 'thumbnail', 'profile' ); ?>
											</figure>
											<h6 class="gn-name"><?php echo esc_html( gigneo_profile_name( $attachment->profile_id ) ); ?></h6>
										</div>
										<?php } ?>
										<?php if ( ! empty( $attachment->size ) ) { ?>
										<span class="gn-size">
											<i class="hi hi-server-stack" aria-hidden="true"></i>
											<?php
											printf(
												// translators: %s is attachment size in MB
												esc_html__( '%s MB', 'gigneo' ),
												esc_html( number_format( $attachment->size / ( 1024 * 1024 ), 2 ) )
											);
											?>
										</span>
									<?php } ?>
										<?php
										if ( ! empty( $attachment->uploaded_at ) ) {
											$datetime_format = $date_format . ' ' . $time_format;
											$upload_date     = gigneo_datetime( $attachment->uploaded_at_gmt, $datetime_format );
											?>
										<span class="gn-date"><i class="hi hi-calendar" aria-hidden="true"></i><?php echo esc_html( $upload_date ); ?></span>
										<?php } ?>
								</div>
							</div>
							<div class="gn-actions">
								<a href="<?php echo esc_url( $attachment_url ); ?>" class="gn-btn gn-btn-download" download>
									<i class="hi hi-arrow-down-tray" aria-hidden="true"></i>
								</a>
							</div>
						</li>
							<?php
						}
					}
				} else {
					gigneo_not_found( 'order_job_attachments' );
				}
				?>
			</ul>
		</div>
	</div>
</div>
