<?php

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

$service_id     = $args['service_id'] ?? 0;
$dashboard_url  = $args['dashboard_url'] ?? 0;
$featured_limit = $args['featured_limit'] ?? 0;
$date_format    = $args['date_format'] ?? get_option( 'date_format' );

$service_title  = get_the_title( $service_id );
$service_link   = get_permalink( $service_id );
$service_status = get_post_status( $service_id );

$service_status       = 'publish' === $service_status ? 'active' : $service_status;
$service_status_label = gigneo_status_label( $service_status );
$service_date         = get_post_time( 'Y-m-d H:i:s', true );
$service_price        = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_price' );
$service_thumbnail_id = get_post_thumbnail_id( $service_id );
$service_gallery      = gigneo_post_meta( $service_id, '_gigneo_pm_gallery' );
$service_video_type   = gigneo_post_meta( $service_id, '_gigneo_pm_video_type' );
$service_edit_link    = gigneo_dashboard_url( 'service', 'edit', array( 'p_id' => $service_id ) );

//Video
$service_video_link = '';
if ( 'link' === $service_video_type ) {
	$service_video_link = gigneo_post_meta( $service_id, '_gigneo_pm_video_link' );
} else {
	$service_video      = gigneo_post_meta( $service_id, '_gigneo_pm_video' );
	$service_video_link = wp_get_attachment_url( $service_video ) ?? '';
}

$is_featured   = gigneo_post_meta( $service_id, '_gigneo_pm_featured' );
$active_orders = gigneo_service_orders_count( $service_id, array( 'publish', 'disputed' ) );

?>
<div class="gn-service-card">
	<div class="gn-head">
		<div class="gn-slick-slider gn-slick-theme gn-service-media-slider">
			<div class="gn-slick-slide">
				<figure class="gn-service-media">
					<?php gigneo_attachment_image_src( $service_thumbnail_id, 'large' ); ?>
					<?php if ( ! empty( $service_video_link ) ) { ?>
						<a href="<?php echo esc_url( $service_video_link ); ?>" data-gn-fancybox class="gn-play-video-toggle">
							<i class="hi hi-play" aria-hidden="true"></i>
						</a>
					<?php } ?>
				</figure>
			</div>
			<?php
			if ( ! empty( $service_gallery ) && is_array( $service_gallery ) ) {
				foreach ( $service_gallery as $gallery_item ) {
					if ( intval( $gallery_item ) === intval( $service_thumbnail_id ) ) {
						continue;
					}
					?>
					<div class="gn-slick-slide">
						<figure class="gn-service-media"><?php gigneo_attachment_image_src( $gallery_item, 'large' ); ?></figure>
					</div>
					<?php
				}
			}
			?>
		</div>
		<div class="gn-tags">
			<span class="gn-status-tag-fill <?php echo esc_attr( gigneo_status_class( $service_status ) ); ?>"><?php echo esc_html( $service_status_label ); ?></span>
			<?php if ( $is_featured ) { ?>
				<span class="gn-status-tag-fill gn-warning"><?php echo esc_html__( 'Featured', 'gigneo' ); ?></span>
			<?php } ?>
		</div>
	</div>
	<div class="gn-body">
		<h2 class="gn-title gn-two-line-ellipsis">
			<a href="<?php echo esc_url( $service_link ); ?>" target="_blank"><?php echo esc_html( $service_title ); ?></a>
		</h2>
		<div class="gn-meta">
			<div class="gn-price">
				<span class="gn-label"><?php echo esc_html__( 'Starting from', 'gigneo' ); ?></span>
				<strong class="gn-value"><?php echo esc_html( gigneo_price_format( $service_price, true ) ); ?></strong>
			</div>
			<div class="gn-orders">
				<span class="gn-label"><?php echo esc_html__( 'Active orders', 'gigneo' ); ?></span>
				<strong class="gn-value"><?php echo esc_html( $active_orders ?? 0 ); ?></strong>
			</div>
		</div>
	</div>
	<div class="gn-footer">
		<div class="gn-actions">
			<a href="<?php echo esc_url( gigneo_dashboard_url( 'service', 'edit', array( 'p_id' => $service_id ) ) ); ?>" class="gn-btn gn-btn-sm gn-btn-outline-grey">
				<i class="hi hi-pencil-edit" aria-hidden="true"></i>
				<?php echo esc_html__( 'Edit', 'gigneo' ); ?>
			</a>
			<?php if ( 'active' === $service_status ) { ?>
				<button class="gn-btn gn-btn-sm gn-btn-outline-grey gn-update-service-status" data-gn-status="paused" data-gn-id="<?php echo esc_attr( $service_id ); ?>">
					<i class="hi hi-pause" aria-hidden="true"></i>
					<?php echo esc_html__( 'Pause', 'gigneo' ); ?>
				</button>
			<?php } elseif ( 'paused' === $service_status ) { ?>
				<button class="gn-btn gn-btn-sm gn-btn-outline-grey gn-update-service-status" data-gn-status="publish" data-gn-id="<?php echo esc_attr( $service_id ); ?>">
					<i class="hi hi-play" aria-hidden="true"></i>
					<?php echo esc_html__( 'Active', 'gigneo' ); ?>
				</button>
			<?php } ?>
		</div>
		<div class="gn-dropdown-container">
			<button class="gn-btn gn-btn-sm gn-btn-outline-grey gn-dropdown-toggle" data-gn-target="#gn-service-action-dropdown-<?php echo esc_attr( $service_id ); ?>">
				<i class="hi hi-ellipsis-horizontal" aria-hidden="true"></i>
			</button>
			<div class="gn-dropdown-content gn-enter-top" id="gn-service-action-dropdown-<?php echo esc_attr( $service_id ); ?>">
				<ul class="gn-dropdown-menu">
					<li class="gn-dropdown-item">
						<a href="<?php echo esc_url( $service_link ); ?>" target="_blank">
							<i class="hi hi-eye" aria-hidden="true"></i>
							<span class="gn-title"><?php echo esc_html__( 'View service', 'gigneo' ); ?></span>
						</a>
					</li>
					<?php if ( 'draft' !== $service_status ) { ?>
					<li class="gn-dropdown-item">
						<span class="gn-draft-service" data-gn-id="<?php echo esc_attr( $service_id ); ?>">
							<i class="hi hi-arrow-right" aria-hidden="true"></i>
							<span class="gn-title"><?php echo esc_html__( 'Move to drafts', 'gigneo' ); ?></span>
						</span>
					</li>
						<?php
					}
					if ( in_array( $service_status, array( 'active', 'paused' ) ) ) {
						if ( $is_featured || $featured_limit > 0 ) {
							?>
							<li class="gn-dropdown-item">
						<span class="gn-featured-service"
								data-gn-featured="<?php echo ! esc_attr( $is_featured ); ?>"
								data-gn-id="<?php echo esc_attr( $service_id ); ?>">
							<i class="hi hi-star" aria-hidden="true"></i>
							<span class="gn-title"><?php echo $is_featured ? esc_html__( 'Remove featured', 'gigneo' ) : esc_html__( 'Featured', 'gigneo' ); ?></span>
						</span>
							</li>
							<?php
						}
					}
					?>
					<li class="gn-dropdown-item">
						<span class="gn-clone-service" data-gn-id="<?php echo esc_attr( $service_id ); ?>">
							<i class="hi hi-document" aria-hidden="true"></i>
							<span class="gn-title"><?php echo esc_html__( 'Use as template', 'gigneo' ); ?></span>
						</span>
					</li>
					<li class="gn-dropdown-item">
						<span class="gn-remove-service" data-gn-id="<?php echo esc_attr( $service_id ); ?>">
							<i class="hi hi-trash" aria-hidden="true"></i>
							<span class="gn-title"><?php echo esc_html__( 'Delete service', 'gigneo' ); ?></span>
						</span>
					</li>
				</ul>
			</div>
		</div>
	</div>
</div>