<?php

namespace Oro\Bundle\SalesBundle\Tests\Functional\Controller;

use Oro\Bundle\SalesBundle\Tests\Functional\Fixture\LoadClosedOpportunityFixtures;
use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;

class OpportunityReopenTest extends WebTestCase
{
    #[\Override]
    protected function setUp(): void
    {
        $this->initClient(
            ['debug' => false],
            $this->generateBasicAuthHeader()
        );
        $this->client->useHashNavigation(true);
        $this->loadFixtures([LoadClosedOpportunityFixtures::class]);
    }

    public function testReopenOpportunity()
    {
        $opportunity = $this->getReference('lost_opportunity');

        $crawler = $this->client->request(
            'GET',
            $this->getUrl('oro_sales_opportunity_update', ['id' => $opportunity->getId()])
        );

        $form = $crawler->selectButton('Save and Close')->form();
        $form['oro_sales_opportunity_form[status]']->setValue('opportunity_status.in_progress');
        $form['oro_sales_opportunity_form[budgetAmount][value]']->setValue('10');

        $this->client->followRedirects(true);
        $this->client->submit($form);

        $result = $this->client->getResponse();
        $this->assertHtmlResponseStatusCodeEquals($result, 200);

        $crawler = $this->client->request(
            'GET',
            $this->getUrl('oro_sales_opportunity_update', ['id' => $opportunity->getId()])
        );

        $form = $crawler->selectButton('Save and Close')->form();

        $budgetAmount = $form['oro_sales_opportunity_form[budgetAmount][value]'];
        $status = $form['oro_sales_opportunity_form[status]'];

        $this->assertEquals(
            10,
            round($budgetAmount->getValue()),
            'Budget amount incorrect. Please check opportunity update from close status to open'
        );
        $this->assertEquals(
            'opportunity_status.in_progress',
            $status->getValue(),
            'Opportunity status incorrect. Please check opportunity update from close status to open'
        );
    }
}
