<?php
/** @var bool $hasPickup */
/** @var bool $hasDelivery */
/** @var bool $pickup */
/** @var bool $hasFA */
/** @var \Soft_Tehnica\Freya_Sync_Service\Data\Location[] $locations */
/** @var ?string $activeLocationUid */
/** @var ?string $userAddress */
/** @var ?string $noAddressMessage */
/** @var bool $hideDismiss */
?>
<div class="fss-modal" id="fss-modal">
  <div class="fss-modal__container">
    <h1 class="fss-modal__title"><?php esc_html_e( 'Preferințe livrare', 'freya-sync-service' ); ?></h1>
    <h2 class="fss-modal__subtitle"><?php esc_html_e( 'Pentru a putea alege locația care vă va servi, vă rugăm să vă alegeți preferințele de livrare', 'freya-sync-service' ); ?></h2>
    <?php if ( $hasPickup === true && $hasDelivery === true ): ?>
      <section class="fss-delivery-choice-container">
        <fieldset class="fss-delivery-choice">
          <legend><?php esc_html_e( 'Selectați o metodă de livrare', 'freya-sync-service' ); ?></legend>
          <div>
            <input type="radio" name="deliveryType" id="fssDelivery" value="1"
              <?= $pickup === false ? 'checked="checked"' : '' ?>>
            <label for="fssDelivery"><?php esc_html_e( 'Livrare', 'freya-sync-service' ); ?></label>
          </div>
          <div>
            <input type="radio" name="deliveryType" id="fssPickup" value="2"
              <?= $pickup ? 'checked="checked"' : '' ?>>
            <label for="fssPickup"><?php esc_html_e( 'Ridicare personală', 'freya-sync-service' ); ?></label>
          </div>
        </fieldset>
        <div class="fss-delivery-choice"></div>
      </section>
    <?php endif; ?>
    <div class="fss-modal__body">
      <form class="fss-user-address-form">
        <?php if ( $hasDelivery ): ?>
          <div class="fss-modal__fields_group">
            <div class="fss-form-item-container flex-long">
              <label for="streetName"><?php esc_html_e( 'Stradă și număr', 'freya-sync-service' ); ?></label>
              <input type="text" id="streetName" name="streetName" class="streetName" autocomplete="off">
            </div>
            <div class="fss-form-item-container">
              <label for="blockNo"><?php esc_html_e( 'Bloc', 'freya-sync-service' ); ?></label>
              <input type="text" id="blockNo" name="blockNo" class="blockNo">
            </div>
            <div class="fss-form-item-container">
              <label for="entranceNo"><?php esc_html_e( 'Scară', 'freya-sync-service' ); ?></label>
              <input type="text" id="entranceNo" name="entranceNo" class="entranceNo">
            </div>
            <div class="fss-form-item-container">
              <label for="floor"><?php esc_html_e( 'Etaj', 'freya-sync-service' ); ?></label>
              <input type="text" id="floor" name="floor" class="fss-floor">
            </div>
            <div class="fss-form-item-container">
              <label for="apartmentNo"><?php esc_html_e( 'Apartament', 'freya-sync-service' ); ?></label>
              <input type="text" id="apartmentNo" name="apartmentNo" class="apartmentNo">
            </div>
          </div>
        <?php endif; ?>
        <?php if ( $hasPickup ): ?>
          <div class="location-select-container">
            <label for="location"><?php esc_html_e( 'Alegeți locația de la care ridicați comanda', 'freya-sync-service' ); ?></label>
            <select id="location" name="location">
              <?php foreach ( $locations as $location ): ?>
                <?php if ( $location->hasPickup ): ?>
                  <?php $selected = $activeLocationUid !== null && $activeLocationUid === $location->uid; ?>
                  <option value="<?= esc_attr( $location->uid ) ?>" <?= $selected ? 'selected' : '' ?>>
                    <?= esc_html( $location->name ) ?></option>
                <?php endif; ?>
              <?php endforeach; ?>
            </select>
          </div>
        <?php endif; ?>
      </form>
      <?php if ( $noAddressMessage ): ?>
        <p class="fss-modal__subtitle noAddressFoundMessage"><?= wp_kses_post( $noAddressMessage ) ?></p>
      <?php endif; ?>

      <?php if ( isset( $userAddress ) && strlen( $userAddress ) > 0 ): ?>
        <p class="fss-modal__subtitle">
          <?php
          // translators: %s: user's current delivery address
          printf( esc_html__( 'Adresa dumeavoastră curentă este: %s', 'freya-sync-service' ), esc_html( $userAddress ) );
          ?>
        </p>
      <?php endif; ?>
      <p class="fss-modal__subtitle"><?php esc_html_e( 'Schimband adresa, este posibil să se șteargă și coșul de cumpărături în funcție de locația din care se va face livrarea.', 'freya-sync-service' ); ?></p>
      <p id='localizationModalErrorMessages'></p>
      <p id='localizationModalSuccessMessages'></p>

      <div class="fss-modal__buttons">
        <?php if ( ! $hideDismiss ): ?>
          <button class="btn-secondary address-modal-close">
            <?php if ( $hasFA ): ?>
              <i class="fa fa-spinner fa-spin cancelLoading"></i>
            <?php endif; ?>
            <?php esc_html_e( 'Anulează', 'freya-sync-service' ); ?>
          </button>
        <?php endif; ?>
        <button class="btn-primary address-modal-save">
          <?php if ( $hasFA ): ?>
            <i class="fa fa-spinner fa-spin mLoading"></i>
          <?php endif; ?>
          <?php esc_html_e( 'Salvează', 'freya-sync-service' ); ?>
        </button>
      </div>
    </div>
  </div>
</div>
