<?php

namespace KupShop\B2BBundle\Controller;

use KupShop\B2BBundle\View\B2BContactForm;
use KupShop\KupShopBundle\Routing\TranslatedRoute;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class B2BController extends Controller
{
    /**
     * @TranslatedRoute("/#b2b-registration#/", notModule="components")
     */
    public function b2bRegistrationAction(Request $request, B2BContactForm $B2BContactForm)
    {
        if (findModule(\Modules::B2B, \Modules::SUB_OLD)) {
            return $this->forward('KupShop\ContentBundle\Controller\ContactFormController::SendFormAction', ['type' => 'registration-b2b']);
        }

        if ($request->isMethod('POST')) {
            $B2BContactForm->setRequest($request);

            $B2BContactForm->setFormData(array_merge($request->query->all(), $request->request->all()));

            if ($B2BContactForm->submitForm()) {
                return new RedirectResponse(
                    path('kupshop_content_contactform_sendform_sent', ['type' => 'registration-b2b-default'])
                );
            }
        }

        return $B2BContactForm->getResponse();
    }
}
