<?php
/**
* 2022 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
*  @author    Anvanto <anvantoco@gmail.com>
*  @copyright 2022 Anvanto
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/

class anProductTabsLabels extends ObjectModel
{
    /**
     * @var int
     */
    public $id_label;
    /**
     * @var int
     */
    public $id;
    /**
     * @var int
     */
    public $special_id_label;
    public $active = 1;
    public $label;
    public $color;
    public $color_background;
    public $relation = 0;
    public $position = 0;


    /**
     * @var array
     */

    public static $definition = [
        'table' => 'an_productextratabs_labels',
        'primary' => 'id_label',
        'multilang' => true,
        'fields' => [
            'special_id_label' => ['type' =>self::TYPE_STRING ],
            'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
            'label' => ['type' =>self::TYPE_HTML, 'validate' => 'isString', 'lang' => true ],
            'color' => ['type' =>self::TYPE_STRING, 'validate' => 'isColor'],
            'color_background' => ['type' =>self::TYPE_STRING, 'validate' => 'isColor'],

            'relation' => ['type' =>self::TYPE_INT, 'validate' => 'isUnsignedId'],
            'position' => ['type' =>self::TYPE_INT, 'validate' => 'isUnsignedId'],

        ],
    ];

    public function __construct($id = null, $id_lang = null)
    {
        parent::__construct($id, $id_lang);
    }

    public function add($auto_date = true, $null_values = false)
    {
        if (empty($this->special_id_label)) {
            $this->special_id_label = 'anpt-label-' . uniqid();
            
        }
    
        return parent::add($auto_date, $null_values);
    }  

    public static function getAllLabels()
    {
        $sql = '
        SELECT * FROM `' . _DB_PREFIX_ . 'an_productextratabs_labels` sw';    

        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
    }

    public static function getLabelsByIdProduct($id_product)
    {
        $context = Context::getContext();
        $cats = product::getProductCategories($id_product);

        $sql = '
        SELECT * FROM `' . _DB_PREFIX_ . 'an_productextratabs_labels_relations` szwr, `' . _DB_PREFIX_ . 'an_productextratabs_labels` sw
        LEFT JOIN `' . _DB_PREFIX_ . 'an_productextratabs_labels_lang` sl 
            ON (sw.`id_label` = sl.`id_label`
            AND sl.`id_lang` = ' . (int) $context->language->id . ')
        WHERE sw.`active`=1
            AND sw.`id_label` = szwr.`id_label` AND sw.`relation` = szwr.`type`
            AND ((szwr.`type` = 1 AND szwr.`id_type` IN (' . implode(', ', $cats) . ')   )
                OR (szwr.`type` = 2 AND szwr.`id_type` = '.(int) $id_product.')
                OR (szwr.`type` = 0 AND szwr.`id_type` = 0)
                )
        ';    
        
        if (Shop::isFeatureActive()) {
            $sql .= ' AND sw.`id_label` IN (
                SELECT sa.`id_label`
                FROM `' . _DB_PREFIX_ . 'an_productextratabs_shop` sa
                WHERE sa.id_shop IN (' . implode(', ', Shop::getContextListShopID()) . ')
            )';
        }
       
        $sql .= ' GROUP BY sw.`id_label`';
        $sql .= ' ORDER BY sw.`position`';
       
        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
        
        if (!$result){
            return [];
        }

        return $result;
    }


    public static function getProducsByIdLabels($id_label = 0)
    {
        if (!$id_label){
            return [];
        }
        
        $sql = '
        SELECT  *, p.*
        FROM `' . _DB_PREFIX_ . 'an_productextratabs_labels_relations` awl
        
        LEFT JOIN `' . _DB_PREFIX_ . 'product` p
            ON (p.`id_product` = awl.`id_type`)
        
        LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl
            ON (p.`id_product` = pl.`id_product`
            AND pl.`id_lang` = ' . (int) Context::getContext()->language->id . Shop::addSqlRestrictionOnLang('pl') . ')        
        
        WHERE awl.`id_label` = ' . (int) $id_label . '  AND awl.`type`="2" ';
        
        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql, true, false);

        return Product::getProductsProperties(Context::getContext()->language->id, $result);
    } 

    public static function getRelationCategories($id_label = 0)
    {
        if (!$id_label){
            return [];
        }
        
        $sql = '
        SELECT `id_type`
        FROM `' . _DB_PREFIX_ . 'an_productextratabs_labels_relations` awl        
        WHERE awl.`id_label` = ' . (int) $id_label . ' AND awl.`type`="1"  ';
        
        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql, true, false);
        
        $cats = [];
        if ($result) {
            foreach ($result as $item){
                $cats[] = $item['id_type'];
            }
        }
        
        return $cats;        
    }  
  
}