<?php
/*
 *   $Id$
 *
 *   AbanteCart, Ideal OpenSource Ecommerce Solution
 *   http://www.AbanteCart.com
 *
 *   Copyright © 2011-2025 Belavier Commerce LLC
 *
 *   This source file is subject to Open Software License (OSL 3.0)
 *   License details are bundled with this package in the file LICENSE.txt.
 *   It is also available at this URL:
 *   <http://www.opensource.org/licenses/OSL-3.0>
 *
 *  UPGRADE NOTE:
 *    Do not edit or add to this file if you wish to upgrade AbanteCart to newer
 *    versions in the future. If you wish to customize AbanteCart for your
 *    needs, please refer to http://www.AbanteCart.com for more information.
 */

class ControllerPagesTotalAvataxIntegrationTotal extends AController
{
    private $error = [];
    private $fields = [
        'avatax_integration_total_status',
        'avatax_integration_total_sort_order',
        'avatax_integration_total_calculation_order',
        'avatax_integration_total_total_type',
    ];

    public function main()
    {
        $this->loadModel('setting/setting');
        $this->loadLanguage('extension/total');
        $this->loadLanguage('extension/total');
        $this->loadLanguage('avatax_integration/avatax_integration');

        if ($this->request->is_POST() && $this->validate()) {
            $this->model_setting_setting->editSetting('avatax_integration_total', $this->request->post);
            $this->session->data['success'] = $this->language->get('text_success');
            redirect($this->html->getSecureURL('total/avatax_integration_total'));
        }

        $this->document->setTitle($this->language->get('total_name'));
        $this->data['error_warning'] = $this->error['warning'] ?? '';

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

        $this->document->initBreadcrumb(
            [
                'href'      => $this->html->getSecureURL('index/home'),
                'text'      => $this->language->get('text_home'),
                'separator' => false,
            ]
        );
        $this->document->addBreadcrumb(
            [
                'href'      => $this->html->getSecureURL('total/avatax_integration_total'),
                'text'      => $this->language->get('total_name'),
                'separator' => ' :: ',
                'current'   => true
            ]
        );

        foreach ($this->fields as $f) {
            $this->data [$f] = $this->request->post [$f] ?? $this->config->get($f);
        }

        $this->data['action'] = $this->html->getSecureURL('total/avatax_integration_total');
        $this->data['cancel'] = $this->html->getSecureURL('extension/total');
        $this->data['heading_title'] = $this->language->get('text_edit') . ' ' . $this->language->get('total_name');
        $this->data['form_title'] = $this->language->get('total_name');
        $this->data['update'] = $this->html->getSecureURL(
            'listing_grid/total/update_field',
            '&id=avatax_integration_total'
        );

        $form = new AForm ('HS');
        $form->setForm(
            [
                'form_name' => 'editFrm',
                'update'    => $this->data ['update']
            ]
        );

        $this->data['form']['form_open'] = $form->getFieldHtml(
            [
                'type'   => 'form',
                'name'   => 'editFrm',
                'action' => $this->data ['action'],
                'attr'   => 'data-confirm-exit="true" class="aform form-horizontal"'
            ]
        );

        $this->data['form']['submit'] = $form->getFieldHtml(
            [
                'type'  => 'button',
                'name'  => 'submit',
                'text'  => $this->language->get('button_save'),
                'style' => 'button1',
            ]
        );

        $this->data['form']['cancel'] = $form->getFieldHtml(
            [
                'type'  => 'button',
                'name'  => 'cancel',
                'text'  => $this->language->get('button_cancel'),
                'style' => 'button2',
            ]
        );

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

        $this->data['form']['fields']['status'] = $form->getFieldHtml(
            [
                'type'  => 'checkbox',
                'name'  => 'avatax_integration_total_status',
                'value' => $this->data['avatax_integration_total_status'],
                'style' => 'btn_switch',
            ]
        );

        $this->data['form']['fields']['total_type'] = $form->getFieldHtml(
            [
                'type'  => 'hidden',
                'name'  => 'avatax_integration_total_total_type',
                'value' => 'avatax_integration',
            ]
        );

        $this->data['entry_sort_order'] = $this->language->get('column_sort_order');
        $this->data['form']['fields']['sort_order'] = $form->getFieldHtml(
            [
                'type'  => 'input',
                'name'  => 'avatax_integration_total_sort_order',
                'value' => (int)$this->data['avatax_integration_total_sort_order'],
            ]
        );

        $this->data['entry_calculation_order'] = $this->language->get('column_calculation_order');
        $this->data['form']['fields']['calculation_order'] = $form->getFieldHtml(
            [
                'type'  => 'input',
                'name'  => 'avatax_integration_total_calculation_order',
                'value' => (int)$this->data['avatax_integration_total_calculation_order'],
            ]
        );

        $this->view->assign('help_url', $this->gen_help_url('edit_avatax_integration_total'));
        $this->view->batchAssign($this->data);
        $this->processTemplate('pages/total/form.tpl');
    }

    protected function validate()
    {
        if (!$this->user->canModify('total/avatax_integration_total')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }
        $this->extensions->hk_ValidateData($this);
        return (!$this->error);
    }
}