<?php
namespace PortalPeople\DisabledPostcodes\Block\Adminhtml\Postcode;

use Magento\Backend\Block\Template;
use Magento\Store\Model\System\Store as SystemStore;
use PortalPeople\DisabledPostcodes\Api\DisabledPostcodeManagementInterface;

class Edit extends Template
{
    protected $_template = 'PortalPeople_DisabledPostcodes::postcode/edit.phtml';

    private DisabledPostcodeManagementInterface $management;
    private SystemStore $systemStore;

    public function __construct(
        Template\Context $context,
        DisabledPostcodeManagementInterface $management,
        SystemStore $systemStore,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->management = $management;
        $this->systemStore = $systemStore;
    }

    public function getRow(): array
    {
        $id = (int)$this->getRequest()->getParam('id');
        if ($id <= 0) {
            return [
                'id' => 0,
                'postcode' => '',
                'match_type' => 'area',
                'store_id' => 0,
                'description' => '',
                'is_active' => true,
            ];
        }

        return $this->management->getById($id);
    }

    public function getSaveUrl(): string
    {
        return $this->getUrl('disabledpostcodes/postcode/save');
    }

    public function getBackUrl(): string
    {
        return $this->getUrl('disabledpostcodes/postcode/index');
    }

    public function getStoreOptions(): array
    {
        return $this->systemStore->getStoreValuesForForm(false, true);
    }
}
