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

/**
 * Elementor oEmbed Widget.
 *
 * Elementor widget that inserts an embbedable content into the page, from any given URL.
 *
 * @since 1.0.0
 */
class Elementor_cart_widget_Widget extends \Elementor\Widget_Base {

/**
 * Get widget name.
 *
 * Retrieve oEmbed widget name.
 *
 * @return string Widget name.
 * @since 1.0.0
 * @access public
 */
public function get_name()
{
    return 'hyperkala_cart_widget';
}

/**
 * Get widget title.
 *
 * Retrieve oEmbed widget title.
 *
 * @return string Widget title.
 * @since 1.0.0
 * @access public
 */
public function get_title()
{
    return 'سبد خرید';
}

/**
 * Get widget icon.
 *
 * Retrieve oEmbed widget icon.
 *
 * @return string Widget icon.
 * @since 1.0.0
 * @access public
 */
public function get_icon()
{
    return 'eicon-single-product';
}

/**
 * Get custom help URL.
 *
 * Retrieve a URL where the user can get more information about the widget.
 *
 * @return string Widget help URL.
 * @since 1.0.0
 * @access public
 */
public function get_custom_help_url()
{
    return 'https://developers.elementor.com/docs/widgets/';
}

/**
 * Get widget categories.
 *
 * Retrieve the list of categories the oEmbed widget belongs to.
 *
 * @return array Widget categories.
 * @since 1.0.0
 * @access public
 */
public function get_categories()
{
    return ['hyperkala'];
}

/**
 * Get widget keywords.
 *
 * Retrieve the list of keywords the oEmbed widget belongs to.
 *
 * @return array Widget keywords.
 * @since 1.0.0
 * @access public
 */
public function get_keywords()
{
    return ['oembed', 'url', 'link'];
}

/**
 * Register oEmbed widget controls.
 *
 * Add input fields to allow the user to customize the widget settings.
 *
 * @since 1.0.0
 * @access protected
 */
    function get_product_cat(){
        $terms = get_terms( array(
            'taxonomy' => 'product_cat',
            'hide_empty' => true,
        ));

        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
            foreach ( $terms as $term ) {
                $options[ $term->term_id ] = $term->name;
            }
            return $options;
        }
    }
protected function register_controls()
{

    $this->start_controls_section(
        'content_section',
        [
            'label' => 'تنظیمات محتوا',
            'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
        ]
    );
    $this->add_control(
        'section_title',
        [
            'label' => 'عنوان بخش',
            'type' => \Elementor\Controls_Manager::TEXT,
            'placeholder' => 'برای مثال : آخرین محصولاتی که خریده‌اید',
        ]
    );

    $this->end_controls_section();
    $this->start_controls_section(
        'style_section',
        [
            'label' => 'تنظیمات ظاهر',
            'tab' => \Elementor\Controls_Manager::TAB_STYLE,
        ]
    );
    $this->add_control(
        'last_buy_bg_color',
        [
            'label' => 'رنگ پس زمینه',
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .the-ajax-cart-element' => 'background-color: {{VALUE}}',
            ],
        ]
    );
    $this->add_control(
        'last_buy_title_color',
        [
            'label' => 'رنگ عنوان',
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .the-ajax-cart-element-top' => 'color: {{VALUE}}',
            ],
        ]
    );
    $this->add_control(
        'last_buy_bb_color',
        [
            'label' => 'رنگ خط زیر عنوان',
            'type' => \Elementor\Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .the-ajax-cart-element-top' => 'border-bottom-color: {{VALUE}}',
            ],
        ]
    );
    $this->end_controls_section();
}

/**
 * Render oEmbed widget output on the frontend.
 *
 * Written in PHP and used to generate the final HTML.
 *
 * @since 1.0.0
 * @access protected
 */
protected function render() {
$settings = $this->get_settings_for_display();
?>
    <div class="the-ajax-cart-element">
        <div class="the-ajax-cart-element-top"><?php echo $settings['section_title'];?></div>
        <?php
        global $woocommerce;
        $items = WC()->cart->total;
        ?>
        <ul>
            <?php
            foreach( $items as $item => $values ) {
                $product = wc_get_product($values['data']->get_id());
                $quan = $product->get_stock_quantity();
                ?>
                <a href="<?php echo $product->get_permalink();?>"><li><?php echo $product->get_image();?>
                        <div><p><?php echo $product->get_title();?></p>
                            <?php if ($product->is_in_stock()){?>
                                <?php if (!empty($quan) && $quan<10){?>
                                    <div><?php echo 'تنها ' . $quan . ' عدد در انبار باقی مانده'; ?></div>
                                <?php } ?>
                            <?php }else{?>
                                <div style="color: #db2323;"><?php echo 'این محصول دیگر موجود نیست !'; ?></div>
                            <?php }?>
                        </div>
                    </li></a>
            <?php } ?>
        </ul>
        <div class="the-ajax-cart-element-bottom">
            <a href="<?php echo home_url('/checkout');?>"><button><i class="fas fa-shipping-fast"></i>تکمیل سفارش</button></a>
        </div>
    </div>
    <?php
}

}
