<?php
class ModelEntegrasyonMarketProduct extends Model
{


    public function getMarketProductsTotal($data)
    {
        $sql = "SELECT count(*) as total FROM market_product mp left join product_url pu on (mp.barcode=pu.barcode) where `code` = '" . $data['code'] . "' and marketplace_product_id !='' ";
        if (!empty($data['filter_name'])) {

            $sql .= "AND mp.name LIKE '%" . $this->db->escape($data['filter_name']) . "%' OR pu.url LIKE '%" . $this->db->escape($data['filter_name']) . "%'";

        }
        if (!empty($data['filter_oc_product_id'])) {

            $sql .= "AND oc_product_id LIKE '%" . $this->db->escape($data['filter_oc_product_id']) . "%'";
        }


        if (!empty($data['filter_model'])) {
            $sql .= " AND model LIKE '" . $this->db->escape($data['filter_model']) . "%'";
        }


        if (isset($data['filter_match']) && $data['filter_match'] !== '*') {

            if ($data['filter_match'] == 1) {
                $sql .= " AND product_id  != 0 ";

            } else {
                $sql .= " AND product_id = 0";

            }

        }

        if (!empty($data['filter_barcode'])) {
            $sql .= " AND barcode LIKE '" . $this->db->escape($data['filter_barcode']) . "%'";
        }
        if (!empty($data['filter_marketplace_product_id'])) {
            $sql .= " AND marketplace_product_id LIKE '" . $this->db->escape($data['filter_marketplace_product_id']) . "%'";
        }



        if (isset($data['filter_marketplace_do'])) {
            if($data['filter_marketplace_do'] !='*') {
                $sql .= " AND sale_status ='" . $data['filter_marketplace_do'] . "'";
            }
        }


        if (!empty($data['filter_stock_prefix'])) {


            if ($data['filter_stock_prefix'] != '*') {
                if ((int)$data['filter_stock']) {

                    $sql .= " AND quantity " . $data['filter_stock_prefix'] . " " . $data['filter_stock'] . " ";
                }
            }
        }

        if (!empty($data['filter_total_sale_prefix'])) {


            if ($data['filter_total_sale_prefix'] != '*') {

                $sql .= " AND total_sale " . $data['filter_total_sale_prefix'] . " " . $data['filter_total_sale'] . " ";

            }
        }


        if (!empty($data['filter_total_add_to_list_prefix'])) {


            if ($data['filter_total_add_to_list_prefix'] != '*') {

                $sql .= " AND total_add_list " . $data['filter_total_add_to_list_prefix'] . " " . $data['filter_total_add_to_list'] . " ";

            }
        }

        if (!empty($data['filter_total_view_prefix'])) {


            if ($data['filter_total_view_prefix'] != '*') {

                $sql .= " AND total_view " . $data['filter_total_view_prefix'] . " " . $data['filter_total_view'] . " ";

            }
        }

        if (!empty($data['filter_price_prefix'])) {


            if ($data['filter_price_prefix'] != '*') {
                if ($data['filter_price']) {

                    $sql .= " AND sale_price " . $data['filter_price_prefix'] . " " . $data['filter_price'] . " ";
                }
            }
        }



        $query = $this->db->query($sql);
        return $query->row['total'];
    }

    public function getMarketProducts($data)
    {



        $sql = "SELECT  *,DATEDIFF(NOW(), pu.last_update_date) AS day_passed FROM market_product mp left join product_url pu on (mp.barcode=pu.barcode) where mp.`code` = '" . $data['code'] . "' and marketplace_product_id !='' and pu.status=1 ";


        if (!empty($data['filter_name'])) {

            $sql .= "AND mp.name LIKE '%" . $this->db->escape($data['filter_name']) . "%' OR pu.url LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
        }

        if (!empty($data['filter_product_id'])) {

            $sql .= "AND mp.product_id LIKE '%" . $this->db->escape($data['filter_product_id']) . "%'";
        }


        if (!empty($data['filter_model'])) {
            $sql .= " AND mp.marketplace_product_id LIKE '" . $this->db->escape($data['filter_model']) . "%'";
        }


        if (isset($data['filter_match']) && $data['filter_match'] !== '*') {

            if ($data['filter_match'] == 1) {
                $sql .= " AND mp.product_id  NOT LIKE '0'";

            } else {
                $sql .= " AND mp.product_id LIKE '0'";

            }

        }

        if (isset($data['filter_marketplace_do'])) {

            if (isset($data['filter_marketplace_do'])) {
                if($data['filter_marketplace_do'] !='*'){
                    $sql .= " AND mp.sale_status ='".$data['filter_marketplace_do']."'";

                }
            }
        }

        if (!empty($data['filter_barcode'])) {
            $sql .= " AND mp.barcode LIKE '%" . $this->db->escape($data['filter_barcode']) . "%'";
        }
        if (!empty($data['filter_marketplace_product_id'])) {
            $sql .= " AND mp.marketplace_product_id LIKE '" . $this->db->escape($data['filter_marketplace_product_id']) . "%'";
        }

        if (!empty($data['filter_stock_prefix'])) {


            if ($data['filter_stock_prefix'] != '*') {
                if ((int)$data['filter_stock']) {

                    $sql .= " AND mp.quantity " . $data['filter_stock_prefix'] . " " . $data['filter_stock'] . " ";
                }
            }
        }

        if (!empty($data['filter_total_sale_prefix'])) {


            if ($data['filter_total_sale_prefix'] != '*') {


                $sql .= " AND mp.total_sale " . $data['filter_total_sale_prefix'] . " " . $data['filter_total_sale'] . " ";

            }
        }


        if (!empty($data['filter_total_add_to_list_prefix'])) {


            if ($data['filter_total_add_to_list_prefix'] != '*') {

                $sql .= " AND mp.total_add_list " . $data['filter_total_add_to_list_prefix'] . " " . $data['filter_total_add_to_list'] . " ";

            }
        }
        if (!empty($data['filter_total_view_prefix'])) {


            if ($data['filter_total_view_prefix'] != '*') {

                $sql .= " AND mp.total_view " . $data['filter_total_view_prefix'] . " " . $data['filter_total_view'] . " ";
            }
        }

        if (!empty($data['filter_price_prefix'])) {


            if ($data['filter_price_prefix'] != '*') {
                if ($data['filter_price']) {

                    $sql .= " AND mp.sale_price " . $data['filter_price_prefix'] . " " . $data['filter_price'] . " ";
                }
            }
        }


        // Sıralama ekle
        if (isset($data['sort']) && in_array($data['sort'], ['total_view', 'total_sale', 'total_add_list','buybox'])) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY mp.name"; // Varsayılan sıralama
        }

        // Sıralama türü ekle (ASC veya DESC)
        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }


            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }





        $query = $this->db->query($sql);
        return $query->rows;


    }


    public function getTotalUnApprovedProducts($data)
    {

        $keyword = 'approval_status";i:0';
        $sql = "select count(*) as total from " . DB_PREFIX . "product_to_marketplace p2m left join " . DB_PREFIX . "product p ON(p.product_id=p2m.product_id) where p2m." . $data['code'] . " like '%" . $keyword . "%' ";


        if (!empty($data['filter_model'])) {
            $sql .= " AND p.model LIKE '" . $this->db->escape($data['filter_model']) . "%'";
        }


        try {
            $query = $this->db->query($sql);
            return $query->row['total'];

        } catch (Exception $exception) {

            echo $exception->getMessage();
        }


    }

    public function is_product_exists($stock_code)
    {
        $query = $this->db->query("select product_id from product where stock_code='" . $this->db->escape($stock_code) . "' ");
        if ($query->num_rows) {
            return true;
        }
        $query = $this->db->query("select product_id from product_variant where stock_code='" . $this->db->escape($stock_code) . "' ");

        if ($query->num_rows) {
            return true;
        }

        return false;

    }

    public function getMarketPlaceProductForMarket($product_id, $code)
    {

        $sql = "select * from product_to_marketplace where product_id='" . $product_id . "'";
        $query = $this->db->query($sql);

        if ($query->num_rows) {
            return $query->row[$code] ? json_decode($query->row[$code], 1) : array();

        } else {
            return array();
        }
    }
    public function getUnApprovedProducts($data)
    {

        $keyword = 'approval_status";i:0';
        $sql = "select * from " . DB_PREFIX . "product_to_marketplace p2m left join " . DB_PREFIX . "product p ON(p.product_id=p2m.product_id) left join " . DB_PREFIX . "product_description pd ON(p.product_id=pd.product_id)  where p2m." . $data['code'] . " like '%" . $keyword . "%' ";

        if (!empty($data['filter_model'])) {
            $sql .= " AND p.model LIKE '" . $this->db->escape($data['filter_model']) . "%'";
        }


        $sql .= " ORDER BY p2m.date_added DESC";

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }
        }

        //  $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];

        try {
            $query = $this->db->query($sql);
            return $query->rows;
        } catch (Exception $exception) {

            echo $exception->getMessage();
        }


    }
    public function getPassiveProducts($code)
    {

        $sql = "select p.product_id from product p left join product_to_marketplace p2m ON(p.product_id=p2m.product_id) where p2m." . $code . "='' ";


        $query = $this->db->query($sql);


        return $query->rows;

    }
    public function getCloseMarketPlaceProducts($code)
    {


        $status = 'sale_status";i:0';
        $alternatif_status = 'sale_status";b:0';

        $sql = " select * from product_to_marketplace WHERE " . $code . " LIKE '%" . $status . "%'  or  " . $code . " LIKE '%" . $alternatif_status . "%'    ";


        //  $sql = "select * from product_to_marketplace where $code != '' ";

        $query = $this->db->query($sql);


        //foreach ($query->rows as $row){
        //   if (!unserialize($row[$code])['sale_status']){
        //  $products[] = $row;
        //  }

        // }


        return $query->rows;
    }
    public function getMarketPlaceProductsForMatch($product_id, $code, $product_model)
    {

        $query = $this->db->query("select * from product_to_marketplace where product_id='" . $product_id . "'");
        if ($query->num_rows) {
            $row = $query->row;

            $variable = $row['' . $code . ''];

            if ($variable) {
                $settings = unserialize($variable);

                $temp = isset($settings['product_match']) ? $settings['product_match'] : 0;
                $settings['product_match'] = $product_model;

                if (!$product_model) {

                    unset($settings['product_match']);
                }


                if ($settings) {


                    $this->db->query("update product_to_marketplace SET $code='" . $this->db->escape(serialize($settings)) . "', date_modified=NOW() where product_id='" . $product_id . "' ");

                } else {

                    $this->db->query("update product_to_marketplace SET $code='', date_modified=NOW() where product_id_id='" . $product_id . "' ");
                }


            } else {

                $insert_data = array('product_match' => $product_model);
                $this->db->query("update product_to_marketplace SET $code='" . $this->db->escape(serialize($insert_data)) . "',date_modified=NOW() where product_id ='" . $product_id . "' ");

            }
        } else {
            //Yeni Match_Id Oluştur;
            $insert_data = array('product_match' => $product_model);
            $this->db->query("insert into product_to_marketplace SET $code='" . $this->db->escape(serialize($insert_data)) . "', product_id='" . $product_id . "', date_modified=NOW() ");
        }


    }
    public function getTotalProductByMarketPlace($marketplace)
    {

        $query = $this->db->query("SELECT COUNT(product_id) as total FROM `product_to_marketplace` WHERE `$marketplace` !=''");

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

    }
    public function is_product_exists_in_product_to_marketplace($code, $product)
    {

        if (!$product['barcode']) return false;
        try {
            $query = $this->db->query("select product_id from product_to_marketplace where  $code LIKE '%" . $product['barcode'] . "%' ");

            if ($query->num_rows) {
                return true;
            } else {


                return 0;
            }


        } catch (Exception $exception) {

            echo $exception->getMessage();

        }
    }
    public function getMarketPlaceUrl($code, $market_id, $cs_link = false)
    {


        if ($code == 'gg') {

            return 'http://urun.gittigidiyor.com/' . $market_id;
        } else if ($code == 'n11') {

            return 'http://urun.n11.com/xxx-P' . $market_id;
        } else if ($code == 'ty') {

            return 'https://www.trendyol.com/p/p-p-p-' . $market_id;
        } else if ($code == 'hb') {

            return 'https://www.hepsiburada.com/product-p-' . $market_id;

        } else if ($code == 'eptt') {

            return 'https://www.epttavm.com/item/' . $market_id . '_p';

        } else if ($code == 'cs') {

            return $cs_link;

        } else {

            return false;
        }


    }

}
