<?php
class ControllerCatalogProduct extends Controller {

    private $error = array();
    public function muimage() {
        $this->load->model('tool/image');
        if (isset($this->request->get['image'])) {
            $this->response->setOutput($this->model_tool_image->resize(html_entity_decode($this->request->get['image'], ENT_QUOTES, 'UTF-8'), 100, 100));
        }
    }

    public function index() {

        $this->load->language('catalog/product');

        $this->load->language('catalog/product');
        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('catalog/product');

        $this->getList();

    }

    public function add() {
        $this->load->language('catalog/product');
        $this->document->addStyle('https://scripts.piyersoft.com/stylesheet/autocomplete.css');

        $this->document->setTitle($this->language->get('heading_title'));

        $query = $this->db->query("SHOW COLUMNS FROM ps_url_alias WHERE field = 'language_id'");

        $exists = 0;
        foreach ($query->rows as $index) {
            $exists++;
        }

        if (!$exists) {
            $this->db->query("ALTER TABLE ps_url_alias ADD COLUMN `language_id` int(11) NOT NULL DEFAULT '" . $this->config->get('config_language_id') . "';");
        }


        $this->load->model('catalog/product');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {

            $this->model_catalog_product->addProduct($this->request->post);




            $this->session->data['success'] = $this->language->get('text_success');

            $this->creatAllCache();


            $url = '';

            if (isset($this->request->get['filter_name'])) {

                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));

            }

            if (isset($this->request->get['filter_except_name'])) {

                $url .= '&filter_except_name=' . urlencode(html_entity_decode($this->request->get['filter_except_name'], ENT_QUOTES, 'UTF-8'));

            }

            if (isset($this->request->get['filter_model'])) {

                $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));

            }



            if (isset($this->request->get['filter_price'])) {

                $url .= '&filter_price=' . $this->request->get['filter_price'];

            }



            if (isset($this->request->get['filter_quantity'])) {

                $url .= '&filter_quantity=' . $this->request->get['filter_quantity'];

            }



            if (isset($this->request->get['filter_status'])) {

                $url .= '&filter_status=' . $this->request->get['filter_status'];

            }



            if (isset($this->request->get['sort'])) {

                $url .= '&sort=' . $this->request->get['sort'];

            }



            if (isset($this->request->get['order'])) {

                $url .= '&order=' . $this->request->get['order'];

            }



            if (isset($this->request->get['page'])) {

                $url .= '&page=' . $this->request->get['page'];

            }



            $this->response->redirect($this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, true));

        }



        $this->getForm();

    }

    public function edit() {

        $this->load->language('catalog/product');



        $this->document->setTitle($this->language->get('heading_title'));



        $query = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "url_alias WHERE field = 'language_id'");



        $exists = 0;

        foreach ($query->rows as $index) {

            $exists++;

        }



        if (!$exists) {

            $this->db->query("ALTER TABLE " . DB_PREFIX . "url_alias ADD COLUMN `language_id` int(11) NOT NULL DEFAULT '" . $this->config->get('config_language_id') . "';");

        }







        $this->load->model('catalog/product');



        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {


            $this->model_catalog_product->editProduct($this->request->get['product_id'], $this->request->post);



            $this->session->data['success'] = $this->language->get('text_success');

            //$this->creatAllCache();

            $url = '';

            if (isset($this->request->get['filter_name'])) {

                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));

            }

            if (isset($this->request->get['filter_except_name'])) {

                $url .= '&filter_except_name=' . urlencode(html_entity_decode($this->request->get['filter_except_name'], ENT_QUOTES, 'UTF-8'));

            }

            if (isset($this->request->get['filter_model'])) {

                $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));

            }



            if (isset($this->request->get['filter_price'])) {

                $url .= '&filter_price=' . $this->request->get['filter_price'];

            }



            if (isset($this->request->get['filter_quantity'])) {

                $url .= '&filter_quantity=' . $this->request->get['filter_quantity'];

            }



            if (isset($this->request->get['filter_status'])) {

                $url .= '&filter_status=' . $this->request->get['filter_status'];

            }



            if (isset($this->request->get['sort'])) {

                $url .= '&sort=' . $this->request->get['sort'];

            }



            if (isset($this->request->get['order'])) {

                $url .= '&order=' . $this->request->get['order'];

            }



            if (isset($this->request->get['page'])) {

                $url .= '&page=' . $this->request->get['page'];

            }



            $this->response->redirect($this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, true));

        }



        $this->getForm();

    }

    public function editStatus() {

        $this->load->language('catalog/product');

        $this->load->model('catalog/product');



        $product_id = (int) $this->request->post['product_id'];

        $status = (bool) $this->request->post['status'];

        $status = !$status;



        if ($this->validateEditStatus()) {

            $this->model_catalog_product->editProductStatus($product_id, $status);

            $json = array(

                'success' => true,

                'status' => (int) $status

            );

        } else {

            $json = array(

                'success' => false,

                'message' => $this->error['warning']

            );

        }

        $this->response->addHeader('Content-Type: application/json');

        $this->response->setOutput(json_encode($json));

    }

    public function editProductShowcaseStatus() {
        $product_id = (int) $this->request->post['product_id'];
        $status = $this->request->post['status'];
        $status = !$status;
        $this->db->query("UPDATE ps_product SET product_showcase = '" . $status . "' WHERE product_id = '" . (int) $product_id . "' ");
        $json = array(

            'success' => true,

            'status' => (int) $status

        );
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

    public function editProductLidyanaStatus() {
        $product_id = (int) $this->request->post['product_id'];
        $status = $this->request->post['status'];
        $status = !$status;
        $this->db->query("UPDATE ps_product SET lidyana = '" . $status . "' WHERE product_id = '" . (int) $product_id . "' ");
        $json = array(
            'success' => true,
            'status' => (int)$status
        );
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

    public function editProductDrStatus() {
        $product_id = (int) $this->request->post['product_id'];
        $status = $this->request->post['status'];
        $status = !$status;
        $this->db->query("UPDATE ps_product SET dr = '" . $status . "' WHERE product_id = '" . (int) $product_id . "' ");
        $json = array(
            'success' => true,
            'status' => (int)$status
        );
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

    public function delete() {

        $this->load->language('catalog/product');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('catalog/product');


        if (isset($this->request->post['selected']) && $this->validateDelete()) {

            foreach ($this->request->post['selected'] as $product_id) {

                $this->model_catalog_product->deleteProduct($product_id);

            }

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['filter_name'])) {
                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
            }

            if (isset($this->request->get['filter_except_name'])) {
                $url .= '&filter_except_name=' . urlencode(html_entity_decode($this->request->get['filter_except_name'], ENT_QUOTES, 'UTF-8'));
            }

            if (isset($this->request->get['filter_model'])) {
                $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
            }


            if (isset($this->request->get['filter_price'])) {
                $url .= '&filter_price=' . $this->request->get['filter_price'];
            }

            if (isset($this->request->get['filter_quantity'])) {
                $url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
            }

            if (isset($this->request->get['filter_status'])) {
                $url .= '&filter_status=' . $this->request->get['filter_status'];
            }

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

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

            $this->response->redirect($this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, true));

        }



        $this->getList();

    }

    public function allProductDelete() {



        $data['all_product_delete_kesin'] = $this->url->link('catalog/product/allProductDeleteKesin', 'token=' . $this->session->data['token'], true);



        echo "<a href='" . $data['all_product_delete_kesin'] . "'>Sil</a>";

    }

    public function allProductDeleteKesin() {

        $this->db->query("TRUNCATE ps_coupon_product ");

        $this->db->query("TRUNCATE ps_homepage_tab_products ");

        //$this->db->query("TRUNCATE ps_korgun_product ");

        $this->db->query("TRUNCATE ps_n11_product_to_product ");

        $this->db->query("TRUNCATE ps_order_product ");

        $this->db->query("TRUNCATE ps_product ");

        $this->db->query("TRUNCATE ps_product_attribute ");

        $this->db->query("TRUNCATE ps_product_description ");

        $this->db->query("TRUNCATE ps_product_discount ");

        $this->db->query("TRUNCATE ps_product_filter ");

        $this->db->query("TRUNCATE ps_product_image ");

        $this->db->query("TRUNCATE ps_product_option ");

        $this->db->query("TRUNCATE ps_product_option_value ");

        $this->db->query("TRUNCATE ps_product_recurring ");

        $this->db->query("TRUNCATE ps_product_related ");

        $this->db->query("TRUNCATE ps_product_reward ");

        $this->db->query("TRUNCATE ps_product_special ");

        $this->db->query("TRUNCATE ps_product_to_category ");

        $this->db->query("TRUNCATE ps_product_to_download ");

        $this->db->query("TRUNCATE ps_product_to_layout ");

        $this->db->query("TRUNCATE ps_product_to_store ");





        $this->response->redirect($this->url->link('catalog/product', 'token=' . $this->session->data['token'], true));

    }

    public function copy() {

        $this->load->language('catalog/product');

        $this->document->setTitle($this->language->get('heading_title'));

        $query = $this->db->query("SHOW COLUMNS FROM " . DB_PREFIX . "url_alias WHERE field = 'language_id'");

        $exists = 0;

        foreach ($query->rows as $index) {
            $exists++;

        }


        if (!$exists) {
            $this->db->query("ALTER TABLE " . DB_PREFIX . "url_alias ADD COLUMN `language_id` int(11) NOT NULL DEFAULT '" . $this->config->get('config_language_id') . "';");
        }







        $this->load->model('catalog/product');



        if (isset($this->request->post['selected']) && $this->validateCopy()) {

            foreach ($this->request->post['selected'] as $product_id) {

                $this->model_catalog_product->copyProduct($product_id);

            }



            $this->session->data['success'] = $this->language->get('text_success');



            $url = '';



            if (isset($this->request->get['filter_name'])) {

                $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));

            }

            if (isset($this->request->get['filter_except_name'])) {

                $url .= '&filter_except_name=' . urlencode(html_entity_decode($this->request->get['filter_except_name'], ENT_QUOTES, 'UTF-8'));

            }

            if (isset($this->request->get['filter_model'])) {

                $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));

            }



            if (isset($this->request->get['filter_price'])) {

                $url .= '&filter_price=' . $this->request->get['filter_price'];

            }



            if (isset($this->request->get['filter_quantity'])) {

                $url .= '&filter_quantity=' . $this->request->get['filter_quantity'];

            }



            if (isset($this->request->get['filter_status'])) {

                $url .= '&filter_status=' . $this->request->get['filter_status'];

            }



            if (isset($this->request->get['sort'])) {

                $url .= '&sort=' . $this->request->get['sort'];

            }



            if (isset($this->request->get['order'])) {

                $url .= '&order=' . $this->request->get['order'];

            }



            if (isset($this->request->get['page'])) {

                $url .= '&page=' . $this->request->get['page'];

            }



            $this->response->redirect($this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, true));

        }



        $this->getList();

    }
/*
// eski logo entegrasyonundan kalma kullanılmıyor kapattım -can 15.09.2022
    public function savelogo()
    {
        $this->load->language('catalog/product');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('catalog/product');


        if (isset($this->request->post['selected'])) {


            foreach ($this->request->post['selected'] as $product_id) {
                $this->model_catalog_product->logoMateryalSaveQuery($this->model_catalog_product->getProduct($product_id));

            }

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['filter_logo_materyal_list'])) {
                $url .= '&filter_logo_materyal_list=' . urlencode(html_entity_decode($this->request->get['filter_logo_materyal_list'], ENT_QUOTES, 'UTF-8'));
            }

            $this->response->redirect($this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, true));

        }


        $this->getList();

    }
    */

    protected function getList() {

        if (isset($this->session->data['error_facebook_sync'])) {
            $data['error_facebook_sync'] = $this->session->data['error_facebook_sync'];
            unset($this->session->data['error_facebook_sync']);
        } else {
            $data['error_facebook_sync'] = '';
        }

        if (isset($this->session->data['error_facebook_delete'])) {
            $data['error_facebook_delete'] = $this->session->data['error_facebook_delete'];
            unset($this->session->data['error_facebook_delete']);
        } else {
            $data['error_facebook_delete'] = '';
        }

        if (isset($this->session->data['plugin_upgrade_message'])) {
            $data['plugin_upgrade_message'] = $this->session->data['plugin_upgrade_message'];
            unset($this->session->data['plugin_upgrade_message']);
        } else {
            $data['plugin_upgrade_message'] = '';
        }

        $this->document->addStyle('https://scripts.piyersoft.com/stylesheet/quick-enable-disable-product/enabledisable.css');

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

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

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


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


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

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


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



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


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



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



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



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



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



        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = 'p.product_id';
        }


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


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


        $url = '';



        if (isset($this->request->get['filter_category_assigned'])) {
            $url .= '&filter_category_assigned=' . urlencode(html_entity_decode($this->request->get['filter_category_assigned'], ENT_QUOTES, 'UTF-8'));
        }


        if (isset($this->request->get['filter_name'])) {
            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_except_name'])) {
            $url .= '&filter_except_name=' . urlencode(html_entity_decode($this->request->get['filter_except_name'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_model'])) {
            $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_logo_materyal_list'])) {
            $url .= '&filter_logo_materyal_list=' . urlencode(html_entity_decode($this->request->get['filter_logo_materyal_list'], ENT_QUOTES, 'UTF-8'));
        }


        if (isset($this->request->get['filter_price'])) {
            $url .= '&filter_price=' . $this->request->get['filter_price'];
        }

        if (isset($this->request->get['filter_quantity'])) {
            $url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
        }


        if (isset($this->request->get['filter_product_id'])) {
            $url .= '&filter_product_id=' . $this->request->get['filter_product_id'];
        }


        if (isset($this->request->get['filter_category'])) {
            $url .= '&filter_category=' . $this->request->get['filter_category'];
        }



        if (isset($this->request->get['filter_marka'])) {
            $url .= '&filter_marka=' . $this->request->get['filter_marka'];
        }


        if (isset($this->request->get['filter_status'])) {
            $url .= '&filter_status=' . $this->request->get['filter_status'];
        }


        if (isset($this->request->get['filter_image'])) {
            $url .= '&filter_image=' . $this->request->get['filter_image'];
        }


        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }


        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }


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


        if ($this->user->getSuperAdmin()) {
            $data['super_admin'] = 1;
        } else {
            $data['super_admin'] = 0;
        }


        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
        );


        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, true)
        );



        $data['add'] = $this->url->link('catalog/product/add', 'token=' . $this->session->data['token'] . $url, true);
        $data['copy'] = $this->url->link('catalog/product/copy', 'token=' . $this->session->data['token'] . $url, true);
        $data['savelogo'] = $this->url->link('catalog/product/savelogo', 'token=' . $this->session->data['token'] . $url, true);
        $data['delete'] = $this->url->link('catalog/product/delete', 'token=' . $this->session->data['token'] . $url, true);
        $data['all_product_delete'] = $this->url->link('catalog/product/allProductDelete', 'token=' . $this->session->data['token'] . $url, true);


        $data['products'] = array();


        $filter_data = array(
            'filter_name' => $filter_name,
            'filter_except_name' => $filter_except_name,
            'filter_model' => $filter_model,
            'filter_logo_materyal_list' => $filter_logo_materyal_list,
            'filter_price' => $filter_price,
            'filter_quantity' => $filter_quantity,
            'filter_product_id' => $filter_product_id,
            'filter_category' => $filter_category,
            'filter_marka' => $filter_marka,
            'filter_status' => $filter_status,
            'filter_category_assigned' => $filter_category_assigned,
            'filter_image' => $filter_image,
            'sort' => $sort,
            'order' => $order,
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
            'limit' => $this->config->get('config_limit_admin')
        );



        $this->load->model('tool/image');


        $results = $this->model_catalog_product->getProducts($filter_data);

        $product_total = count($results);

        foreach ($results as $result) {



            if (is_file(DIR_IMAGE . $result['image'])) {
                $image = $this->model_tool_image->resize($result['image'], 40, 40);
            } else if (!is_file(DIR_IMAGE . $result['image'])) {
                $image = $this->model_tool_image->resize('no_image.png', 40, 40);
                if ($this->model_catalog_product->getProductOptionValueThumb($result['product_id'])) {
                    $image = $this->model_tool_image->resize($this->model_catalog_product->getProductOptionValueThumb($result['product_id']), 40, 40);
                }
            } else {
                $image = $this->model_tool_image->resize('no_image.png', 40, 40);
            }

            $special = false;

            $product_specials = $this->model_catalog_product->getProductSpecials($result['product_id']);

            foreach ($product_specials as $product_special) {
                if (($product_special['date_start'] == '0000-00-00' || strtotime($product_special['date_start']) < time()) && ($product_special['date_end'] == '0000-00-00' || strtotime($product_special['date_end']) > time())) {
                    $special = $product_special['price'];
                    break;
                }
            }

            $data['products'][] = array(
                'product_id' => $result['product_id'],
                'image' => $image,
                'name' => $result['name'],
                'model' => $result['model'],
                'barcode' => $result['barcode'],
                'lidyana' => $result['lidyana'],
                'dr' => $result['dr'],
                'cost_price' => $result['cost_price'],
                'price' => $this->currency->format($this->currency->convert($result['price'], $this->currency->getCodeOrDefault($result['currency_id']), $this->config->get('config_currency')), $this->currency->getCodeOrDefault($result['currency_id'])),
                'special' => empty($special) ? false : $this->currency->format(
                        $this->currency->convert($special, $this->currency->getCodeOrDefault($result['currency_id']), $this->config->get('config_currency')), $this->currency->getCodeOrDefault($result['currency_id'])
                ),
                'quantity' => $this->model_catalog_product->getQuantity($result['product_id']),
                'status' => $result['status'],
                'product_showcase' => $result['product_showcase'],
                'edit' => $this->url->link('catalog/product/edit', 'token=' . $this->session->data['token'] . '&product_id=' . $result['product_id'] . $url, true),
                'logo_code' => $result['logo_code']
            );

        }


        $this->load->model('catalog/category');

        $categories = $this->model_catalog_category->getCategories();

        $data['categories'] = array();

        foreach ($categories as $key => $category) {
            $data['categories'][] = array(
                'category_id' => $category['category_id'],
                'name' => $category['name'],
            );
        }

        if (isset($this->request->get['filter_category'])) {
            $filter_category = $this->request->get['filter_category'];
            $data['filter_category'] = $this->request->get['filter_category'];

            $data['filter_category_name'] = $this->model_catalog_category->getCategoryName($filter_category);

        } else {
            $data['filter_category_name'] = '';
            $data['filter_category'] = 0;
        }

        $this->load->model('catalog/manufacturer');

        $manufacturers = $this->model_catalog_manufacturer->getManufacturers(); //var_dump($manufacturers); die();
        $data['manufacturers'] = array();
        foreach ($manufacturers as $key => $manufacturer) {
            $data['manufacturers'][] = array(
                'manufacturer_id' => $manufacturer['manufacturer_id'],
                'name' => $manufacturer['name'],
            );
        }

        $data['heading_title'] = $this->language->get('heading_title');

        $data['text_list'] = $this->language->get('text_list');
        $data['text_enabled'] = $this->language->get('text_enabled');
        $data['text_disabled'] = $this->language->get('text_disabled');
        $data['text_no_results'] = $this->language->get('text_no_results');
        $data['text_confirm'] = $this->language->get('text_confirm');

        $data['column_image'] = $this->language->get('column_image');
        $data['column_name'] = $this->language->get('column_name');
        $data['column_model'] = $this->language->get('column_model');
        $data['column_price'] = $this->language->get('column_price');
        $data['column_quantity'] = $this->language->get('column_quantity');
        $data['column_status'] = $this->language->get('column_status');
        $data['column_action'] = $this->language->get('column_action');



        $data['entry_name'] = $this->language->get('entry_name');
        $data['entry_except_name'] = $this->language->get('entry_except_name');
        $data['entry_model'] = $this->language->get('entry_model');
        $data['entry_price'] = $this->language->get('entry_price');
        $data['entry_quantity'] = $this->language->get('entry_quantity');
        $data['entry_status'] = $this->language->get('entry_status');
        $data['entry_image'] = $this->language->get('entry_image');
        $data['entry_filter_category_assigned'] = $this->language->get('entry_filter_category_assigned');


        $data['entry_category_name'] = $this->language->get('entry_category_name');
        $data['button_copy'] = $this->language->get('button_copy');
        $data['button_add'] = $this->language->get('button_add');
        $data['button_edit'] = $this->language->get('button_edit');
        $data['button_delete'] = $this->language->get('button_delete');
        $data['button_filter'] = $this->language->get('button_filter');


        $data['token'] = $this->session->data['token'];


        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }



        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];
            unset($this->session->data['success']);
        } else {
            $data['success'] = '';
        }



        if (isset($this->request->post['selected'])) {

            $data['selected'] = (array) $this->request->post['selected'];

        } else {

            $data['selected'] = array();

        }



        $url = '';



        if (isset($this->request->get['filter_name'])) {

            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));

        }
        
        if (isset($this->request->get['filter_except_name'])) {

            $url .= '&filter_except_name=' . urlencode(html_entity_decode($this->request->get['filter_except_name'], ENT_QUOTES, 'UTF-8'));

        }


        if (isset($this->request->get['filter_model'])) {

            $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));

        }



        if (isset($this->request->get['filter_price'])) {

            $url .= '&filter_price=' . $this->request->get['filter_price'];

        }



        if (isset($this->request->get['filter_quantity'])) {

            $url .= '&filter_quantity=' . $this->request->get['filter_quantity'];

        }



        if (isset($this->request->get['filter_product_id'])) {

            $url .= '&filter_product_id=' . $this->request->get['filter_product_id'];

        }



        if (isset($this->request->get['filter_category'])) {

            $url .= '&filter_category=' . $this->request->get['filter_category'];

        }



        if (isset($this->request->get['filter_marka'])) {

            $url .= '&filter_marka=' . $this->request->get['filter_marka'];

        }



        if (isset($this->request->get['filter_status'])) {

            $url .= '&filter_status=' . $this->request->get['filter_status'];

        }



        if (isset($this->request->get['filter_image'])) {

            $url .= '&filter_image=' . $this->request->get['filter_image'];

        }



        if ($order == 'ASC') {

            $url .= '&order=DESC';

        } else {

            $url .= '&order=ASC';

        }



        if (isset($this->request->get['page'])) {

            $url .= '&page=' . $this->request->get['page'];

        }



        $data['sort_name'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=pd.name' . $url, true);

        $data['sort_product_id'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.product_id' . $url, true);

        $data['sort_model'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.model' . $url, true);

        $data['sort_price'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.price' . $url, true);

        $data['sort_quantity'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.quantity' . $url, true);

        $data['sort_status'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.status' . $url, true);

        $data['sort_order'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=p.sort_order' . $url, true);



        $url = '';



        if (isset($this->request->get['filter_name'])) {

            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));

        }

        if (isset($this->request->get['filter_except_name'])) {

            $url .= '&filter_except_name=' . urlencode(html_entity_decode($this->request->get['filter_except_name'], ENT_QUOTES, 'UTF-8'));

        }

        if (isset($this->request->get['filter_model'])) {

            $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));

        }



        if (isset($this->request->get['filter_price'])) {

            $url .= '&filter_price=' . $this->request->get['filter_price'];

        }



        if (isset($this->request->get['filter_quantity'])) {

            $url .= '&filter_quantity=' . $this->request->get['filter_quantity'];

        }



        if (isset($this->request->get['filter_product_id'])) {

            $url .= '&filter_product_id=' . $this->request->get['filter_product_id'];

        }



        if (isset($this->request->get['filter_category'])) {

            $url .= '&filter_category=' . $this->request->get['filter_category'];

        }



        if (isset($this->request->get['filter_marka'])) {

            $url .= '&filter_marka=' . $this->request->get['filter_marka'];

        }



        if (isset($this->request->get['filter_status'])) {

            $url .= '&filter_status=' . $this->request->get['filter_status'];

        }



        if (isset($this->request->get['filter_image'])) {

            $url .= '&filter_image=' . $this->request->get['filter_image'];

        }



        if (isset($this->request->get['sort'])) {

            $url .= '&sort=' . $this->request->get['sort'];

        }



        if (isset($this->request->get['order'])) {

            $url .= '&order=' . $this->request->get['order'];

        }



        $pagination = new Pagination();

        $pagination->total = $product_total;

        $pagination->page = $page;

        $pagination->limit = $this->config->get('config_limit_admin');

        $pagination->url = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url . '&page={page}', true);



        $data['pagination'] = $pagination->render();



        $data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($product_total - $this->config->get('config_limit_admin'))) ? $product_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $product_total, ceil($product_total / $this->config->get('config_limit_admin')));



        $data['filter_name'] = $filter_name;

        $data['filter_except_name'] = $filter_except_name;

        $data['filter_model'] = $filter_model;

        $data['filter_price'] = $filter_price;

        $data['filter_quantity'] = $filter_quantity;

        $data['filter_product_id'] = $filter_product_id;

        //$data['filter_category'] = $filter_category;

        //$data['filter_marka'] = $filter_marka;

        $data['filter_status'] = $filter_status;

        $data['filter_image'] = $filter_image;

        $data['filter_category_assigned'] = $filter_category_assigned;



        $data['sort'] = $sort;

        $data['order'] = $order;



        $data['header'] = $this->load->controller('common/header');

        $data['column_left'] = $this->load->controller('common/column_left');

        $data['footer'] = $this->load->controller('common/footer');


    if (file_exists(DIR_LOCAL_TEMPLATE . 'catalog/product_list.tpl')) {
        $this->response->setOutput($this->load->view(DIR_LOCAL_TEMPLATE . 'catalog/product_list', $data));

    } else {
        $this->response->setOutput($this->load->view(DIR_TEMPLATE . 'catalog/product_list', $data));
    }

    }

    protected function getForm() {

        $data['heading_title'] = $this->language->get('heading_title');



        $data['text_form'] = !isset($this->request->get['product_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');

        $data['text_enabled'] = $this->language->get('text_enabled');

        $data['text_disabled'] = $this->language->get('text_disabled');

        $data['text_none'] = $this->language->get('text_none');

        $data['text_yes'] = $this->language->get('text_yes');

        $data['text_no'] = $this->language->get('text_no');

        $data['text_plus'] = $this->language->get('text_plus');

        $data['text_minus'] = $this->language->get('text_minus');

        $data['text_default'] = $this->language->get('text_default');

        $data['text_option'] = $this->language->get('text_option');

        $data['text_option_value'] = $this->language->get('text_option_value');

        $data['text_select'] = $this->language->get('text_select');

        $data['text_percent'] = $this->language->get('text_percent');

        $data['text_amount'] = $this->language->get('text_amount');



        $data['text_product_type'] = $this->language->get('text_product_type');

        $data['text_product_type1'] = $this->language->get('text_product_type1');

        $data['text_product_type2'] = $this->language->get('text_product_type2');

        $data['text_product_type3'] = $this->language->get('text_product_type3');

        $data['text_product_type4'] = $this->language->get('text_product_type4');

        $data['text_product_type5'] = $this->language->get('text_product_type5');

        $data['text_product_type6'] = 'AirPods';



        $data['entry_name'] = $this->language->get('entry_name');

        $data['entry_description'] = $this->language->get('entry_description');

        $data['entry_meta_title'] = $this->language->get('entry_meta_title');

        $data['entry_custom_alt'] = $this->language->get('entry_custom_alt');

        $data['entry_custom_h1'] = $this->language->get('entry_custom_h1');

        $data['entry_custom_h2'] = $this->language->get('entry_custom_h2');

        $data['entry_custom_imgtitle'] = $this->language->get('entry_custom_imgtitle');

        $data['entry_meta_description'] = $this->language->get('entry_meta_description');

        $data['entry_meta_keyword'] = $this->language->get('entry_meta_keyword');

        $data['entry_keyword'] = $this->language->get('entry_keyword');

        $data['entry_model'] = $this->language->get('entry_model');

        $data['entry_sku'] = $this->language->get('entry_sku');

        $data['entry_upc'] = $this->language->get('entry_upc');

        $data['entry_ean'] = $this->language->get('entry_ean');

        $data['entry_jan'] = $this->language->get('entry_jan');

        $data['entry_isbn'] = $this->language->get('entry_isbn');

        $data['entry_mpn'] = $this->language->get('entry_mpn');

        $data['entry_location'] = $this->language->get('entry_location');

        $data['entry_minimum'] = $this->language->get('entry_minimum');

        $data['entry_shipping'] = $this->language->get('entry_shipping');

        $data['entry_payment_method'] = $this->language->get('entry_payment_method');

        $data['entry_shipping_method'] = $this->language->get('entry_shipping_method');

        $data['entry_date_available'] = $this->language->get('entry_date_available');

        $data['entry_quantity'] = $this->language->get('entry_quantity');

        $data['entry_stock_status'] = $this->language->get('entry_stock_status');

        $data['entry_price'] = $this->language->get('entry_price');

        $data['entry_tax_class'] = $this->language->get('entry_tax_class');

        $data['entry_points'] = $this->language->get('entry_points');

        $data['entry_option_points'] = $this->language->get('entry_option_points');

        $data['entry_subtract'] = $this->language->get('entry_subtract');

        $data['entry_weight_class'] = $this->language->get('entry_weight_class');

        $data['entry_weight'] = $this->language->get('entry_weight');

        $data['entry_dimension'] = $this->language->get('entry_dimension');

        $data['entry_length_class'] = $this->language->get('entry_length_class');

        $data['entry_length'] = $this->language->get('entry_length');

        $data['entry_width'] = $this->language->get('entry_width');

        $data['entry_height'] = $this->language->get('entry_height');

        $data['entry_image'] = $this->language->get('entry_image');

        $data['entry_additional_image'] = $this->language->get('entry_additional_image');

        $data['entry_store'] = $this->language->get('entry_store');

        $data['entry_manufacturer'] = $this->language->get('entry_manufacturer');

        $data['entry_download'] = $this->language->get('entry_download');

        $data['entry_category'] = $this->language->get('entry_category');

        $data['entry_filter'] = $this->language->get('entry_filter');

        $data['entry_related'] = $this->language->get('entry_related');

        $data['entry_attribute'] = $this->language->get('entry_attribute');

        $data['entry_text'] = $this->language->get('entry_text');

        $data['entry_option'] = $this->language->get('entry_option');

        $data['entry_option_value'] = $this->language->get('entry_option_value');

        $data['entry_required'] = $this->language->get('entry_required');

        $data['entry_sort_order'] = $this->language->get('entry_sort_order');

        $data['entry_status'] = $this->language->get('entry_status');

        $data['entry_date_start'] = $this->language->get('entry_date_start');

        $data['entry_date_end'] = $this->language->get('entry_date_end');

        $data['entry_priority'] = $this->language->get('entry_priority');

        $data['entry_tag'] = $this->language->get('entry_tag');

        $data['entry_customer_group'] = $this->language->get('entry_customer_group');

        $data['entry_reward'] = $this->language->get('entry_reward');

        $data['entry_layout'] = $this->language->get('entry_layout');

        $data['entry_recurring'] = $this->language->get('entry_recurring');



        $data['help_keyword'] = $this->language->get('help_keyword');

        $data['help_sku'] = $this->language->get('help_sku');

        $data['help_upc'] = $this->language->get('help_upc');

        $data['help_ean'] = $this->language->get('help_ean');

        $data['help_jan'] = $this->language->get('help_jan');

        $data['help_isbn'] = $this->language->get('help_isbn');

        $data['help_mpn'] = $this->language->get('help_mpn');

        $data['help_minimum'] = $this->language->get('help_minimum');

        $data['help_manufacturer'] = $this->language->get('help_manufacturer');

        $data['help_stock_status'] = $this->language->get('help_stock_status');

        $data['help_points'] = $this->language->get('help_points');

        $data['help_category'] = $this->language->get('help_category');

        $data['help_filter'] = $this->language->get('help_filter');

        $data['help_download'] = $this->language->get('help_download');

        $data['help_related'] = $this->language->get('help_related');

        $data['help_tag'] = $this->language->get('help_tag');



        $data['button_save'] = $this->language->get('button_save');

        $data['button_view'] = $this->language->get('button_view');

        $data['button_cancel'] = $this->language->get('button_cancel');

        $data['button_attribute_add'] = $this->language->get('button_attribute_add');

        $data['button_option_add'] = $this->language->get('button_option_add');

        $data['button_option_value_add'] = $this->language->get('button_option_value_add');

        $data['button_discount_add'] = $this->language->get('button_discount_add');

        $data['button_special_add'] = $this->language->get('button_special_add');

        $data['button_image_add'] = $this->language->get('button_image_add');

        $data['button_remove'] = $this->language->get('button_remove');

        $data['button_recurring_add'] = $this->language->get('button_recurring_add');



        $data['tab_general'] = $this->language->get('tab_general');

        $data['text_filemanager'] = ucfirst(strtolower($this->language->get('button_add') . ' ' . $this->language->get('tab_image')));

        $data['language_code'] = $this->language->get('code');



        $data['tab_data'] = $this->language->get('tab_data');

        $data['tab_attribute'] = $this->language->get('tab_attribute');

        $data['tab_option'] = $this->language->get('tab_option');

        $data['tab_recurring'] = $this->language->get('tab_recurring');

        $data['tab_discount'] = $this->language->get('tab_discount');

        $data['tab_special'] = $this->language->get('tab_special');

        $data['tab_image'] = $this->language->get('tab_image');



        $data['tab_links'] = $this->language->get('tab_links');

        $data['tab_reward'] = $this->language->get('tab_reward');

        $data['tab_design'] = $this->language->get('tab_design');

        $data['tab_openbay'] = $this->language->get('tab_openbay');





        if (isset($this->error['warning'])) {

            $data['error_warning'] = $this->error['warning'];

        } else {

            $data['error_warning'] = '';

        }



        if (isset($this->error['name'])) {

            $data['error_name'] = $this->error['name'];

        } else {

            $data['error_name'] = array();

        }



        if (isset($this->error['meta_title'])) {

            $data['error_meta_title'] = $this->error['meta_title'];

        } else {

            $data['error_meta_title'] = array();

        }



        if (isset($this->error['model'])) {

            $data['error_model'] = $this->error['model'];

        } else {

            $data['error_model'] = '';

        }



        if (isset($this->error['keyword'])) {

            $data['error_keyword'] = $this->error['keyword'];

        } else {

            $data['error_keyword'] = '';

        }



        $url = '';



        if (isset($this->request->get['filter_name'])) {

            $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));

        }

        if (isset($this->request->get['filter_except_name'])) {

            $url .= '&filter_except_name=' . urlencode(html_entity_decode($this->request->get['filter_except_name'], ENT_QUOTES, 'UTF-8'));

        }

        if (isset($this->request->get['filter_model'])) {

            $url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));

        }



        if (isset($this->request->get['filter_price'])) {

            $url .= '&filter_price=' . $this->request->get['filter_price'];

        }



        if (isset($this->request->get['filter_quantity'])) {

            $url .= '&filter_quantity=' . $this->request->get['filter_quantity'];

        }



        if (isset($this->request->get['filter_status'])) {

            $url .= '&filter_status=' . $this->request->get['filter_status'];

        }



        if (isset($this->request->get['sort'])) {

            $url .= '&sort=' . $this->request->get['sort'];

        }



        if (isset($this->request->get['order'])) {

            $url .= '&order=' . $this->request->get['order'];

        }



        if (isset($this->request->get['page'])) {

            $url .= '&page=' . $this->request->get['page'];

        }



        $data['breadcrumbs'] = array();



        $data['breadcrumbs'][] = array(

            'text' => $this->language->get('text_home'),

            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)

        );



        $data['breadcrumbs'][] = array(

            'text' => $this->language->get('heading_title'),

            'href' => $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, true)

        );





        if (isset($this->request->get['product_id'])) {

            $data['view'] = '/index.php?route=product/product&product_id=' . $this->request->get['product_id'];

        } else {

            $data['view'] = '';

        }



        $data['cancel'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, true);





        if (!isset($this->request->get['product_id'])) {

            $data['action'] = $this->url->link('catalog/product/add', 'token=' . $this->session->data['token'] . $url, true);

        } else {

            $data['action'] = $this->url->link('catalog/product/edit', 'token=' . $this->session->data['token'] . '&product_id=' . $this->request->get['product_id'] . $url, true);

        }



        $data['cancel'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, true);




        $data['product_id'] = $this->request->get['product_id'];
        if (isset($this->request->get['product_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {

            $product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);

        }







        $data['token'] = $this->session->data['token'];



        $this->load->model('localisation/language');



        $data['languages'] = $this->model_localisation_language->getLanguages();



        if (isset($this->request->post['product_description'])) {

            $data['product_description'] = $this->request->post['product_description'];

        } elseif (isset($this->request->get['product_id'])) {

            $data['product_description'] = $this->model_catalog_product->getProductDescriptions($this->request->get['product_id']);

        } else {

            $data['product_description'] = array();

        }







        if (isset($this->request->post['model'])) {

            $data['model'] = $this->request->post['model'];

        } elseif (!empty($product_info)) {

            $data['model'] = $product_info['model'];

        } else {

            $data['model'] = '';

        }



        if (isset($this->request->post['barcode'])) {

            $data['barcode'] = $this->request->post['barcode'];

        } elseif (!empty($product_info)) {

            $data['barcode'] = $product_info['product_stock_id'];

        } else {

            $data['barcode'] = '';

        }



        if (isset($this->request->post['keyword'])) {

            $data['keyword'] = $this->request->post['keyword'];

        } elseif (!empty($product_info)) {

            $data['keyword'] = $this->model_catalog_product->getKeyWords($this->request->get['product_id']);

        } else {

            $data['keyword'] = array();

        }

        if (isset($this->request->post['product_payment'])) {

            $data['product_payment'] = $this->request->post['product_payment'];

        } elseif (!empty($product_info['product_payment'])) {

            $data['product_payment'] = $product_info['product_payment'];

        } else {

            $data['product_payment'] = '';

        }

        if (isset($this->request->post['product_shipping'])) {

            $data['product_shipping'] = $this->request->post['product_shipping'];

        } elseif (!empty($product_info['product_shipping'])) {

            $data['product_shipping'] = $product_info['product_shipping'];

        } else {

            $data['product_shipping'] = '';

        }



        //Payment Methods

        $this->load->model('extension/extension');

        $data['payment_methods'] = array();

        $extensions = $this->model_extension_extension->getInstalled('payment');

        foreach ($extensions as $code) {

            if ($this->config->get($code . '_status')) {

                $this->language->load('extension/payment/' . $code);

                $data['payment_methods'][$code] = $this->language->get('heading_title');

            }

        }

        //Shipping Methods



        $data['shipping_methods'] = array();

        $extensions = $this->model_extension_extension->getInstalled('shipping');

        foreach ($extensions as $code) {

            if ($this->config->get($code . '_status')) {

                $this->language->load('extension/shipping/' . $code);

                $data['shipping_methods'][$code] = $this->language->get('heading_title');

            }

        }



        if (isset($this->request->post['shipping'])) {

            $data['shipping'] = $this->request->post['shipping'];

        } elseif (!empty($product_info)) {

            $data['shipping'] = $product_info['shipping'];

        } else {

            $data['shipping'] = 1;

        }



        if (isset($this->request->post['product_type'])) {

            $data['product_type'] = $this->request->post['product_type'];

        } elseif (!empty($product_info)) {

            $data['product_type'] = $product_info['product_type'];

        } else {

            $data['product_type'] = 1;

        }



        if (isset($this->request->post['product_showcase'])) {

            $data['product_showcase'] = $this->request->post['product_showcase'];

        } elseif (!empty($product_info)) {

            $data['product_showcase'] = $product_info['product_showcase'];

        } else {

            $data['product_showcase'] = 1;

        }



        $this->load->model('localisation/currency');

        $data['currencies'] = $this->model_localisation_currency->getCurrencies();



        if (isset($this->request->post['currency_id'])) {

            $data['currency_id'] = $this->request->post['currency_id'];

        } elseif (!empty($product_info)) {

            $data['currency_id'] = $product_info['currency_id'];

        } else {

            $data['currency_id'] = '';

        }



        if (isset($this->request->post['price'])) {

            $data['price'] = $this->request->post['price'];

        } elseif (!empty($product_info)) {

            $data['price'] = $product_info['price'];

        } else {

            $data['price'] = '';

        }



        $this->load->model('catalog/recurring');



        $data['recurrings'] = $this->model_catalog_recurring->getRecurrings();



        if (isset($this->request->post['product_recurrings'])) {

            $data['product_recurrings'] = $this->request->post['product_recurrings'];

        } elseif (!empty($product_info)) {

            $data['product_recurrings'] = $this->model_catalog_product->getRecurrings($product_info['product_id']);

        } else {

            $data['product_recurrings'] = array();

        }



        $this->load->model('localisation/tax_class');



        $data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses();



        if (isset($this->request->post['tax_class_id'])) {

            $data['tax_class_id'] = $this->request->post['tax_class_id'];

        } elseif (!empty($product_info)) {

            $data['tax_class_id'] = $product_info['tax_class_id'];

        } else {

            $data['tax_class_id'] = 0;

        }



        if (isset($this->request->post['date_available'])) {

            $data['date_available'] = $this->request->post['date_available'];

        } elseif (!empty($product_info)) {

            $data['date_available'] = ($product_info['date_available'] != '0000-00-00') ? $product_info['date_available'] : '';

        } else {

            $data['date_available'] = date('Y-m-d');

        }



        if (isset($this->request->post['quantity'])) {

            $data['quantity'] = $this->request->post['quantity'];

        } elseif (!empty($product_info)) {

            $data['quantity'] = $product_info['quantity'];

        } else {

            $data['quantity'] = 1;

        }



        if (isset($this->request->post['minimum'])) {

            $data['minimum'] = $this->request->post['minimum'];

        } elseif (!empty($product_info)) {

            $data['minimum'] = $product_info['minimum'];

        } else {

            $data['minimum'] = 1;

        }



        if (isset($this->request->post['subtract'])) {

            $data['subtract'] = $this->request->post['subtract'];

        } elseif (!empty($product_info)) {

            $data['subtract'] = $product_info['subtract'];

        } else {

            $data['subtract'] = 1;

        }



        if (isset($this->request->post['sort_order'])) {

            $data['sort_order'] = $this->request->post['sort_order'];

        } elseif (!empty($product_info)) {

            $data['sort_order'] = $product_info['sort_order'];

        } else {

            $data['sort_order'] = 1;

        }



        $this->load->model('localisation/stock_status');



        $data['stock_statuses'] = $this->model_localisation_stock_status->getStockStatuses();



        if (isset($this->request->post['stock_status_id'])) {

            $data['stock_status_id'] = $this->request->post['stock_status_id'];

        } elseif (!empty($product_info)) {

            $data['stock_status_id'] = $product_info['stock_status_id'];

        } else {

            $data['stock_status_id'] = 1;

        }



        if (isset($this->request->post['status'])) {

            $data['status'] = $this->request->post['status'];

        } elseif (!empty($product_info)) {

            $data['status'] = $product_info['status'];

        } else {

            $data['status'] = true;

        }



        if (isset($this->request->post['weight'])) {

            $data['weight'] = $this->request->post['weight'];

        } elseif (!empty($product_info)) {

            $data['weight'] = $product_info['weight'];

        } else {

            $data['weight'] = '';

        }



        $this->load->model('localisation/weight_class');



        $data['weight_classes'] = $this->model_localisation_weight_class->getWeightClasses();



        if (isset($this->request->post['weight_class_id'])) {

            $data['weight_class_id'] = $this->request->post['weight_class_id'];

        } elseif (!empty($product_info)) {

            $data['weight_class_id'] = $product_info['weight_class_id'];

        } else {

            $data['weight_class_id'] = $this->config->get('config_weight_class_id');

        }



        if (isset($this->request->post['length'])) {

            $data['length'] = $this->request->post['length'];

        } elseif (!empty($product_info)) {

            $data['length'] = $product_info['length'];

        } else {

            $data['length'] = '';

        }



        if (isset($this->request->post['width'])) {

            $data['width'] = $this->request->post['width'];

        } elseif (!empty($product_info)) {

            $data['width'] = $product_info['width'];

        } else {

            $data['width'] = '';

        }



        if (isset($this->request->post['height'])) {

            $data['height'] = $this->request->post['height'];

        } elseif (!empty($product_info)) {

            $data['height'] = $product_info['height'];

        } else {

            $data['height'] = '';

        }



        $this->load->model('localisation/length_class');



        $data['length_classes'] = $this->model_localisation_length_class->getLengthClasses();



        if (isset($this->request->post['length_class_id'])) {

            $data['length_class_id'] = $this->request->post['length_class_id'];

        } elseif (!empty($product_info)) {

            $data['length_class_id'] = $product_info['length_class_id'];

        } else {

            $data['length_class_id'] = $this->config->get('config_length_class_id');

        }



        $this->load->model('catalog/manufacturer');



        if (isset($this->request->post['manufacturer_id'])) {

            $data['manufacturer_id'] = $this->request->post['manufacturer_id'];

        } elseif (!empty($product_info)) {

            $data['manufacturer_id'] = $product_info['manufacturer_id'];

        } else {

            $data['manufacturer_id'] = 0;

        }



        if (isset($this->request->post['manufacturer'])) {

            $data['manufacturer'] = $this->request->post['manufacturer'];

        } elseif (!empty($product_info)) {

            $manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($product_info['manufacturer_id']);



            if ($manufacturer_info) {

                $data['manufacturer'] = $manufacturer_info['name'];

            } else {

                $data['manufacturer'] = '';

            }

        } else {

            $data['manufacturer'] = '';

        }



        // Categories

        $this->load->model('catalog/category');



        if (isset($this->request->post['product_category'])) {

            $categories = $this->request->post['product_category'];

        } elseif (isset($this->request->get['product_id'])) {

            $categories = $this->model_catalog_product->getProductCategories($this->request->get['product_id']);

        } else {

            $categories = array();

        }



        $data['product_categories'] = array();



        foreach ($categories as $category_id) {

            $category_info = $this->model_catalog_category->getCategory($category_id);



            if ($category_info) {

                $data['product_categories'][] = array(

                    'category_id' => $category_info['category_id'],

                    'name' => ($category_info['path']) ? $category_info['path'] . ' &gt; ' . $category_info['name'] : $category_info['name']

                );

            }

        }



        // Filters

        $this->load->model('catalog/filter');



        if (isset($this->request->post['product_filter'])) {

            $filters = $this->request->post['product_filter'];

        } elseif (isset($this->request->get['product_id'])) {

            $filters = $this->model_catalog_product->getProductFilters($this->request->get['product_id']);

        } else {

            $filters = array();

        }



        $data['product_filters'] = array();



        foreach ($filters as $filter_id) {

            $filter_info = $this->model_catalog_filter->getFilter($filter_id);



            if ($filter_info) {

                $data['product_filters'][] = array(

                    'filter_id' => $filter_info['filter_id'],

                    'name' => $filter_info['group'] . ' &gt; ' . $filter_info['name']

                );

            }

        }



        // Attributes

        $this->load->model('catalog/attribute');



        if (isset($this->request->post['product_attribute'])) {

            $product_attributes = $this->request->post['product_attribute'];

        } elseif (isset($this->request->get['product_id'])) {

            $product_attributes = $this->model_catalog_product->getProductAttributes($this->request->get['product_id']);

        } else {

            $product_attributes = array();

        }



        $data['product_attributes'] = array();



        foreach ($product_attributes as $product_attribute) {

            $attribute_info = $this->model_catalog_attribute->getAttribute($product_attribute['attribute_id']);



            if ($attribute_info) {

                $data['product_attributes'][] = array(

                    'attribute_id' => $product_attribute['attribute_id'],

                    'name' => $attribute_info['name'],

                    'product_attribute_description' => $product_attribute['product_attribute_description']

                );

            }

        }



        // Options

        $this->load->model('catalog/option');

        $this->load->model('tool/image');



        if (isset($this->request->post['product_option'])) {

            $product_options = $this->request->post['product_option'];

        } elseif (isset($this->request->get['product_id'])) {

            $product_options = $this->model_catalog_product->getProductOptions($this->request->get['product_id']);

        } else {

            $product_options = array();

        }



        $data['product_options'] = array();

        foreach ($product_options as $product_option) {

            $product_option_value_data = array();



            if (isset($product_option['product_option_value'])) {

                foreach ($product_option['product_option_value'] as $product_option_value) {

                    $option_thumb_image = array();

                    if ($product_option_value['option_thumb_image']) {

                        foreach ($product_option_value['option_thumb_image'] as $thumb_image) {

                            $option_thumb_image[] = array(

                                'src' => $this->model_tool_image->resize($thumb_image, 100, 100),

                                'value' => $thumb_image

                            );

                        }

                    }



                    $product_option_value_data[] = array(

                        'product_option_value_id' => $product_option_value['product_option_value_id'],

                        'option_value_id' => $product_option_value['option_value_id'],

                        'sub_option_id' => $product_option_value['sub_option_id'],

                        'sub_option_value_id' => $product_option_value['sub_option_value_id'],

                        'quantity' => $product_option_value['quantity'],

                        'subtract' => $product_option_value['subtract'],

                        'price' => $product_option_value['price'],

                        'price_prefix' => $product_option_value['price_prefix'],

                        'points' => $product_option_value['points'],

                        'points_prefix' => $product_option_value['points_prefix'],

                        'customer_group_id' => $product_option_value['customer_group_id'],

                        'option_thumb_image' => $option_thumb_image,

                        'option_value_barcode' => $product_option_value['option_value_barcode'],

                        'weight' => $product_option_value['weight'],

                        'weight_prefix' => $product_option_value['weight_prefix'],
                        'logo_reference' => $product_option_value['logo_reference'],
                        'logo_code' => $product_option_value['logo_code'],
                        'logo_message' => $product_option_value['logo_message'],
                    );

                }

            }



            $data['product_options'][] = array(

                'product_option_id' => $product_option['product_option_id'],

                'product_option_value' => $product_option_value_data,

                'option_id' => $product_option['option_id'],

                'sub_option_id' => $product_option['sub_option_id'],

                'name' => $product_option['name'],

                'type' => $product_option['type'],

                'value' => isset($product_option['value']) ? $product_option['value'] : '',

                'required' => $product_option['required']

            );

        }



        $data['option_values'] = array();

        $data['sub_option_values'] = array();



        foreach ($data['product_options'] as $product_option) {

            if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {

                if (!isset($data['option_values'][$product_option['option_id']])) {

                    $data['option_values'][$product_option['option_id']] = $this->model_catalog_option->getOptionValues($product_option['option_id']);

                }

                if (!isset($data['sub_option_values'][$product_option['sub_option_id']])) {

                    $data['sub_option_values'][$product_option['sub_option_id']] = $this->model_catalog_option->getOptionValues($product_option['sub_option_id']);

                }

            }

        }



        $this->load->model('customer/customer_group');



        $data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();



        if (isset($this->request->post['product_discount'])) {

            $product_discounts = $this->request->post['product_discount'];

        } elseif (isset($this->request->get['product_id'])) {

            $product_discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);

        } else {

            $product_discounts = array();

        }



        $data['product_discounts'] = array();



        foreach ($product_discounts as $product_discount) {

            $data['product_discounts'][] = array(

                'customer_group_id' => $product_discount['customer_group_id'],

                'quantity' => $product_discount['quantity'],

                'priority' => $product_discount['priority'],

                'price' => $product_discount['price'],

                'date_start' => ($product_discount['date_start'] != '0000-00-00') ? $product_discount['date_start'] : '',

                'date_end' => ($product_discount['date_end'] != '0000-00-00') ? $product_discount['date_end'] : ''

            );

        }



        if (isset($this->request->post['product_special'])) {

            $product_specials = $this->request->post['product_special'];

        } elseif (isset($this->request->get['product_id'])) {

            $product_specials = $this->model_catalog_product->getProductSpecials($this->request->get['product_id']);

        } else {

            $product_specials = array();

        }



        $data['product_specials'] = array();



        foreach ($product_specials as $product_special) {

            $data['product_specials'][] = array(

                'customer_group_id' => $product_special['customer_group_id'],

                'priority' => $product_special['priority'],

                'price' => $product_special['price'],

                'date_start' => ($product_special['date_start'] != '0000-00-00') ? $product_special['date_start'] : '',

                'date_end' => ($product_special['date_end'] != '0000-00-00') ? $product_special['date_end'] : ''

            );

        }





        // Pattern

        if (isset($this->request->post['pattern'])) {

            $data['pattern'] = $this->request->post['pattern'];

        } elseif (!empty($product_info)) {

            $data['pattern'] = $product_info['pattern'];

        } else {

            $data['pattern'] = '';

        }



        if (isset($this->request->post['pattern']) && is_file(DIR_IMAGE . $this->request->post['pattern'])) {

            $data['pattern_thumb'] = $this->model_tool_image->resize($this->request->post['pattern'], 100, 100);

        } elseif (!empty($product_info) && is_file(DIR_IMAGE . $product_info['pattern'])) {

            $data['pattern_thumb'] = $this->model_tool_image->resize($product_info['pattern'], 100, 100);

        } else {

            $data['pattern_thumb'] = $this->model_tool_image->resize('no_image.png', 100, 100);

        }



        // Media

        if (isset($this->request->post['media'])) {

            $data['media'] = $this->request->post['media'];

        } elseif (!empty($product_info)) {

            $data['media'] = $product_info['media'];

        } else {

            $data['media'] = '';

        }

        // pattern names
        if (isset($this->request->post['pattern_names'])) {

            $data['pattern_names'] = $this->request->post['pattern_names'];

        }elseif (!empty($product_info)) {

            $data['pattern_names'] = $product_info['pattern_names'];

        } else{

            $data['pattern_names'] = "";

        }
        // cover names
        if (isset($this->request->post['cover_names'])) {

            $data['cover_names'] = $this->request->post['cover_names'];

        }elseif (!empty($product_info)) {

            $data['cover_names'] = $product_info['cover_names'];

        } else{

            $data['cover_names'] = "";

        }

        // Mask

        if (isset($this->request->post['mask'])) {

            $data['mask'] = $this->request->post['mask'];

        } elseif (!empty($product_info)) {

            $data['mask'] = $product_info['mask'];

        } else {

            $data['mask'] = '';

        }

        if (isset($this->request->post['mask']) && is_file(DIR_IMAGE . $this->request->post['mask'])) {

            $data['mask_thumb'] = $this->model_tool_image->resize($this->request->post['mask'], 100, 100);

        } elseif (!empty($product_info) && is_file(DIR_IMAGE . $product_info['mask'])) {

            $data['mask_thumb'] = $this->model_tool_image->resize($product_info['mask'], 100, 100);

        } else {

            $data['mask_thumb'] = $this->model_tool_image->resize('no_image.png', 100, 100);

        }



        $data['media_thumb_extension'] = '';

        if (isset($this->request->post['media']) && is_file(DIR_IMAGE . $this->request->post['media'])) {

            $data['media_thumb'] = $this->request->post['media'];

        } elseif (!empty($product_info) && is_file(DIR_IMAGE . $product_info['media'])) {

            $media = explode('.', $product_info['media']);

            if ($media[1] != 'mp4' AND $media[1] != 'webm') {

                $data['media_thumb'] = $this->model_tool_image->resize($product_info['media'], 100, 100);

                $data['media_thumb_extension'] = 'image';

            } else {

                $data['media_thumb'] = HTTP_CATALOG . 'image/' . $media[0];

                $data['media_thumb_extension'] = 'video';

            }

        } else {

            $data['media_thumb'] = '';

        }





        // Image

        if (isset($this->request->post['image'])) {
            $data['image'] = $this->request->post['image'];
        } elseif (!empty($product_info)) {
            $data['image'] = $product_info['image'];
        } else {
            $data['image'] = '';
        }

        if (isset($this->request->post['image_vitrin'])) {
            $data['image_vitrin'] = $this->request->post['image'];
        } elseif (!empty($product_info)) {
            $data['image_vitrin'] = $product_info['image_vitrin'];
        } else {
            $data['image_vitrin'] = '';
        }


        if (isset($this->request->post['image']) && is_file(DIR_IMAGE . $this->request->post['image'])) {
            $data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
        } elseif (!empty($product_info) && is_file(DIR_IMAGE . $product_info['image'])) {
            $data['thumb'] = $this->model_tool_image->resize($product_info['image'], 100, 100);
        } else {
            $data['thumb'] = $this->model_tool_image->resize('no_image.png', 100, 100);
        }

        if (isset($this->request->post['image_vitrin']) && is_file(DIR_IMAGE . $this->request->post['image_vitrin'])) {
            $data['thumb_vitrin'] = $this->model_tool_image->resize($this->request->post['image_vitrin'], 100, 100);
        } elseif (!empty($product_info) && is_file(DIR_IMAGE . $product_info['image_vitrin'])) {
            $data['thumb_vitrin'] = $this->model_tool_image->resize($product_info['image_vitrin'], 100, 100);
        } else {
            $data['thumb_vitrin'] = $this->model_tool_image->resize('no_image.png', 100, 100);
        }



        $data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);



        // Images

        if (isset($this->request->post['product_image'])) {

            $product_images = $this->request->post['product_image'];

        } elseif (isset($this->request->get['product_id'])) {

            $product_images = $this->model_catalog_product->getProductImages($this->request->get['product_id']);

        } else {

            $product_images = array();

        }



        $data['product_images'] = array();



        foreach ($product_images as $product_image) {

            if (is_file(DIR_IMAGE . $product_image['image'])) {

                $image = $product_image['image'];

                $thumb = $product_image['image'];

            } else {

                $image = '';

                $thumb = 'no_image.png';

            }



            $data['product_images'][] = array(

                'image' => $image,

                'thumb' => $this->model_tool_image->resize($thumb, 100, 100),

                'sort_order' => $product_image['sort_order']

            );

        }



        // Downloads

        $this->load->model('catalog/download');



        if (isset($this->request->post['product_download'])) {

            $product_downloads = $this->request->post['product_download'];

        } elseif (isset($this->request->get['product_id'])) {

            $product_downloads = $this->model_catalog_product->getProductDownloads($this->request->get['product_id']);

        } else {

            $product_downloads = array();

        }



        $data['product_downloads'] = array();



        foreach ($product_downloads as $download_id) {

            $download_info = $this->model_catalog_download->getDownload($download_id);



            if ($download_info) {

                $data['product_downloads'][] = array(

                    'download_id' => $download_info['download_id'],

                    'name' => $download_info['name']

                );

            }

        }



        if (isset($this->request->post['product_related'])) {

            $products = $this->request->post['product_related'];

        } elseif (isset($this->request->get['product_id'])) {

            $products = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);

        } else {

            $products = array();

        }



        $data['product_relateds'] = array();



        foreach ($products as $product_id) {

            $related_info = $this->model_catalog_product->getProduct($product_id);



            if ($related_info) {

                $data['product_relateds'][] = array(

                    'product_id' => $related_info['product_id'],

                    'name' => $related_info['name']

                );

            }

        }



        if (isset($this->request->post['points'])) {

            $data['points'] = $this->request->post['points'];

        } elseif (!empty($product_info)) {

            $data['points'] = $product_info['points'];

        } else {

            $data['points'] = '';

        }



        if (isset($this->request->post['product_reward'])) {

            $data['product_reward'] = $this->request->post['product_reward'];

        } elseif (isset($this->request->get['product_id'])) {

            $data['product_reward'] = $this->model_catalog_product->getProductRewards($this->request->get['product_id']);

        } else {

            $data['product_reward'] = array();

        }

        /**
         * 15.09.20 idle stock field added (ali.a)
         */
        if (isset($this->request->post['idle_stock'])) {

            $data['idle_stock'] = $this->request->post['idle_stock'];

        } elseif (!empty($product_info)) {

            $data['idle_stock'] = $product_info['idle_stock'];

        } else {

            $data['idle_stock'] = '';

        }



        // multi image uploader

        $this->document->addScript('//scripts.piyersoft.com/javascript/fileuploader.js');

        $this->document->addStyle('//scripts.piyersoft.com/stylesheet/fileuploader.css');

        // multi image uploader

        $this->load->model('design/layout');



        $data['layouts'] = $this->model_design_layout->getLayouts();



        $data['header'] = $this->load->controller('common/header');

        $data['column_left'] = $this->load->controller('common/column_left');

        $data['footer'] = $this->load->controller('common/footer');







        if (file_exists(DIR_LOCAL_TEMPLATE . 'catalog/product_form.tpl')) {

            $this->response->setOutput($this->load->view(DIR_LOCAL_TEMPLATE . 'catalog/product_form', $data));

        } else {

            $this->response->setOutput($this->load->view(DIR_TEMPLATE . 'catalog/product_form', $data));

        }

    }

    public function excel_exportt(){


        $this->load->model('catalog/product');



        $results = $this->model_catalog_product->getproductsbymodelforexcelreport();

        require_once DIR_SYSTEM . 'PHPExcel/Classes/PHPExcel.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Product name')
            ->setCellValue('B1', 'Product Model')
            ->setCellValue('C1', 'Product Image')
            ->setCellValue('D1', 'Product price')
            ->setCellValue('E1', 'Product Status');


        $file_name = $this->config->get('config_name').'-'.@date("Y-m-d-His")."-products.xls" ;

        $row_i = 2;

        foreach ($results as $result) {




            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A' . $row_i, $result['name'])
                ->setCellValue('B' . $row_i, $result['model'])
                ->setCellValue('C' . $row_i, 'https://www.deercase.com/image/'.$result['image'])
                ->setCellValue('D' . $row_i, $result['price'])
                ->setCellValue('E' . $row_i, $result['status']);
            $row_i++;


        }

        $objPHPExcel->getActiveSheet()->setTitle('Simple');

        $objPHPExcel->setActiveSheetIndex(0);

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$file_name.'"');
        header('Cache-Control: max-age=0');

        header('Cache-Control: max-age=1');

        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }

    protected function validateForm() {



        if (!$this->user->hasPermission('modify', 'catalog/product')) {

            $this->error['warning'] = $this->language->get('error_permission');

        }



        foreach ($this->request->post['product_description'] as $language_id => $value) {

            if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 255)) {

                $this->error['name'][$language_id] = $this->language->get('error_name');

            }



            if ((utf8_strlen($value['meta_title']) < 3) || (utf8_strlen($value['meta_title']) > 255)) {

                $this->error['meta_title'][$language_id] = $this->language->get('error_meta_title');

            }

        }



        /* if ((utf8_strlen($this->request->post['model']) < 1) || (utf8_strlen($this->request->post['model']) > 64)) {

          $this->error['model'] = $this->language->get('error_model');

          } */



        if (utf8_strlen(current($this->request->post['keyword'])) > 0) {

            $this->load->model('catalog/url_alias');



            $url_alias_info = $this->model_catalog_url_alias->getUrlAlias(current($this->request->post['keyword']));



            if ($url_alias_info && isset($this->request->get['product_id']) && $url_alias_info['query'] != 'product_id=' . $this->request->get['product_id']) {

                $this->error['keyword'] = sprintf($this->language->get('error_keyword'));

            }



            if ($url_alias_info && !isset($this->request->get['product_id'])) {

                $this->error['keyword'] = sprintf($this->language->get('error_keyword'));

            }

        }





        if ($this->error && !isset($this->error['warning'])) {

            $this->error['warning'] = $this->language->get('error_warning');

        }



        return !$this->error;

    }

    protected function validateEditStatus() {

        if (!$this->user->hasPermission('modify', 'catalog/product')) {

            $this->error['warning'] = $this->language->get('error_permission');

        }



        return !$this->error;

    }

    protected function validateDelete() {

        if (!$this->user->hasPermission('modify', 'catalog/product')) {

            $this->error['warning'] = $this->language->get('error_permission');

        }



        return !$this->error;

    }

    protected function validateCopy() {

        if (!$this->user->hasPermission('modify', 'catalog/product')) {

            $this->error['warning'] = $this->language->get('error_permission');

        }



        return !$this->error;

    }

    public function autocomplete() {

        $this->load->model('tool/image');



        if (isset($this->request->get['filter_status']) && $this->request->get['filter_status'] != '*') {

            $filter_status = $this->request->get['filter_status'];

        } else {

            $filter_status = null;

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

        $json = array();



        if (isset($this->request->get['filter_name']) || isset($this->request->get['filter_model'])) {

            $this->load->model('catalog/product');

            $this->load->model('catalog/option');



            if (isset($this->request->get['filter_name'])) {

                $filter_name = $this->request->get['filter_name'];

            } else {

                $filter_name = '';

            }



            if (isset($this->request->get['filter_model'])) {

                $filter_model = $this->request->get['filter_model'];

            } else {

                $filter_model = '';

            }



            if (isset($this->request->get['limit'])) {

                $limit = $this->request->get['limit'];

            } else {

                $limit = 5;

            }



            if (isset($this->request->get['filter_category_id'])) {

                $filter_category_id = $this->request->get['filter_category_id'];

            } else {

                $filter_category_id = '';

            }



            $filter_data = array(

                'filter_name' => $filter_name,

                'filter_from' => $filter_from,

                'filter_model' => $filter_model,

                'start' => 0,

                'limit' => $limit,

                'filter_status' => $filter_status,

                'filter_category_id' => $filter_category_id

            );



            $results = $this->model_catalog_product->getProducts($filter_data);



            foreach ($results as $result) {

                $option_data = array();



                $product_options = $this->model_catalog_product->getProductOptions($result['product_id']);



                foreach ($product_options as $product_option) {

                    $option_info = $this->model_catalog_option->getOption($product_option['option_id']);



                    if ($option_info) {

                        $product_option_value_data = array();



                        foreach ($product_option['product_option_value'] as $product_option_value) {

                            $option_value_info = $this->model_catalog_option->getOptionValue($product_option_value['option_value_id']);



                            if ($option_value_info) {

                                $product_option_value_data[] = array(

                                    'product_option_value_id' => $product_option_value['product_option_value_id'],

                                    'option_value_id' => $product_option_value['option_value_id'],

                                    'name' => $option_value_info['name'],

                                    'price' => (float) $product_option_value['price'] ? $this->currency->format($product_option_value['price'], $this->config->get('config_currency')) : false,

                                    'price_prefix' => $product_option_value['price_prefix']

                                );

                            }

                        }



                        $option_data[] = array(

                            'product_option_id' => $product_option['product_option_id'],

                            'product_option_value' => $product_option_value_data,

                            'option_id' => $product_option['option_id'],

                            'name' => $option_info['name'],

                            'type' => $option_info['type'],

                            'value' => $product_option['value'],

                            'required' => $product_option['required']

                        );

                    }

                }



                $json[] = array(

                    'image' => $result['image'] ? $this->model_tool_image->resize($result['image'], 40, 40) : $this->model_tool_image->resize('no_image.png', 40, 40),
'product_image' => $result['image'] ? $this->model_tool_image->resize($result['image'], 100, 100) : $this->model_tool_image->resize('no_image.png', 100, 100),
'product_type' => $result['product_type'],
                    'quantity' => $result['quantity'],

                    'status' => $result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),

                    'edit' => $this->url->link('catalog/product/edit', 'token=' . $this->session->data['token'] . '&product_id=' . $result['product_id'] . '&filter_name=' . str_replace('%', '', $filter_name) . '&filter_model=' . $filter_model . '&limit=' . $limit . ($filter_status ? '&filter_status=' . $filter_status : ''), true),

                    'product_id' => $result['product_id'],

                    'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),

                    'model' => $result['model'],

                    'option' => $option_data,

                    'price' => $result['price']

                );

            }

        }



        $this->response->addHeader('Content-Type: application/json');

        $this->response->setOutput(json_encode($json));

    }

    public function barcode() {
        $product_id = $this->request->get['product_id'];
        
        $this->load->model('catalog/product');
        
        // ürün verisini veritabanından getir.
        $product_info = $this->model_catalog_product->getProduct($product_id);

        if(!empty($product_info)) {
            $option_value_id = !empty($this->request->get['option_value_id']) ?  $this->request->get['option_value_id'] : "";
            require_once(DIR_SYSTEM . 'picqer/php-barcode-generator/generate-verified-files.php');
            $generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG();

            if(!empty($option_value_id)) {
                $option_value_info = $this->db->query("SELECT name FROM " . DB_PREFIX ."option_value_description WHERE option_value_id = {$option_value_id} ")->row;
                $barcode_image = $generatorJPG->getBarcode($product_id.$option_value_id, $generatorJPG::TYPE_CODE_128);

                $data = [
                    'barcode_image' => 'data:image/jpeg;base64,' . base64_encode($barcode_image),
                    'barcode_text' => $product_id . $option_value_id,
                    'name' => $product_info['name'] . ' ' . $option_value_info['name']
                ];     
            } else {
                $barcode_image = $generatorJPG->getBarcode($product_id, $generatorJPG::TYPE_CODE_128);

                $data = [
                    'barcode_image' => 'data:image/jpeg;base64,' . base64_encode($barcode_image),
                    'barcode_text' => $product_id,
                    'name' => $product_info['name']
                ];                
            }


            $html = $this->load->view(DIR_LOCAL_TEMPLATE . 'catalog/product_barcode', $data);

            require_once(DIR_SYSTEM . 'dompdf/autoload.inc.php');

            $dompdf = new Dompdf\Dompdf(array('enable_remote' => true, 'debugLayout' => false));
            $dompdf->loadHtml($html);
    
           // $customPaper = array(0, 0, 284, 60); // 7.5cm, 1.5cm
            $customPaper = array(0, 0, 377, 60); // 10cm, 1.5cm

            $dompdf->set_paper($customPaper);
    
            $dompdf->render();
            // Output the generated PDF to Browser
    
            $dompdf->stream($product_id . ".pdf", array("Attachment" => false));
        }
    }

    private function creatAllCache() {

        $url = HTTPS_CATALOG . '';



        $links = array(

            HTTPS_CATALOG . 'index.php?route=product/category/createcache',

            HTTPS_CATALOG . 'index.php?route=product/manufacturer/createcache',

            //HTTPS_CATALOG . 'index.php?route=extension/module/featured/createcache',

            HTTPS_CATALOG . 'index.php?route=common/homepagetabs/createcache',

            HTTPS_CATALOG . 'index.php?route=tool/settingcache/createcache',

        );



        foreach ($links as $key => $link) {

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $link);

            curl_setopt($ch, CURLOPT_POST, 1);

            curl_setopt($ch, CURLOPT_POSTFIELDS, "");

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            $server_output = curl_exec($ch);

            curl_close($ch);

        }





        //var_dump($server_output); die();

    }

    public function sendLogo() {
        // product_id yoksa ve istek post değilse işlemi durdur.
        if (empty($this->request->post['product_id']) || $this->request->server['REQUEST_METHOD'] !== 'POST') {
            return;
        }

        $this->load->model('catalog/product');

        $product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']);

        if(!empty($product_options) && empty($this->request->post['option_value_id'])) {
            $response = [
                'status' => 'failed',
                'message' => "Seçeneği bulunan ürünün ana ürünü aktarılamaz."
            ];

            $this->response->addHeader('Content-Type: application/json');
            $this->response->setOutput(json_encode($response));
            return;
        }

        $option_value_id = !empty($this->request->post['option_value_id']) ? $this->request->post['option_value_id'] : "";
        $product_id = $this->request->post['product_id'];

        $product_info = $this->model_catalog_product->getProduct($product_id);

        $category = $this->db->query("SELECT
            c.category_id,
            cd.`name`, 
            c.infinityeditid
        FROM
            ps_product_to_category AS ptc
            INNER JOIN ps_category AS c ON ptc.category_id = c.category_id
            INNER JOIN ps_category_description AS cd ON c.category_id = cd.category_id 
        WHERE
            ptc.product_id = {$product_id}   
            AND c.is_logo_material = 1
        ")->rows;
    
        $category = $category[0];

        $color = $this->db->query("SELECT ad.`name`, a.infinityeditid, a.attribute_id 
            FROM ps_product AS p 
            INNER JOIN ps_product_attribute AS pa ON p.product_id = pa.product_id
            INNER JOIN ps_attribute AS a ON pa.attribute_id = a.attribute_id
            INNER JOIN ps_attribute_description AS ad ON a.attribute_id = ad.attribute_id
            INNER JOIN ps_attribute_group AS ag ON a.attribute_group_id = ag.attribute_group_id 
            WHERE a.attribute_group_id = 13
            AND p.product_id = '" . $product_id ."'
        ")->row;

        if(empty($color)) {
            return [
                "status"=> false,
                "message" => "Renk bilgisi yok."
            ];
        }

        $collection = $this->db->query("SELECT ad.`name`, a.infinityeditid, a.attribute_id 
            FROM ps_product AS p 
            INNER JOIN ps_product_attribute AS pa ON p.product_id = pa.product_id
            INNER JOIN ps_attribute AS a ON pa.attribute_id = a.attribute_id
            INNER JOIN ps_attribute_description AS ad ON a.attribute_id = ad.attribute_id
            INNER JOIN ps_attribute_group AS ag ON a.attribute_group_id = ag.attribute_group_id 
            WHERE a.attribute_group_id = 11
            AND p.product_id = '" . $product_id ."'
        ")->row;

        if(empty($collection)) {
            return [
                "status"=> false,
                "message" => "Koleksiyon bilgisi yok."
            ];
        }

        # Saat kordonlarında barkoda bedeni de ekler
        if($category['category_id'] == 4762 || $category['category_id'] == 4894) {
            $barcode_category = !empty($category['infinityeditid']) ? $category['infinityeditid'] : $category['category_id'];
            $barcode_collection = !empty($collection['infinityeditid']) ? $collection['infinityeditid'] : $collection['attribute_id'];
            $barcode_color = !empty($color['infinityeditid']) ? $color['infinityeditid'] : $color['attribute_id'];
            $barcode = $barcode_category . $barcode_collection . $barcode_color . $option_value_id;
        } else {
            $barcode_category = !empty($category['infinityeditid']) ? $category['infinityeditid'] : $category['category_id'];
            $barcode_collection = !empty($collection['infinityeditid']) ? $collection['infinityeditid'] : $collection['attribute_id'];
            $barcode_color = !empty($color['infinityeditid']) ? $color['infinityeditid'] : $color['attribute_id'];
            $barcode = $barcode_category . $barcode_collection . $barcode_color;
        }      

        $code = !empty($option_value_id) ? $product_info['product_id'] . $option_value_id : $product_info['product_id'];
        //$product_material = $this->model_catalog_product->getProductMaterial($barcode);

        // Ürün aktarıldıysa cevap gönder işlemi bitir.
        if (!empty($product_info['logo_reference'])) {
            $response = [
                'status' => 'failed',
                'message' => "Ürün zaten aktarılmış"
            ];

            $this->response->addHeader('Content-Type: application/json');
            $this->response->setOutput(json_encode($response));
            return;
        }

        $name = $product_info['name'];
       
        if(!empty($option_value_id)) {
            $option_value_name = $this->db->query("SELECT name FROM ps_option_value_description WHERE option_value_id = " . $option_value_id)->row['name'];
            $name = $product_info['name'] . ' ' . $option_value_name;
        }

        $name_part1 = $name;
        $name_part2 = "";

        if(strlen($name)>50) {
            // İsmi böler. (Logo'da devamının görünmesi için)
            $name_part1 = mb_substr($name, 0, 49, 'UTF-8');
            $name_part2 = mb_substr($name, 49, null, 'UTF-8');            
        }

        $product_barcode = !empty($this->request->post['option_value_id']) ? $product_info['product_id'] . $option_value_id : $product_info['product_id'];
        $product_barcode = str_pad($product_barcode, 6,"0",STR_PAD_LEFT);

        // Logo için dizi hazırla.
        $material_data = [
            "INTERNAL_REFERENCE" => "0",
            "CARD_TYPE" => "11",
            "CODE" => $code,
            "NAME" => $name_part1,
            "NAME3" => $name_part2,
            "GROUP_CODE" => $collection['name'],
            "USEF_PURCHASING" => 1,
            "USEF_MM" => 1,
            "USEF_SALES" => 1,
            "VAT" => "18.0",
            "AUTOINCSL" => "1",
            "LOTS_DIVISIBLE" => "1",
            "UNITSET_CODE" => "05",
            "SELVAT" => "18.0",
            "RETURNVAT" => "18.0",
            "SELPRVAT" => "18.0",
            "RETURNPRVAT" => "18.0", 
            "EXT_ACC_FLAGS" => "2",
            "AUXIL_CODE2" => $barcode,
            "AUXIL_CODE3" => $product_info['model'] ? $product_info['model'] : "",
            "UNITS" => [
                "items" => [
                   [
                      "UNIT_CODE" => "ADET", 
                      "USEF_MTRLCLASS" => "1", 
                      "USEF_PURCHCLAS" => "1", 
                      "USEF_SALESCLAS" => "1", 
                      "CONV_FACT1" => "1", 
                      "CONV_FACT2" => "1", 
                      "EXT_ACC_FLAGS" => "2", 
                      "DATA_REFERENCE" => "0", 
                      "INTERNAL_REFERENCE" => "0", 
                      "BARCODE_LIST" => [
                         "items" => [
                            [
                               "INTERNAL_REFERENCE" => "0", 
                               "BARCODE" => $product_barcode, 
                               "XML_ATTRIBUTE" => "1", 
                               "DATA_REFERENCE" => "0", 
                            ] 
                         ] 
                      ] 
                   ] 
                ] 
             ] 
        ];

        // Logo servera gönder.
        $logo = new Logo;
        $post_material = $logo->postLogo('/api/v1/items', json_encode($material_data)); 

        $response = [];
        // Cevabın içerisinde referans ve kod geldiyse kayıt başarılıdır veritabanına yaz.
        if(!empty($post_material['INTERNAL_REFERENCE']) && !empty($post_material['CODE'])) {

            if(!empty($option_value_id)) {
                $this->db->query("UPDATE ps_product_option_value SET logo_reference = '" . $post_material['INTERNAL_REFERENCE'] . "', logo_code = '" . $post_material['CODE'] ."' WHERE product_id = '" . (int) $product_id . "' AND option_value_id ='" . (int) $option_value_id . "' LIMIT 1");
            } else {
                $this->db->query("UPDATE ps_product SET logo_reference = '" . $post_material['INTERNAL_REFERENCE'] . "', logo_code = '" . $post_material['CODE'] ."' WHERE product_id = '" . (int) $product_id . "' ");
            }

            $response = [
                'status' => 'success',
                'message' => "Ürün başarıyla aktarıldı."
            ];
        } else {
            // Hatayı yakala geri döndür.
            $error_keys = ['LoginError', 'DBError', 'ServerError', 'ValError0', 'OtherError'];
            $error = 0;
            foreach($error_keys as $key) {
                if(!empty($post_material['ModelState'][$key])) {
                    $error = $post_material['ModelState'][$key];
                }
            }

            if(!empty($error)) {
                if(!empty($option_value_id)) {
                    $this->db->query("UPDATE ps_product_option_value SET logo_message = '" . json_encode($error) . "' WHERE product_id = '" . (int) $product_id . "' AND option_value_id ='" . (int) $option_value_id . "' LIMIT 1");
                } else {
                    $this->db->query("UPDATE ps_product SET logo_message = '" . json_encode($error). "' WHERE product_id = '" . (int) $product_id . "' ");
                }
                $response = [
                    'status' => 'failed',
                    'message' => "Ürün aktarılamadı. Materyal eklenirken hata oluştu: " . json_encode($error)
                ];
            }
        }
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($response));
    }


}

