<?php
/**
 * NOTICE OF LICENSE.
 *
 * Copyright 2022 Kosmonaft.dev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *  @author    Alexandre DEBUSSCHÈRE <alexandre@kosmonaft.dev>
 *  @copyright 2022 - 2023 Kosmonaft.dev
 *  @license   MIT
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

require_once _PS_MODULE_DIR_.'mrmax/vendor/autoload.php';

/**
 * Class AdminTrackingMRMaxController
 */
class AdminTrackingMRMaxController extends MRMaxAdminController
{

    public function renderForm()
    {
        if (!(int)Tools::getValue('submitFiltermrmax')) {
            $this->processResetFilters();
        }

        if (Tools::isSubmit('viewmrmax')) {
            $this->redirectToOrder();
        }

        $this->token = Tools::getAdminTokenLite('AdminTrackingMRMax');
        $this->current_index = AdminController::$currentIndex.'&configure='.$this->module->name;

        $form = '';
        $form .= $this->getOrdersView();

        return $form;
    }

    public function getOrdersView()
    {
        $fields_list = [
            'id_order' => [
                'title' => 'ID',
                'align' => 'center',
                'class' => 'fixed-width-xs',
                'remove_onclick' => true
            ],
            'reference' => [
                'title' => $this->l('Reference'),
                'remove_onclick' => true
            ],
            'customer' => [
                'title' => $this->l('Customer'),
                'remove_onclick' => true
            ],
            'status' => [
                'title' => $this->l('Status'),
                'remove_onclick' => true
            ],
            'date' => [
                'title' => $this->l('Date'),
                'remove_onclick' => true
            ]
        ];

        $content = array();
        foreach (MRMaxTracking::getPaginatedTrackings(0, 50) as $timeline) {
            $order = new Order((int)$timeline['id_order']);
            if (!Validate::isLoadedObject($order)) {
                continue;
            }

            $content[] = [
                'id_order' => $order->id,
                'reference' => $order->reference,
                'customer' => MRMaxCustomer::getNames($order->id_customer),
                'status' => $timeline['label_short'],
                'date' => $timeline['date']
            ];
        }

        $helper = new HelperList();
        $helper->imageType = 'jpg';
        $helper->shopLinkType = '';
        $helper->simple_header = true;
        $helper->actions = ['view'];
        $helper->show_toolbar = false;
        $helper->module = $this->module;
        $helper->listTotal = count($content);
        $helper->identifier = 'id_order';
        $helper->title = $this->l('Orders');
        $helper->table = $this->module->name;
        $helper->token = $this->token;
        $helper->currentIndex = $this->current_index;

        return $helper->generateList($content, $fields_list);
    }
}
