<?php
/**
* 2023 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
*  @author    Anvanto <anvantoco@gmail.com>
*  @copyright 2023 Anvanto
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/

class anProductTabsTplContent extends ObjectModel
{
    /**
     * @var int
     */
    public $id_content;
    /**
     * @var int
     */
    public $id;
    /**
     * @var int
     */
    public $content_active = 1;
    public $content_combinations = 0;
    public $content_contactform = 0;

    public $id_product;
    public $id_template;

    public $content_content;
    public $content_name;

    /**
     * @var array
     */

    public static $definition = [
        'table' => 'an_productextratabs_tpl_content',
        'primary' => 'id_content',
        'multilang' => true,
        'fields' => [
            'content_active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
            'content_combinations' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
            'content_contactform' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],

            'id_product' => ['type' =>self::TYPE_INT ],
            'id_template' => ['type' =>self::TYPE_INT ],
            
            'content_content' => ['type' =>self::TYPE_HTML,'lang' => true ],
            'content_name' => ['type' =>self::TYPE_HTML,'lang' => true ],
        ],
    ];


    /**
     * Formula constructor.
     *
     * @param null $id
     */
    public function __construct($id = null, $id_lang = null)
    {
        parent::__construct($id, $id_lang);
    }

    public static function getContentByProductTemplate($id_product, $id_template)
    {
        $context = Context::getContext();

        $sql = '
        SELECT * FROM `' . _DB_PREFIX_ . 'an_productextratabs_tpl_content` sw
        LEFT JOIN `' . _DB_PREFIX_ . 'an_productextratabs_tpl_content_lang` sl 
            ON (sw.`id_content` = sl.`id_content`
            AND sl.`id_lang` = ' . (int) $context->language->id . ')
        WHERE sw.`id_product` = '.(int)$id_product.' AND sw.`id_template` = '.(int)$id_template.' 
        '; 
  
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
    }


    public static function getTabsByIdProduct($id_product)
    {
        $context = Context::getContext();

        $sql = '
        SELECT * FROM `' . _DB_PREFIX_ . 'an_productextratabs` pt, `' . _DB_PREFIX_ . 'an_productextratabs_tpl_content` sw
        LEFT JOIN `' . _DB_PREFIX_ . 'an_productextratabs_tpl_content_lang` sl 
            ON (sw.`id_content` = sl.`id_content`
            AND sl.`id_lang` = ' . (int) $context->language->id . ')
        WHERE sw.`id_template` = pt.`idtab` AND sw.`id_product` = '.(int) $id_product.' AND sw.`content_active` = 1
        '; 

        $sql .= ' ORDER BY pt.`position`';
      
        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
 
        $tabs = [];
        foreach ($result as $item){
            $tabs[$item['idtab']] = [
                'tab_name' => $item['content_name'],
                'tab_content' => $item['content_content'],
                'combinations' => $item['content_combinations'],
                'contactform' => $item['content_contactform'],
            ];
        }

        return $tabs;
    }
      
    public static function getIdByProductTemplate($id_product, $id_template)
    {
        $sql = '
        SELECT id_content FROM `' . _DB_PREFIX_ . 'an_productextratabs_tpl_content`
        WHERE `id_product` = '.(int)$id_product.' AND `id_template` = '.(int)$id_template.'        ';    

        return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
    }

    public static function getIdContentByIdTemplate($id_template)
    {
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
        SELECT id_content FROM `' . _DB_PREFIX_ . 'an_productextratabs_tpl_content`
        WHERE `id_template` = '.(int)$id_template.' ');
    }



}