 <?php
 /**
 * =============================================================================
 * @package         Blue Raven (Free Version)
 * @Plugin URI      https://enterrahost.com/plugins/blue-raven
 * 
 * @Author          EnterraHost
 * @Author URI      https://enterrahost.com
 * @License         GPL-2.0-or-later
 *  
 * @Copyright       (c) 2025 EnterraHost
 * @Contents        Product Table List item
 *
 * ============================================================================= 
 * || INDEX:
 * ||   - BUILD THE PRODUCT TABLE LIST ITEM
 * || 
 * =============================================================================
 */
 
if ( ! defined( 'ABSPATH' ) ) exit; 

/*
============================================================
    BUILD THE PRODUCT TABLE LIST ITEM
============================================================
*/

$woo_product_id = $product['woo_product_id'] ?? '';
$skuValue = trim($product['sku'] ?? ''); 
$SKU = "SKU : " . ($skuValue !== '' ? $skuValue : 'None');
$status = $product['status'] ?? 'Error';
$pending = $product['pending'] ?? 'false';
$cat = $product['category'] ?? 'Uncategorized';
$placeholder_url = BLUERAVEN_PLUGIN_URL . 'assets/media/blue_raven_place_holder.webp';
$thumbnail = $product['thumbnail'] ?? '';
$title = $product['title'] ?? 'Product image';
$isPlaceholder = empty($thumbnail);
$imageSrc = $isPlaceholder ? $placeholder_url : $thumbnail;
$imageStyle = $isPlaceholder ? 'width:100px; height:100px;' : 'width:140px; height:140px; object-fit: contain;';

$status_indicator = '';

switch ($status) {
    case 'pending':
        $status_indicator = 'bri-pending';
        break;
    case 'draft':
        $status_indicator = 'bri-draft';
        break;
    case 'issue':
        $status_indicator = 'bri-issue';
        break;
    case 'published':
        $status_indicator = 'bri-blue-publish';
        break;
    case 'woo':
        $status_indicator = 'bri-woo';
        break;
    default:
        $status_indicator = 'bri-blue';
        break;
}

?>

<div class="blue-raven-product flex-sb" style="margin: 3rem 0rem 2rem 0; padding: 0.5rem 0 0 0; border-bottom: 1px solid #6e6e6e; min-height: 170px;">
    <div style="width:150px; height:150px; display:flex; align-items:center; justify-content:center; margin-right: 1rem;">
        <img class="blue-raven-product-thumbnail" src="<?php echo esc_url($imageSrc); ?>" alt="<?php echo esc_attr($title); ?>" style="<?php echo esc_attr($imageStyle); ?>" >
    </div>
    
    <div class="blue-raven-product-details" style="width: calc(100% - 9rem); position: relative">
        <div class="blue-raven-product-header flex-sb" style="border-bottom: 1px solid #6e6e6e; min-height: 52px">
            <h4 class="blue-raven-product-title" style="margin: 8px 0;"><?php echo esc_html($product['title']); ?></h4> 
            <div class="blue-raven-product-actions flex-sb" style='align-items: end;'>
                <p class="blue-raven-product-stock" style="font-size: 0.9rem; margin-right: 1rem; width: 7rem; align-content: center; height: 2rem;">
                    <?php
                        if (!empty($woo_product_id)) {
                            $wc_product = wc_get_product($woo_product_id);
                        
                            if ($wc_product) {
                                $stock_quantity = $wc_product->get_stock_quantity();
                                $is_in_stock = $wc_product->is_in_stock();
                                $get_stock_status = $wc_product->get_stock_status();
                        
                                if (($is_in_stock && $stock_quantity > 0) || $get_stock_status == 'instock') {
                                    echo '<span style="color: #8ae08a; text-align: right; display: block;">' . esc_html__('In Stock', 'blue-raven') . '</span>';
                                } else {
                                    echo '<span style="color: #e08a8a; text-align: right; display: block;">' . esc_html__('Out of Stock', 'blue-raven') . '</span>';
                                }
                            }
                        }
                    ?>
                </p>
                <div class="blue-raven-product-favorite" style='align-content: center; padding-bottom: 4px; height: 2rem;'>
                    <input type="checkbox" id="star-<?php echo absint($product['id']); ?>" class="br-star-checkbox favorite-star" name="star" data-id="<?php echo absint($product['id']); ?>" <?php checked( $product['favorite'], 1 ); ?>>
                    <label for="star-<?php echo absint($product['id']); ?>" class="br-star-label" style=>★</label>
                </div>
                <p class="blue-raven-product-view" style="font-size: 0.9rem; width: 4rem; text-align: end; align-content: center; height: 2rem; margin-right: 0.5rem;">
                    <?php if ($product['woo_product_id'] != '' && $product['status'] == 'draft'): ?>
                        <a class="clickable" href="<?php echo esc_url(add_query_arg(['post_type' => 'product','p' => absint($product['woo_product_id']),'preview' => 'true'], home_url('/'))); ?>">
                            <?php esc_html_e('Preview', 'blue-raven'); ?>
                        </a>
                    
                    <?php elseif ( $product['woo_product_id'] != '' && in_array($product['status'], ['issue', 'woo', 'published'], true) ): ?>
                        <a class="clickable" href="<?php echo esc_url(get_permalink(absint($product['woo_product_id']))); ?>">
                            <?php esc_html_e('View', 'blue-raven'); ?>
                        </a>
                    <?php endif; ?>
                </p>
            </div>
        </div>
        
        <div class="blue-raven-product-content">
            <div class="blue-raven-product-info flex-sb" style="height: 100px;">
                <div class="blue-raven-product-meta" style="display: flex; flex-direction: column; justify-content: space-between; height: 100%; margin-left: 0;">
                    <p class="blue-raven-product-sku" style="font-size: 0.9rem; margin-right: 0.5rem; margin-top: 0.2rem">
                        <?php echo esc_html($SKU); ?>
                    </p>
                    <p class="blue-raven-product-category" style="font-size: 0.9rem; margin-right: 0.5rem; color: #ababab">
                        <?php echo esc_html($cat); ?>
                    </p>
                    <div class="blue-raven-product-type flex-left">
                        <p style="font-size: 0.9rem; align-items: center; margin-right: 0.5rem;" class="flex-left">
                            <span class="br-product-indicator <?php echo esc_attr($status_indicator); ?> bri-blue"></span> 
                            <?php echo esc_html($status); ?>
                        </p>
                    </div>
                </div>
                <div class="blue-raven-product-secondary-actions" style="display: flex; flex-direction: column; justify-content: space-between; height: 100%; margin-left: 3rem;">
                    <p class="blue-raven-product-edit" style="font-size: 0.9rem; text-align: end; margin-right: 0.5rem; margin-top: 0.25rem;">
                        <a class="clickable" href="<?php echo esc_url(admin_url('admin.php?page=blue-raven&product=' . absint($product['id']))); ?>">
                            <?php esc_html_e('Edit', 'blue-raven'); ?>
                        </a>
                    </p>
                    <div class="blue-raven-product-more-actions flex-right" style='margin-bottom: -0.5rem;'>
                        <!-- <p class="blue-raven-product-statistics" style="font-size: 0.9rem; margin-left: 3rem; text-align: end; align-self: center;">
                            <a class="clickable" id='view-statistics'><?php esc_html_e('Statistics', 'blue-raven'); ?></a>
                        </p>
                        <p class="blue-raven-product-versions" style="font-size: 0.9rem; margin-left: 3rem; text-align: end; align-self: center;">
                            <a class="clickable" id='view-versions'><?php esc_html_e('Versions', 'blue-raven'); ?></a>
                        </p> -->
                        <label class="br-checkbox" style="width: 100%; height: 2rem; justify-content: end; margin-left: 3rem;">
                            <label for="delete-<?php echo absint($product['id']); ?>"><?php esc_html_e('Delete', 'blue-raven'); ?></label>
                            <div style="margin-left: 0.5rem; width: 2rem;">
                                <input class='delete-check' type="checkbox" name="deleteList[]" value="<?php echo absint($product['id']); ?>" style="display: none;" id="delete-<?php echo absint($product['id']); ?>">
                                <span class="br-check-mark deleteList"></span>
                            </div>
                        </label>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
