<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// TODO: Temporary implementation. UI components rework needed.
// TODO: Form validation when no source is set

// @codingStandardsIgnoreFile

/** @var Magento\Backend\Block\Template $block */
/** @var \Magento\InventoryCatalogAdminUi\ViewModel\SourcesSelection $viewModel */
$viewModel = $block->getViewModel();

if ($block->getData('show_only_assigned_sources')) {
    $sources = $viewModel->getAssignedSources();
} else {
    $sources = $viewModel->getSources();
}
?>
<div class="page-main-actions">
    <div class="page-actions">
        <div class="page-actions-buttons">
            <div class="page-actions" data-ui-id="page-actions-toolbar-content-header">
                <div class="page-actions-inner" data-title="Import">
                    <div class="page-actions-buttons">
                        <button
                                id="back"
                                title="Back"
                                type="button"
                                class="action-scalable back">
                            <span><?= /* @noEscape */ __('Back') ?></span>
                        </button>

                        <button id="bulk-operation"
                                title="<?= $block->escapeHtmlAttr(__($block->getData('button_title'))) ?>"
                                type="button"
                                class="action-default scalable save primary">
                            <span><?= $block->escapeHtml(__($block->getData('button_title'))) ?></span>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="entry-edit form-inline">
    <form
            action="<?= /* @noEscape */ $block->getUrl($block->getData('submit_url')) ?>"
            id="bulk-operation-form"
            method="post"
            enctype="multipart/form-data">
        <div>
            <input name="form_key" type="hidden" value="<?= /* @noEscape */ $block->getFormKey() ?>">
        </div>

        <?= $block->getChildHtml('notice') ?>

        <fieldset class="fieldset admin__fieldset" id="base_fieldset">
            <legend class="admin__legend legend">
                <span><?= /* @noEscape */ __('Bulk operation') ?></span>
            </legend>

            <div class="admin__field field field-entity">
                <div class="admin__field-control control">
                    <div class="admin__field">
                        <?= /* @noEscape */ __('You selected <strong>%count</strong> products for this bulk operation.', [
                                'count' => $viewModel->getProductsCount()
                        ]) ?><br />
                    </div>
                </div>
            </div>
        </fieldset>

        <fieldset class="fieldset admin__fieldset" id="base_fieldset">
            <legend class="admin__legend legend">
                <span><?= /* @noEscape */ __('Select one or more sources') ?></span>
            </legend>

            <div class="admin__field field field-entity">
                <div class="admin__field-control control">
                    <div class="admin__field">
                        <?php foreach ($sources as $source) : ?>
                            <?php /** @var \Magento\InventoryApi\Api\Data\SourceInterface $source */ ?>
                            <input
                                    class="admin__control-checkbox"
                                    id="source-selection-<?= $block->escapeHtmlAttr($source->getSourceCode()) ?>"
                                    type="checkbox"
                                    name="sources[]"
                                    value="<?= $block->escapeHtmlAttr($source->getSourceCode()) ?>"/>
                            <label
                                    for="source-selection-<?= $block->escapeHtmlAttr($source->getSourceCode()) ?>">
                                &nbsp;&nbsp;<?= $block->escapeHtml($source->getName())?>
                            </label><br />
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </fieldset>
    </form>
</div>

<script>
    require(['jquery'], function ($) {
        $('#back').click(function () {
            self.location.href='<?= /* @noEscape */ $block->getUrl('catalog/product/index') ?>';
        });

        $('#bulk-operation').click(function () {
           $('#bulk-operation-form').submit();
        });
    })
</script>
