<?php
class ControllerCustomerCustomerApproval extends Controller {
    public function index(): void {
        $this->load->language('customer/customer_approval');

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

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

        $this->getList();
    }

    protected function getList() {
        $this->load->language('customer/customer_approval');

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

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

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

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

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

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

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

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

            unset ($this->session->data['success']);
        } else {
            $data['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_email'])) {
            $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
        }

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

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

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

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

        $data['breadcrumbs'] = [];

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

        $data['breadcrumbs'][] = [
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('customer/customer_approval', 'user_token=' . $this->session->data['user_token'] . $url, true)
        ];

        $data['customer_approvals'] = [];

        $filter_data = [
            'filter_name'              => $filter_name,
            'filter_email'             => $filter_email,
            'filter_customer_group_id' => $filter_customer_group_id,
            'filter_type'              => $filter_type,
            'filter_date_added'        => $filter_date_added,
            'start'                    => ($page - 1) * $this->config->get('config_limit_admin'),
            'limit'                    => $this->config->get('config_limit_admin')
        ];

        // Customer Approvals
        $this->load->model('customer/customer_approval');

        $customer_approval_total = $this->model_customer_customer_approval->getTotalCustomerApprovals($filter_data);

        $results = $this->model_customer_customer_approval->getCustomerApprovals($filter_data);

        foreach ($results as $result) {
            $data['customer_approvals'][] = [
                'customer_id'    => $result['customer_id'],
                'name'           => $result['name'],
                'email'          => $result['email'],
                'customer_group' => $result['customer_group'],
                'type'           => $this->language->get('text_' . $result['type']),
                'date_added'     => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
                'approve'        => $this->url->link('customer/customer_approval/approve', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $result['customer_id'] . '&type=' . $result['type'], true),
                'deny'           => $this->url->link('customer/customer_approval/deny', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $result['customer_id'] . '&type=' . $result['type'], true),
                'edit'           => $this->url->link('customer/customer/edit', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $result['customer_id'], 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_email'])) {
            $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
        }

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

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

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

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

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

        $pagination = new \Pagination();
        $pagination->total = $customer_approval_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_limit_admin');
        $pagination->url = $this->url->link('customer/customer_approval/customer_approval', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);

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

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

        $data['filter_name'] = $filter_name;
        $data['filter_email'] = $filter_email;
        $data['filter_customer_group_id'] = $filter_customer_group_id;
        $data['filter_type'] = $filter_type;
        $data['filter_date_added'] = $filter_date_added;

        $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('customer/customer_approval', $data));
    }

    public function approve(): void {
        $this->load->language('customer/customer_approval');

        $json = [];

        if (!$this->user->hasPermission('modify', 'customer/customer_approval')) {
            $json['error'] = $this->language->get('error_permission');
        } else {
            // Customer Approvals
            $this->load->model('customer/customer_approval');

            if ($this->request->post['type'] == 'customer') {
                $this->model_customer_customer_approval->approveCustomer($this->request->post['customer_id']);
            } elseif ($this->request->post['type'] == 'affiliate') {
                $this->model_customer_customer_approval->approveAffiliate($this->request->post['customer_id']);
            }

            $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_email'])) {
                $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
            }

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

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

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

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

            $json['redirect'] = str_replace('&amp;', '&', $this->url->link('customer/customer_approval', 'user_token=' . $this->session->data['user_token'] . $url, true));
        }

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

    public function deny(): void {
        $this->load->language('customer/customer_approval');

        $json = [];

        if (!$this->user->hasPermission('modify', 'customer/customer_approval')) {
            $json['error'] = $this->language->get('error_permission');
        } else {
            // Customer Approvals
            $this->load->model('customer/customer_approval');

            if ($this->request->post['type'] == 'customer') {
                $this->model_customer_customer_approval->denyCustomer($this->request->post['customer_id']);
            } elseif ($this->request->post['type'] == 'affiliate') {
                $this->model_customer_customer_approval->denyAffiliate($this->request->post['customer_id']);
            }

            $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_email'])) {
                $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
            }

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

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

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

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

            $json['redirect'] = str_replace('&amp;', '&', $this->url->link('customer/customer_approval', 'user_token=' . $this->session->data['user_token'] . $url, true));
        }

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