<?php
/**
 * 2007-2023 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 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/afl-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@prestashop.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    PrestaShop SA <contact@prestashop.com>
 * @copyright 2007-2023 PrestaShop SA
 * @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */

use PrestaShop\Module\AgileMultisellerPro\Controller\Front\AgileModuleFrontController;
use PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoProduct;

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

class AgileMultiSellerProAjaxMakeofferModuleFrontController extends AgileModuleFrontController
{
    public $ajax = true;

    public function displayAjaxSendTheOffer()
    {
        $status = false;
        $message = $this->trans('Your e-mail could not be sent. Please check the e-mail address and try again.', [], 'Modules.Agilemultisellerpro.Makeoffer');

        if (
            Tools::getValue('action') == 'sendTheOffer'
            && Tools::getValue('secure_key') == Tools::encrypt('agilemultisellerpro_makeoffer')
        ) {
            $offer_infos = json_decode(Tools::getValue('offer'));
            $yourName = '';
            $yourMail = '';
            $yourOffer = '';
            $id_product = null;

            foreach ($offer_infos as $entry) {
                if ($entry->key == 'your_name') {
                    $yourName = $entry->value;
                } elseif ($entry->key == 'your_email') {
                    $yourMail = $entry->value;
                } elseif ($entry->key == 'your_offer') {
                    $yourOffer = $entry->value;
                } elseif ($entry->key == 'id_product') {
                    $id_product = (int) $entry->value;
                }
            }

            if ($yourName && $yourMail && $yourOffer && $id_product) {
                $product = new Product((int) $id_product, false, $this->context->language->id);

                $sellerInfoProduct = $this->entityManager->getRepository(SellerinfoProduct::class)->findOneBy(['idProduct' => $id_product]);

                $customer = null;

                if ($sellerInfoProduct) {
                    $sellerInfo = $sellerInfoProduct->getSellerinfo();
                    $customer = new Customer($sellerInfo->getIdCustomer());
                }

                $shop_email = Configuration::get('PS_SHOP_EMAIL');

                if (Validate::isLoadedObject($customer)) {
                    $to = $customer->email;
                    $toname = $customer->firstname . ' ' . $customer->lastname;
                    $bcc = $shop_email;
                } else {
                    $to = $shop_email;
                    $toname = $this->trans('Store Admin', [], 'Modules.Agilemultisellerpro.Makeoffer');
                    $bcc = null;
                }

                $templateVars = [
                    '{product}' => $product->name,
                    '{product_link}' => $this->context->link->getProductLink($product),
                    '{customer}' => $yourName,
                    '{name}' => $toname,
                    '{email}' => $yourMail,
                    '{offer_message}' => $yourOffer,
                ];

                if (
                    Mail::Send(
                        (int) $this->context->language->id,
                        'make_offer',
                        sprintf(Mail::l('%1$s sent you an price offer about product %2$s', (int) $this->context->language->id), $yourName, $product->name),
                        $templateVars,
                        $to,
                        $toname,
                        $shop_email,
                        $yourName,
                        null,
                        null,
                        _PS_ROOT_DIR_ . '/modules/agilemultisellerpro/mails/',
                        false,
                        null,
                        $bcc
                    )
                ) {
                    $status = true;
                    $message = $this->trans('Your e-mail has been sent successfully.', [], 'Admin.Advparameters.Notification');
                }
            }
        }

        $this->ajaxRender(json_encode([
            'status' => $status,
            'message' => $message,
        ]));
    }
}
