<?php

class ControllerDevMarketplaceTy extends Controller
{


    private $market;
    private $debug = false;
    private $register = null;
    private $user = null;
    private $api = array('username' => 'VpPyj9X32HPzb9yyQJNf', 'password' => 'pc5Y7py83FUelL8qz8Fl', 'shopId' => '139975');


    public function get_orders()
    {

        $result = $this->ty->get_orders2('Created', $this->api);
        print_r($result);


    }


    public function trendcheck2($cid){
        $check = $this->db->query("SELECT * FROM ".DB_PREFIX."category WHERE category_id = '".$cid."'");
        return $check->num_rows;
    }




    public function trendyol_categories(){

       // $this->db->query("truncate table ".DB_PREFIX."category");
        $url = 'https://api.trendyol.com/sapigw/product-categories';
       // $this->db->query("truncate table ".DB_PREFIX."category ");
        $categories = json_decode(file_get_contents($url), true);







        $ix = 0;
        foreach ($categories['categories'] as $trendcat){
            $name = $trendcat['name'];
            if($trendcat['subCategories']){
                foreach ($trendcat['subCategories'] as $trend_1) {
                    $name = $name.' > '.$trend_1['name'];
                    if($trend_1['subCategories']){
                        foreach ($trend_1['subCategories'] as $trend_2) {
                            $name = $name.' > '.$trend_2['name'];
                            if($trend_2['subCategories']){
                                foreach ($trend_2['subCategories'] as $trend_3) {
                                    $name = $name.' > '.$trend_3['name'];
                                    if($trend_3['subCategories']){
                                        foreach ($trend_3['subCategories'] as $trend_4) {
                                            $name = $name.' > '.$trend_4['name'];
                                            if($trend_4['subCategories']){
                                                foreach ($trend_4['subCategories'] as $trend_5) {
                                                    if($trend_5['subCategories']){
                                                        foreach ($trend_5['subCategories'] as $trend_6) {
                                                            if($this->trendcheck2($trend_6['id']) < 1){
                                                                $ix++;
                                                                $name = $trendcat['name'].' > '.$trend_1['name'].' > '.$trend_2['name'].' > '.$trend_3['name'].' > '.$trend_4['name'].' > '.$trend_5['name'].' > '.$trend_6['name'];
                                                                if(isset($trend_6['parentId'])){ $parentid = $trend_6['parentId']; } else { $parentid = 0;}
                                                                $this->db->query("INSERT INTO ".DB_PREFIX."category SET category_id = '".$trend_6['id']."', parent_id = '".$parentid."', name = '".$this->db->escape($name)."' , language_id=1");
                                                            }
                                                        }
                                                    } else {
                                                        if($this->trendcheck2($trend_5['id']) < 1){
                                                            $ix++;
                                                            $name = $trendcat['name'].' > '.$trend_1['name'].' > '.$trend_2['name'].' > '.$trend_3['name'].' > '.$trend_4['name'].' > '.$trend_5['name'];
                                                            if(isset($trend_5['parentId'])){ $parentid = $trend_5['parentId']; } else { $parentid = 0;}
                                                            $this->db->query("INSERT INTO ".DB_PREFIX."category SET category_id = '".$trend_5['id']."', parent_id = '".$parentid."', name = '".$this->db->escape($name)."' , language_id=1");
                                                        }
                                                    }
                                                }
                                            } else {
                                                if($this->trendcheck2($trend_4['id']) < 1){
                                                    $ix++;
                                                    $name = $trendcat['name'].' > '.$trend_1['name'].' > '.$trend_2['name'].' > '.$trend_3['name'].' > '.$trend_4['name'];
                                                    if(isset($trend_4['parentId'])){ $parentid = $trend_4['parentId']; } else { $parentid = 0;}
                                                    $this->db->query("INSERT INTO ".DB_PREFIX."category SET category_id = '".$trend_4['id']."', parent_id = '".$parentid."', name = '".$this->db->escape($name)."' , language_id=1");
                                                }
                                            }
                                        }
                                    } else {
                                        if($this->trendcheck2($trend_3['id']) < 1){
                                            $ix++;
                                            $name = $trendcat['name'].' > '.$trend_1['name'].' > '.$trend_2['name'].' > '.$trend_3['name'];
                                            if(isset($trend_3['parentId'])){ $parentid = $trend_3['parentId']; } else { $parentid = 0;}
                                            $this->db->query("INSERT INTO ".DB_PREFIX."category SET category_id = '".$trend_3['id']."', parent_id = '".$parentid."', name = '".$this->db->escape($name)."' , language_id=1");
                                        }
                                    }
                                }
                            } else {
                                if($this->trendcheck2($trend_2['id']) < 1){
                                    $ix++;
                                    $name = $trendcat['name'].' > '.$trend_1['name'].' > '.$trend_2['name'];
                                    if(isset($trend_2['parentId'])){ $parentid = $trend_2['parentId']; } else { $parentid = 0;}
                                    $this->db->query("INSERT INTO ".DB_PREFIX."category SET category_id = '".$trend_2['id']."', parent_id = '".$parentid."', name = '".$this->db->escape($name)."', language_id=1");
                                }
                            }
                        }
                    } else {
                        if($this->trendcheck2($trend_1['id']) < 1){
                            $ix++;
                            $name = $trendcat['name'].' > '.$trend_1['name'];
                            if(isset($trend_1['parentId'])){ $parentid = $trend_1['parentId']; } else { $parentid = 0;}
                            $this->db->query("INSERT INTO ".DB_PREFIX."category SET category_id = '".$trend_1['id']."', parent_id = '".$parentid."', name = '".$this->db->escape($name)."',  language_id=1");
                        }
                    }
                }
            } else {
                if($this->trendcheck2($trendcat['id']) < 1){
                    $ix++;
                    $name = $trendcat['name'];
                    if(isset($trendcat['parentId'])){ $parentid = $trendcat['parentId']; } else { $parentid = 0;}
                    $this->db->query("INSERT INTO ".DB_PREFIX."category SET category_id = '".$trendcat['id']."', parent_id = '".$parentid."', name = '".$this->db->escape($name)."', language_id=1");
                }
            }
        }

        echo 'bitti';
        return array('status' => 1, 'msg' => $ix.' Adet Kategori Başarıyla İndirildi');
    }



    public function category_attributes()
    {
        $api_info['merchant_id'] = $this->merchant_id;
        //  $request_data=$this->request->get['category_id'];

        $marketplace_id = 1;

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {

            $page = 1;
        }

        $token = $this->request->get['user_token'];

        $limit = 1;

        $query = $this->db->query("select category_id from " . DB_PREFIX . "category limit " . $limit * $page . ",  " . $limit . "  ");

        $request_data = $query->row['category_id'];
        $result = $this->ty->category_attributes($request_data, $this->api, false);

        if (isset($result['result']['categoryAttributes'])) {
            foreach ($result['result']['categoryAttributes'] as $attribute) {

                $query = $this->db->query("select attribute_id,market_attribute_id from " . DB_PREFIX . "attribute where market_attribute_id='" . $attribute['attribute']['id'] . "' ");

                if (!$query->num_rows) {

                    $this->db->query("INSERT INTO " . DB_PREFIX . "attribute  SET market_attribute_id ='" . $attribute['attribute']['id'] . "', marketplace_id='" . $marketplace_id . "', attribute_name='" . $this->db->escape($attribute['attribute']['name']) . "', required='" . $attribute['required'] . "', varianter='" . $attribute['varianter'] . "' , slicer='" . $attribute['slicer'] . "', allow_custom='" . $attribute['allowCustom'] . "'  ");


                    $attribute_id = $attribute['attribute']['id'];


                } else {
                    $this->db->query("UPDATE " . DB_PREFIX . "attribute  SET  marketplace_id='" . $marketplace_id . "', attribute_name='" . $this->db->escape($attribute['attribute']['name']) . "', required='" . $attribute['required'] . "', varianter='" . $attribute['varianter'] . "' , slicer='" . $attribute['slicer'] . "', allow_custom='" . $attribute['allowCustom'] . "' where market_attribute_id = '" . $query->row['market_attribute_id'] . "' ");

                    $attribute_id = $query->row['market_attribute_id'];
                }

                /* foreach ($attribute['attributeValues'] as $attributeValue) {
                     $query = $this->db->query("select * from " . DB_PREFIX . "attribute_value where attribute_id='" . $attribute['attribute']['id'] . "' and attribute_value_id='" . $attributeValue['id'] . "'  ");
                    if(!$query->num_rows){
                        $this->db->query("INSERT INTO " . DB_PREFIX . "attribute_value SET  attribute_id='" . $attribute['attribute']['id'] . "', attribute_value_id='" . $attributeValue['id'] . "', `name`='" . $this->db->escape($attributeValue['name']) . "' ");

                    }
                 }
        */
                $query = $this->db->query("select * from " . DB_PREFIX . "category_attribute where attribute_id='" . $attribute_id . "' and category_id='" . $request_data . "' ");

                $varianter = $attribute['varianter'];

                if ($attribute_id == 47) {
                    $varianter = 1;
                }


                if (!$query->num_rows) {

                    $this->db->query("INSERT INTO " . DB_PREFIX . "category_attribute SET attribute_id='" . $attribute_id . "', category_id='" . $request_data . "', varianter='" . $varianter . "',required='" . $attribute['required'] . "' ");

                } else {

                    $this->db->query("UPDATE " . DB_PREFIX . "category_attribute SET  varianter='" . $varianter . "', required='" . $attribute['required'] . "' where attribute_id='" . $attribute_id . "' and category_id='" . $request_data . "' ");

                }


            }
        }
        $page++;

        echo '<meta http-equiv="refresh" content="0; url=index.php?route=dev/marketplace/ty/category_attributes&user_token=' . $token . '&page=' . $page . '" />';

    }


}