<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_EditOrder
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\EditOrder\Block\Adminhtml\Shipment\Edit;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Framework\Data\FormFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Mageplaza\EditOrder\Block\Adminhtml\Order\Renderer\Tracking;
use Mageplaza\EditOrder\Helper\Data as HelperData;

/**
 * Class Form
 * @package Mageplaza\EditOrder\Block\Adminhtml\Shipment\Edit
 */
class Form extends Generic
{
    /**
     * @var HelperData
     */
    protected $_helperData;

    /**
     * Form constructor.
     *
     * @param Context $context
     * @param Registry $registry
     * @param FormFactory $formFactory
     * @param HelperData $helperData
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        FormFactory $formFactory,
        HelperData $helperData,
        array $data = []
    ) {
        $this->_helperData = $helperData;

        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * @return Generic
     * @throws LocalizedException
     */
    protected function _prepareForm()
    {
        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create([
            'data' => [
                'id' => 'mp_shipment_addtrack_form',
                'action' => $this->getUrl(
                    'mpeditorder/shipment/addTrack/',
                    [
                        'form_key'      => $this->getFormKey(),
                        'mass_shipment' => true
                    ]
                ),
                'method' => 'post',
                'enctype' => 'multipart/form-data',
            ],
        ]);

        $form->setHtmlIdPrefix('shipment_');
        $form->setFieldNameSuffix('shipment');

        $form->addField('add_tracking', 'note', [])
            ->setAfterElementHtml(
                $this->_helperData->getLoadTrackingHtml($this->getViewFileUrl('images/loader-1.gif'))
            );

        $form->addField('tracking', Tracking::class, ['name' => 'tracking']);

        $form->setUseContainer(true);
        $this->setForm($form);

        return parent::_prepareForm();
    }
}
