<?php
/**
 * @package    Proxim
 * @author     Davison Pro <davisonpro.coder@gmail.com | https://davisonpro.dev>
 * @copyright  2019 Proxim
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'prox-bootstrap.php';

$smarty = $app->container->smarty;

$app->registerStylesheet('wordpress-css', '/static/wp/header.css', ['media' => 'all']);
$app->registerJavascript('wordpress-js', '/static/wp/header.js', ['position' => 'bottom', 'media' => 'all']);

use Proxim\Tools;
use Proxim\Util\ArrayUtils;

$formValues = Tools::getAllValues();

$orderformValues = array();

foreach( $formValues as $key => $formValue ) {
    if(
        $key == "topcatId" ||
        $key == "pages" || 
        $key == "slides" ||
        $key == "charts"
    ) {
        $orderformValues[ $key ] = (int) $formValue;
    } elseif( $key == "deadlineId" ) {
        $orderformValues["tariffId"] = (int) $formValue;
    } elseif( $key == "academicLevelId" ) {
        $orderformValues["academicLevel"] = (int) $formValue;
    } elseif( $key == "paperTypeId" ) {
        $orderformValues["paperType"] = (int) $formValue;
    } else {
        $orderformValues[ $key ] = pSQL($formValue);
    }
}

$formCurrency = $app->orderFormCurrency();

if ( ArrayUtils::has($formValues, 'currencyName') ) {
    $orderformValues["currencyName"] = ArrayUtils::get($formValues, 'currencyName');
    $formCurrency['orderformCurrency'] = ArrayUtils::get($formValues, 'currencyName');
}

$defaultCountry = array();
if ( $app->container->cookie->country_code ) {
    $countries = $globals['countries'];
    $country_code = Tools::strtolower($app->container->cookie->country_code);

    if ( ArrayUtils::has($countries, $country_code) ) {
        $country = ArrayUtils::get($countries, $country_code);
        $defaultCountry = array(
            "id" => $country_code,
            "name" => ArrayUtils::get($country, 'name'),
            "code" => ArrayUtils::get($country, 'code'),
            "phoneCode" => ArrayUtils::get($country, 'phoneCode')
        );
    }
}

$templateVars = array(
    'page' => [
        'title' => 'Place an Order',
        'description' => 'Place an Order'
    ],
    'app' => 'standalone-form',
    'orderformPurpose' => 'newOrder',
    'target' => 'order',
    'orderformValues'=> !empty($orderformValues) ? $orderformValues : null,
);

$templateVars = array_merge($formCurrency, $templateVars, $defaultCountry);

$app->setVars($templateVars);

$app->setTemplate('order');
$app->display();
