<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\HeroiconsOutline;
use Magento\Customer\Block\Address\Grid;
use Magento\Framework\Escaper;

/** @var Grid $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */

/** @var HeroiconsOutline $heroicons */
$heroicons = $viewModels->require(HeroiconsOutline::class);
?>

<div class="block block-addresses-list">
    <div class="block-title text-2xl mb-6">
        <?= $escaper->escapeHtml(__('Address Book')) ?>
    </div>
    <div class="block-content" x-data="initAddresses()">
        <?php if ($additionalAddresses = $block->getAdditionalAddresses()): ?>
            <div class="card grid grid-cols-3 lg:grid-cols-6 my-6">
                <div class="text-sm text-secondary p-2">
                    <?= $escaper->escapeHtml(__('Company')) ?>
                </div>
                <div class="hidden lg:block text-sm text-secondary p-2">
                    <?= $escaper->escapeHtml(__('Name')) ?>
                </div>
                <div class="hidden lg:block text-sm text-secondary p-2">
                    <?= $escaper->escapeHtml(__('Street Address')) ?>
                </div>
                <div class="text-sm text-secondary p-2">
                    <?= $escaper->escapeHtml(__('City')) ?>
                </div>
                <div class="hidden lg:block text-sm text-secondary p-2">
                    <?= $escaper->escapeHtml(__('Country')) ?>
                </div>
                <div class="text-sm text-secondary p-2"></div>
                <?php $i = 0; ?>
                <?php foreach ($additionalAddresses as $address): ?>
                    <?php $i++ ?>
                    <div class="p-2 <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                        <?= $escaper->escapeHtml($address->getCompany()) ?>
                    </div>
                    <div class="hidden lg:block p-2 <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                        <?= $escaper->escapeHtml($address->getLastname()) ?>,
                        <?= $escaper->escapeHtml($address->getFirstname()) ?>
                    </div>
                    <div class="hidden lg:block p-2 <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                        <?= $escaper->escapeHtml($block->getStreetAddress($address)) ?>
                    </div>
                    <div class=" p-2 <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                        <?= $escaper->escapeHtml($address->getCity()) ?>
                    </div>
                    <div class="hidden lg:block p-2 <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                        <?= $escaper->escapeHtml($block->getCountryByCode($address->getCountryId())) ?>
                    </div>
                    <div class="p-2 <?= $i % 2 === 0 ? '' : ' bg-container-darker' ?>">
                        <a class="action edit inline-block text-sm underline text-secondary-darker"
                            title="<?= $escaper->escapeHtmlAttr(__('Edit')) ?>"
                            href="<?= $escaper->escapeUrl($block->getUrl(
                                'customer/address/edit',
                                ['id' => $address->getId()]
                            )) ?>">
                            <?= $heroicons->pencilAltHtml('', 24, 24, ['aria-hidden' => true]); ?>
                            <span></span>
                        </a>
                        <a class="action delete ml-2 inline-block text-sm underline text-secondary-darker"
                           title="<?= $escaper->escapeHtmlAttr(__('Delete')) ?>"
                           @click.prevent="deleteAddressById(<?= $escaper->escapeJs($address->getId()) ?>)" href="#">
                            <?= $heroicons->trashHtml('', 24, 24, ['aria-hidden' => true]); ?>
                        </a>
                    </div>
                <?php endforeach; ?>
            </div>
            <?php if ($block->getChildHtml('pager')): ?>
                <div class="customer-addresses-toolbar toolbar bottom">
                    <?= $block->getChildHtml('pager') ?>
                </div>
            <?php endif; ?>

        <?php else: ?>
            <p class="empty">
                <?= $escaper->escapeHtml(__('You have no other address entries in your address book.')) ?>
            </p>
        <?php endif ?>

        <script>
            function initAddresses() {
                return {
                    deleteAddressById(id) {
                        if (window.confirm(
                            '<?= $escaper->escapeJs(__('Are you sure you want to delete this address?')) ?>'
                        )) {
                            hyva.postForm(
                                {
                                    "action": '<?= $escaper->escapeJs($block->getDeleteUrl()) ?>',
                                    "data": {
                                        "id": id
                                    }
                                }
                            );
                        }
                    }
                }
            }
        </script>
    </div>
</div>

<div class="actions-toolbar mb-3">
    <div class="primary">
        <a href="<?= $escaper->escapeUrl($block->getUrl('customer/address/new')) ?>"
           class="btn btn-primary">
            <span>
                <?= $escaper->escapeHtml(__('Add New Address')) ?>
            </span>
        </a>
    </div>
    <div class="secondary">
        <a class="action back"
           href="<?= $escaper->escapeUrl($block->getUrl('customer/account')) ?>">
            <span>
                <?= $escaper->escapeHtml(__('Back')) ?>
            </span>
        </a>
    </div>
</div>
