<?php

class ModelEntegrasyonProductVariant extends Model
{


    public function isVarianterProduct($product_id)
    {
        $query = $this->udb->query("select product_id from product_variant where product_id='" . $product_id . "' ");
        return $query->num_rows;
    }


    public function getProductVariants($product_id)
    {

        $query = $this->udb->query("SELECT pv.product_variant_id,pv.barcode,pv.stock_code,pv.sale_price,pv.quantity,pv.image,v2m.n11,v2m.gg,v2m.ty,v2m.eptt,v2m.hb,v2m.cs,v2m.amz,v2m.etsy,v2m.amzus,v2m.amzeu FROM product_variant pv  LEFT JOIN variant_to_marketplace v2m ON(pv.product_variant_id=v2m.product_variant_id) WHERE pv.product_id = '" . (int)$product_id . "'");


        $variants=array();

        foreach ($query->rows as $variant){
            
            $variants[]=array(
                'product_variant_id'=>$variant['product_variant_id'],
                'barcode' =>$variant['barcode'],
                'stock_code' =>$variant['stock_code'],
                'quantity' =>$variant['quantity'],
                'sale_price' =>$variant['sale_price'],
                'image_root'=>$variant['image'],
                'image' =>$this->model_tool_image->resize($variant['image']),
                'variant_values'=>$this->getVariantValues($variant['product_variant_id']),
                'n11' => json_decode($variant['n11'],1),
                'gg' => json_decode($variant['gg'],1),
                'hb' => json_decode($variant['hb'],1),
                'ty' => json_decode($variant['ty'],1),
                'cs' => json_decode($variant['cs'],1),
                'etsy' => json_decode($variant['etsy'],1),
                'amz' => json_decode($variant['amz'],1),
                'amzeu' => json_decode($variant['amzeu'],1),
                'amzus' => json_decode($variant['amzus'],1),
                'eptt' => json_decode($variant['eptt'],1)
            );

        }

        return $variants;

    }

    public function getVariantValues($product_variant_id)
    {

        $sql="SELECT * FROM `product_variant_value` pvv LEFT JOIN multimarket.oc_attribute a ON(a.market_attribute_id=pvv.attribute_id) LEFT JOIN multimarket.oc_attribute_value av ON(pvv.attribute_value_id=av.attribute_value_id)  WHERE  pvv.product_variant_id='".$product_variant_id."' group by pvv.attribute_value_id order by pvv.sort_order ";

        //echo $sql;
       $query= $this->udb->query($sql);

       return $query->rows;

    }



    public function getVariantOptionsByProductId($product_id)
    {

        $query=$this->udb->query("SELECT * FROM multimarket.`oc_category_attribute` ca left join multimarket.`oc_attribute` a on(a.market_attribute_id=ca.attribute_id) left join product p on(p.category_id=ca.category_id) WHERE a.varianter=1 and p.product_id='".$product_id."'");
        return $query->rows;

    }

    public function getVariantOptionsByCategoryId($category_id)
    {

        $query=$this->udb->query("SELECT * FROM multimarket.`oc_category_attribute` ca left join multimarket.`oc_attribute` a on(a.market_attribute_id=ca.attribute_id)  WHERE ca.varianter=1 and ca.category_id='".$category_id."' and a.marketplace_id=1   ");
        return $query->rows;

    }

    public function addOrGetColorId($color)
    {
        $query =$this->db->query("select * from  ".DB_PREFIX."attribute_value where attribute_id=47 and LOWER(name)='".mb_strtolower($color)."' ");

        if($query->num_rows){

            return $query->row['attribute_value_id'];

        }else {
            $attibute_value_id=rand(333333333,999999999);
            $this->db->query("insert into ".DB_PREFIX."attribute_value SET attribute_id=47, attribute_value_id='".$attibute_value_id."', name='".$color."'  ");
            return $attibute_value_id;
        }
    }




    public function updateProductVariant($column, $value, $variant_id)
    {

        $sql = "UPDATE  product_variant SET " . $column . "='" . $value . "' where variant_id='" . $variant_id . "' ";

        try {
            $this->udb->query($sql);

        } catch (Exception $exception) {

            echo $exception->getMessage();
        }

        return $this->udb->countAffected();

    }



}