<?
$orderId = (int)($this -> order['id'] ?? 0);
?>

<div class="site-title">Szczegóły zamówienia: <?= htmlspecialchars((string)($this -> order['number'] ?? ''), ENT_QUOTES, 'UTF-8');?></div>

<div class="mb15">
  <a href="/admin/shop_order/list/" class="btn btn-dark btn-sm mr5">
    <i class="fa fa-reply"></i> Wstecz
  </a>
  <a href="/admin/shop_order/order_edit/order_id=<?= $orderId;?>" class="btn btn-danger btn-sm mr5">
    <i class="fa fa-pencil"></i> Edytuj zamówienie
  </a>

  <? if ( $this -> prev_order_id ):?>
    <a href="/admin/shop_order/order_details/order_id=<?= (int)$this -> prev_order_id;?>" class="btn btn-success btn-sm mr5">
      <i class="fa fa-arrow-left"></i> Poprzednie zamówienie
    </a>
  <? endif;?>

  <? if ( $this -> next_order_id ):?>
    <a href="/admin/shop_order/order_details/order_id=<?= (int)$this -> next_order_id;?>" class="btn btn-success btn-sm mr5">
      <i class="fa fa-arrow-right"></i> Następne zamówienie
    </a>
  <? endif;?>

  <button id="integrationsDropdownBtn" type="button" class="btn btn-primary btn-sm pull-right">
    <i class="fa fa-ellipsis-v"></i>
  </button>
</div>

<div class="details order-details panel">
  <div class="panel-body">
  <div class="row">
  <div class="col-lg-8">
    <div class="row">
      <div class="col-sm-6 col-xl-5">
        <div class="text-big">Numer zamówienia: <b id="order-number"><?= $this -> order[ 'number' ];?></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-number' ); return false;"></i></div>
        <div>Data zamówienia <b><?= $this -> order[ 'date_order' ];?></b></div>
        <br>
        <div class="text-big">Dane do dostawy:</div>
        <div><b id="order-user-name"><?= $this -> order[ 'client_name' ];?> <?= $this -> order[ 'client_surname' ];?></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-user-name' ); return false;"></i></div>
        <? if ( $this -> order[ 'client_street' ] ):?>
          <div><b id="order-street"><?= $this -> order[ 'client_street' ];?></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-street' ); return false;"></i></div>
        <? endif;?>
        <div>
          <? if ( $this -> order[ 'client_postal_code' ] ):?>
            <b id="order-postal-code"><?= $this -> order[ 'client_postal_code' ];?></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-postal-code' ); return false;"></i>
          <? endif;?>
          <? if ( $this -> order[ 'client_city' ] ):?>
            <b id="order-city"><?= $this -> order[ 'client_city' ];?></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-city' ); return false;"></i>
          <? endif;?>
        </div>
        <div><b><a href="mailto:<?= $this -> order[ 'client_email' ];?>" id="order-user-email"><?= $this -> order[ 'client_email' ];?></a></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-user-email' ); return false;"></i></div>
        <div><b><a href="tel:<?= $this -> order[ 'client_phone' ];?>" id="order-user-phone"><?= preg_replace( "/(\d{3})(\d{3})(\d{3})/", "$1 $2 $3", $this -> order[ 'client_phone' ] );?></a></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-user-phone' ); return false;"></i></div>
        <? if ( $this -> order[ 'firm_name' ] ):?>
          <br>
          <div class="text-big">Dane do faktury:</div>
          <div><b id="order-firm-name"><?= $this -> order[ 'firm_name' ];?></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-firm-name' ); return false;"></i></div>
          <div><b id="order-firm-street"><?= $this -> order[ 'firm_street' ];?></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-firm-street' ); return false;"></i></div>
          <div>
            <b id="order-firm-postal-code"><?= $this -> order[ 'firm_postal_code' ];?></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-firm-postal-code' ); return false;"></i>
            <b id="order-firm-city"><?= $this -> order[ 'firm_city' ];?></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-firm-city' ); return false;"></i>
          </div>
          <div>NIP: <b id="order-firm-nip"><?= $this -> order[ 'firm_nip' ];?></b> <i class="fa fa-copy" onclick="copyToClipboard( 'order-firm-nip' ); return false;"></i></div>
        <? endif;?>
        <div class="resend_order_confirmation_email">
          <button class="btn btn-primary">wyślij ponownie mail z potwierdzeniem zamówienia</button>
        </div>
      </div>
      <div class="col-sm-6 col-xl-7">
        <div class="panel">
          <div class="panel-body">
            <div>Kwota zamówienia <b><?= $this -> order[ 'summary' ];?> zł</b></div>
            <? if ( $this -> coupon ):?>
              <div>Kod rabatowy: <span style="color: #cc0000;"><?= $this -> coupon -> name;?> - <?= $this -> coupon -> amount;?> <?= $this -> coupon -> type == 1 ? '%' : 'zł';?></span></div>
            <? endif;?>
            <br>
            <div><?= strip_tags( $this -> order[ 'transport' ] );?>: <b><?= $this -> order[ 'transport_cost' ];?> zł</b></div>
            <? if ( $this -> order['transport_id'] == 2 and $this -> order[ 'inpost_paczkomat' ] ):?>
              <? $paczkomat = explode( '|', $this -> order[ 'inpost_paczkomat' ] );?>
              <div>Paczkomat: <b id="order-paczkomat"><?= $paczkomat[0];?></b> | <?= $paczkomat[1];?> <i class="fa fa-copy" onclick="copyToClipboard( 'order-paczkomat' ); return false;"></i></div>
            <? endif;?>
            <? if ( $this -> order['transport_id'] == 9 and $this -> order[ 'orlen_point' ] ):?>
              <? $orlen = explode( '|', $this -> order[ 'orlen_point' ] );?>
              <div>Punkt Orlen: <b id="order-orlen"><?= $orlen[0];?></b> | <?= $orlen[1];?> <i class="fa fa-copy" onclick="copyToClipboard( 'order-orlen' ); return false;"></i></div>
            <? endif;?>
            <br/>
            <div>
              <b><?= $this -> order[ 'payment_method' ];?> </b>
            </div>
          </div>
        </div>
        <div class="paid-status panel">
          <div class="panel-body">
            <? if ( $this -> order['paid'] ):?>
                  <a href="/admin/shop_order/set_order_as_unpaid/order_id=<?= $orderId;?>" class="set_order_as_unpaid">
                <span><i class="fa fa-dollar"></i></span>
                <b>Oznacz zamówienie jako nieopłacone</b>
              </a>
            <? else:?>
                  <a href="/admin/shop_order/set_order_as_paid/order_id=<?= $orderId;?>" class="set_order_as_paid">
              <span class="danger"><i class="fa fa-dollar"></i></span>
                <b>Oznacz zamówienie jako opłacone</b>
              </a>
            <? endif;?>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-6 col-lg-4">
    <div class="order-status text-big">
      <?= $this -> order_statuses[ $this -> order[ 'status' ] ];?>
    </div>
    <div class="form-group status_select">
      <select class="form-control" id="order-status">
        <? foreach ( $this -> order_statuses as $key => $val ):?>
        <option value="<?= $key;?>" <? if ( $key == $this -> order[ 'status' ] ):?>selected="selected"<? endif;?>>
          <?= $val;?>
        </option>
        <? endforeach;?>
      </select>
    </div>
    <div class="row">
      <div class="col-12 buttons">
        <button class="btn btn-primary order_status_change_email">zmień status i wyślij email</button>
        <button class="btn btn-success order_status_change">zmień status</button>
      </div>
      <div class="col-12 order-history">
        <? if ( \Shared\Helpers\Helpers::is_array_fix( $this -> order['statuses'] ) ): foreach ( $this -> order['statuses'] as $status ):?>
          <div><b><?= date( 'Y-m-d H:i', strtotime( $status['date'] ) );?></b>: <?= $this -> order_statuses[$status['status_id']];?> <? if ( $status['mail'] ):?><i class="fa fa-envelope-o"></i><? endif;?></div>
        <? endforeach; endif;?>
      </div>
    </div>
  </div>
    <div class="d-none d-lg-block col-lg-4">
    </div>
    <div class="d-none d-lg-block col-lg-4">
    </div>
    <div class="col-sm-6 col-lg-4">
      <div class="text-big">Wiadomość do zamówienia:</div>
      <div> <b> <?= $this -> order[ 'message' ];?> </b></div>
    </div>
  </div>
  <div class="row">
    <div class="col-12">
      <div class="table-responsive">
      <table class="table">
        <thead>
          <tr>
            <th>Zdjęcie</th>
            <th scope="col">Nazwa</th>
            <th scope="col" class="tab-center">Ilość</th>
            <th scope="col" class="tab-right">Cena / szt:</th>
            <th scope="col" class="tab-right">Cena / szt (po rabacie):</th>
            <th scope="col" class="tab-right">Suma (po rabacie):</th>
          </tr>
        </thead>
        <tbody>
          <? if ( is_array( $this -> order[ 'products' ] ) ): foreach ( $this -> order[ 'products' ] as $product ):?>
          <?
              if ( $id = ( new \Domain\Product\ProductRepository( $GLOBALS['mdb'] ) )->getParentId( $product['product_id'] ) )
                $product_id = $id;
              else
                $product_id = $product['product_id'];
          ?>
          <tr class="order-product-details">
            <td class="product-image">
              <? if ( $product['product_id'] ):?>
                <img src="<?= \shop\Product::getProductImg( (int)$product_id );?>">
              <? endif;?>
            </td>
            <td>
              <a href="<?= \shop\Product::getProductUrl( (int)$product_id );?>" target="_blank"><?= $product[ 'name' ];?></a>
              <br />
              <div class="atributes">
                <?= $product[ 'attributes' ];?>
              </div>
              <div class="custom-fields">
                <?= $product[ 'custom_fields' ];?>
              </div>
              <div class="product-message">
                <?= $product[ 'message' ] != '' ? '<strong>Wiadomość:</strong> ' . $product['message'] : '';?>
              </div>
            </td>
            <td class="tab-center"><?= $product[ 'quantity' ];?></td>
            <td class="tab-right"><?= \Shared\Helpers\Helpers::decimal( $product[ 'price_brutto' ] );?> zł</td>
            <td class="tab-right"><?= \Shared\Helpers\Helpers::decimal( $product[ 'price_brutto_promo' ] );?> zł</td>
            <td class="tab-right"><?= \Shared\Helpers\Helpers::decimal( $product[ 'price_brutto_promo' ] * $product[ 'quantity' ] );?> zł</td>
          </tr>
          <? endforeach; endif;?>
        </tbody>
      </table>
    </div>
    </div>
  </div>
  <div class="row">
    <div class="col-12">
      <form>
        <div class="form-group">
          <label for="notes">Notatki</label>
          <textarea class="form-control" id="notes" rows="10"><?= $this -> order['notes'];?></textarea>
        </div>
      </form>
    </div>
  </div>
  </div>
</div>

<div class="dropdown-menu dropdown-menu-right" id="integrationsDropdownMenu">
  <a class="dropdown-item btn-send-order-to-apilo" href="/admin/shop_order/send_order_to_apilo/order_id=<?= $orderId;?>">
    <i class="fa fa-refresh"></i> Wyślij ponownie zamówienie do apilo.com
  </a>
  <a class="dropdown-item btn-toggle-trustmate" href="#">
    <i class="fa fa-<?= $this -> order['trustmate_send'] ? 'times' : 'check';?>"></i>
    <?= $this -> order['trustmate_send'] ? 'Odznacz zamówienie jako wysłane do trustmate.io' : 'Zaznacz zamówienie jako wysłane do trustmate.io';?>
  </a>
</div>
<?= \Shared\Tpl\Tpl::view('shop-order/order-details-custom-script', [
  'order_id' => $orderId,
  'trustmate_send' => (int)($this -> order['trustmate_send'] ?? 0),
]);?>