<?php
/**
* 2007-2020 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@buy-addons.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    Buy-Addons <contact@buy-addons.com>
*  @copyright 2007-2020 PrestaShop SA
*  @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
* @since 1.6
*/

class AskforaquotemodulpopupaddressModuleFrontController extends ModuleFrontController
{

    public function run()
    {
        parent::init();
        parent::initHeader();
        parent::initContent();
        $quote_module_token = Tools::getValue('quote_token');
        if ($quote_module_token != md5(sha1(_COOKIE_KEY_.'askforaquotemodul'))) {
            die;
        }
        $check_add_or_edit = Tools::getValue('check_add_or_edit');
        $id_delivery = Tools::getValue('id_delivery');
        $id_invoice = Tools::getValue('id_invoice');
        $firstname = Tools::getValue('firstname');
        $lastname = Tools::getValue('lastname');
        
        $name_country = array();
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $id_lang = $this->context->language->id;
        $select_country = 'Select * from '._DB_PREFIX_.'country_lang cl';
        $select_country .= ' INNER JOIN '._DB_PREFIX_.'country c ON cl.id_country=c.id_country ';
        $select_country .= 'where cl.id_lang = "'.$id_lang.'" AND c.active=1';
        $name_country = $db->ExecuteS($select_country, true, false);
        $data_address = array();
        if (!empty($id_delivery) && $check_add_or_edit == 'delivery') {
            $sql_delivery = 'SELECT * FROM '._DB_PREFIX_.'address WHERE id_address='. (int) $id_delivery;
            $data_address = $db->ExecuteS($sql_delivery);
        }
        if (!empty($id_invoice) && $check_add_or_edit == 'invoice') {
            $sql_invoice = 'SELECT * FROM '._DB_PREFIX_.'address WHERE id_address='. (int) $id_invoice;
            $data_address = $db->ExecuteS($sql_invoice);
        }
        
        $data_address['alias'] = @$data_address[0]['alias'];
        if ($check_add_or_edit == 'addnew') {
            $data_address['firstname'] = @$firstname;
            $data_address['lastname'] = @$lastname;
        } else {
            $data_address['firstname'] = @$data_address[0]['firstname'];
            $data_address['lastname'] = @$data_address[0]['lastname'];
        }
        $data_address['company'] = @$data_address[0]['company'];
        $data_address['id_country'] = @$data_address[0]['id_country'];
        $data_address['city'] = @$data_address[0]['city'];
        $data_address['id_state'] = @$data_address[0]['id_state'];
        $data_address['phone'] = @$data_address[0]['phone'];
        $data_address['dni'] = @$data_address[0]['dni'];
        $data_address['vat_number'] = @$data_address[0]['vat_number'];
        $data_address['address1'] = @$data_address[0]['address1'];
        $data_address['address2'] = @$data_address[0]['address2'];
        $data_address['postcode'] = @$data_address[0]['postcode'];
        $data_address['phone_mobile'] = @$data_address[0]['phone_mobile'];
        $data_address['other'] = @$data_address[0]['other'];
        
        $name_state = array();
        if (!empty($data_address['id_country'])) {
            $id_country = $data_address['id_country'];
            $select_state = 'Select * from '._DB_PREFIX_.'state';
            $select_state .= ' where id_country = "'.$id_country.'" and active="1"';
            $name_state = $db->ExecuteS($select_state, true, false);
        }
        
        $this->context->smarty->assign("name_country", $name_country);
        $this->context->smarty->assign("name_state", $name_state);
        $this->context->smarty->assign("data_address", $data_address);
        $this->context->smarty->assign("id_delivery", $id_delivery);
        $this->context->smarty->assign("id_invoice", $id_invoice);
        $this->context->smarty->assign("check_add_or_edit", $check_add_or_edit);
        
        $ba_dir = 'askforaquotemodul/views/templates/admin/popupaddress.tpl';
        $html = $this->context->smarty->fetch(_PS_MODULE_DIR_ . $ba_dir);
        echo $html;
    }
}
