<?php
include(DIR_SYSTEM . '/library/SimpleXLSX.php');

class ControllerExtensionModuleHbRapor extends Controller {
    private $error = array();

    public function index() {
        $this->load->language('extension/module/hb_rapor');
        $this->document->setTitle($this->language->get('heading_title'));

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

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            // Excel dosyasını işle
            if (!empty($this->request->files['excel_file']['name']) && is_file($this->request->files['excel_file']['tmp_name'])) {
                $file = $this->request->files['excel_file']['tmp_name'];


                $total_updated=0;

                if ( $xlsx = @SimpleXLSX::parse($file) ) {



                    foreach ($xlsx->rows() as $row) {

                        $hb_sku=$row[0];
                        $total_view=$row[1];
                        $total_customer_view=$row[2];
                        $total_add_list=$row[3];
                        $total_sale=$row[4];

                        $query =  $this->db->query("update market_product set total_view='".$total_view."', total_customer_view='".$total_customer_view."', total_add_list='".$total_add_list."', total_sale='".$total_sale."' where marketplace_product_id='".$hb_sku."'");

                        if($this->db->countAffected()){
                            $total_updated++;
                        }

                    }
                }
            }


            $this->session->data['success'] = $this->language->get('text_success') .' Toplam '.$total_updated.' adet ürün güncellendi!';
        }

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

        $data['action'] = $this->url->link('extension/module/hb_rapor', 'user_token=' . $this->session->data['user_token'], true);
        $data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'], true);

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

        $data['breadcrumbs'] = array(
            array(
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
            ),
            array(
                'text' => $this->language->get('text_extension'),
                'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'], true)
            ),
            array(
                'text' => $this->language->get('heading_title'),
                'href' => $this->url->link('extension/module/hb_rapor', 'user_token=' . $this->session->data['user_token'], true)
            )
        );

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

        $this->response->setOutput($this->load->view('extension/module/hb_rapor', $data));
    }

    protected function validate() {
        /*  if (!$this->user->hasPermission('modify', 'extension/module/hb_rapor')) {
              $this->error['warning'] = $this->language->get('error_permission');
          }*/

        return !$this->error;
    }
}
