<?php
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
if (!defined('ABSPATH')) {
    exit;
} // Exit if accessed directly

class kavehwoostatus extends \Elementor\Widget_Base
{

    public function get_name(){
        return 'kavehwoostatus';
      }
    
      public function get_title(){
        return esc_html__( 'وضعیت موجودی صفحه تکی محصول', 'kavehTheme' );
      }
    
      public function get_icon(){
        return 'eicon-archive-posts';
      }
    
      protected function _register_controls() {
        // Widget Controls
        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Content', 'text-domain'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        // Controls for Icons
        $this->add_control(
            'icon_in_stock',
            [
                'label' => __('In Stock Icon', 'text-domain'),
                'type' => \Elementor\Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fas fa-check-circle',
                    'library' => 'fa-solid',
                ],
            ]
        );

        $this->add_control(
            'icon_out_of_stock',
            [
                'label' => __('Out of Stock Icon', 'text-domain'),
                'type' => \Elementor\Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fas fa-times-circle',
                    'library' => 'fa-solid',
                ],
            ]
        );

        $this->add_control(
            'icon_backorder',
            [
                'label' => __('On Backorder Icon', 'text-domain'),
                'type' => \Elementor\Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fas fa-truck-loading',
                    'library' => 'fa-solid',
                ],
            ]
        );

        $this->add_control(
            'icon_preorder',
            [
                'label' => __('Pre-order Icon', 'text-domain'),
                'type' => \Elementor\Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fas fa-calendar-check',
                    'library' => 'fa-solid',
                ],
            ]
        );

        // Controls for Text
        $this->add_control(
            'text_in_stock',
            [
                'label' => __('In Stock Text', 'text-domain'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => __('In Stock', 'text-domain'),
            ]
        );

        $this->add_control(
            'text_out_of_stock',
            [
                'label' => __('Out of Stock Text', 'text-domain'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => __('Out of Stock', 'text-domain'),
            ]
        );

        $this->add_control(
            'text_backorder',
            [
                'label' => __('On Backorder Text', 'text-domain'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => __('On Backorder', 'text-domain'),
            ]
        );

        $this->add_control(
            'text_preorder',
            [
                'label' => __('Pre-order Text', 'text-domain'),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => __('Pre-order', 'text-domain'),
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        if (!class_exists('woocommerce')) {
            return;
        }
        global $product;
        if (!$product) {
            $product = wc_get_product(get_the_ID());
        }
        if (!$product) return;

        $stock_status = $product->get_stock_status();
        $settings = $this->get_settings_for_display();

        // Set icons and texts based on stock status
        switch ($stock_status) {
            case 'instock':
                $icon = $settings['icon_in_stock'];
                $text = $settings['text_in_stock'];
                $class = 'in-stock';
                break;
            case 'outofstock':
                $icon = $settings['icon_out_of_stock'];
                $text = $settings['text_out_of_stock'];
                $class = 'out-of-stock';
                break;
            case 'onbackorder':
                $icon = $settings['icon_backorder'];
                $text = $settings['text_backorder'];
                $class = 'on-backorder';
                break;
            case 'preorder':
                $icon = $settings['icon_preorder'];
                $text = $settings['text_preorder'];
                $class = 'pre-order';
                break;
            default:
                $icon = '';
                $text = __('Unknown', 'text-domain');
                $class = 'unknown';
                break;
        }

        ?>

        <div class="product-stock-status <?php echo esc_attr($class); ?>">
            <button class="stock-status-button">
                <?php if (!empty($icon['value'])) : ?>
                    <i class="<?php echo esc_attr($icon['value']); ?>" aria-hidden="true"></i>
                <?php endif; ?>
                <span><?php echo esc_html($text); ?></span>
            </button>
        </div>
        <style>
        .product-stock-status .stock-status-button {
            display: inline-flex;
            align-items: center;
            gap: 5px;
            padding: 6px 12px;
            border: none;
            border-radius: 12px;
            font-size: 14px;
            font-weight: bold;
            color: #fff;
            cursor: default;
        }
        .product-stock-status.in-stock .stock-status-button {
            background-color:rgba(40, 167, 70, 0.25);
            color: #28a745;
        }
        .product-stock-status.out-of-stock .stock-status-button {
            background-color:rgba(220, 53, 70, 0.25);
            color: #dc3545;

        }
        .product-stock-status.on-backorder .stock-status-button {
            background-color:rgba(0, 123, 255, 0.25);
            color: #007bff;

        }
        .product-stock-status.pre-order .stock-status-button {
            background-color:rgba(0, 123, 255, 0.25);
            color: #007bff;
        }
        .product-stock-status .stock-status-button i {
            font-size: 16px;
        }
        </style>

        <?php
    }
   
}

