<?php
use app\components\UrlHelper;
use app\models\db\Amendment;
use yii\helpers\Html;

/**
 * @var \yii\web\View $this
 * @var Amendment $amendment
 * @var \app\models\db\Consultation $consultation
 * @var string[][] $paragraphSections
 */

/** @var \app\controllers\Base $controller */
$controller = $this->context;
$layout     = $controller->layoutParams;
$layout->loadFuelux();
$layout->loadCKEditor();
$layout->addAMDModule('frontend/MergeSingleAmendment');
$layout->addCSS('css/formwizard.css');

$motion    = $amendment->getMyMotion();
$motionUrl = UrlHelper::createMotionUrl($motion);
$layout->addBreadcrumb($motion->getBreadcrumbTitle(), $motionUrl);
if (!$consultation->getSettings()->hideTitlePrefix && $amendment->titlePrefix != '') {
    $layout->addBreadcrumb($amendment->titlePrefix, UrlHelper::createAmendmentUrl($amendment));
} else {
    $layout->addBreadcrumb(\Yii::t('amend', 'amendment'), UrlHelper::createAmendmentUrl($amendment));
}
$layout->addBreadcrumb(\Yii::t('amend', 'merge1_title'));

$this->title = $amendment->getTitle() . ': ' . \Yii::t('amend', 'merge1_title');


$fixedWidthSections = [];
foreach ($amendment->getActiveSections() as $section) {
    if ($section->getSettings()->fixedWidth) {
        $fixedWidthSections[] = $section->sectionId;
    }
}

echo '<h1>' . Html::encode($this->title) . '</h1>';

echo Html::beginForm('', 'post', ['id' => 'amendmentMergeForm', 'class' => 'fuelux']);

?>
    <div class="content">
        <div class="alert alert-info"><?= \Yii::t('amend', 'merge1_intro_user') ?></div>
        <div class="form-group">
            <label for="motionTitlePrefix"><?= \Yii::t('amend', 'merge1_motion_prefix') ?></label>
            <input type="text" class="form-control" id="motionTitlePrefix" name="motionTitlePrefix"
                   value="<?= Html::encode($amendment->getMyMotion()->getNewTitlePrefix()) ?>">
        </div>

        <fieldset class="affectedParagraphs">
            <?php
            foreach ($paragraphSections as $sectionId => $paragraphs) {
                $fixedClass = (in_array($sectionId, $fixedWidthSections) ? 'fixedWidthFont' : '');

                foreach ($paragraphs as $paragraphNo => $parDat) {
                    $nameBase = 'newParas[' . $sectionId . '][' . $paragraphNo . ']';
                    ?>
                    <section class="paragraph paragraph_<?= $sectionId ?>_<?= $paragraphNo ?> unmodified"
                             data-section-id="<?= $sectionId ?>" data-paragraph-no="<?= $paragraphNo ?>">
                        <h2 class="green">
                            <?php
                            if ($parDat['lineFrom'] == $parDat['lineTo']) {
                                $tpl = \Yii::t('amend', 'merge1_changein_1');
                            } else {
                                $tpl = \Yii::t('amend', 'merge1_changein_x');
                            }
                            echo str_replace(
                                ['%LINEFROM%', '%LINETO%'],
                                [$parDat['lineFrom'], $parDat['lineTo']],
                                $tpl
                            );
                            ?>:
                        </h2>
                        <div class="content">
                            <div class="unmodifiedVersion motionTextHolder">
                                <div class="paragraph">
                                    <div class="text <?= $fixedClass ?>"><?= $parDat['diff'] ?></div>
                                </div>
                            </div>
                        </div>
                    </section>
                    <?php
                }
            }
            ?>
        </fieldset>

        <div class="saveholder content">
            <button type="submit" name="save" class="btn btn-primary save"><?= \Yii::t('admin', 'save') ?></button>
        </div>
    </div>
<?php

echo Html::endForm();
