<?php

namespace Inoby;

use WPBS_Payment;

require_once __DIR__ . "/class-inoby-wpbs-outputter.php";

class WPBS_Prices_Outputter extends WPBS_Outputter {
  private WPBS_Payment $payment;
  private array $prices;

  public function __construct(WPBS_Payment $payment) {
    $this->payment = $payment;
    $this->prices = $payment->get("prices");
  }

  public function table_body(): void {
    echo '<tr class="collapse-trigger" data-name="individual_days_trigger">';
    $first_cell = '<span class="icon"></span>' . $this->prices["events"]["name"] . "&nbsp;<strong>&times;&nbsp;" . $this->prices["quantity"] . "</strong>";
    $this->print_cells($first_cell, "-", $this->format_price($this->prices["events"]["price"]));
    echo "</tr>";

    foreach ($this->prices["events"]["individual_days"] as $date => $price) {
      echo '<tr class="is-collapsible" data-trigger-name="individual_days_trigger" data-depth="1" style="display: none;">';
      $this->print_cells("&emsp;" . $this->format_date($date), $this->format_price($price), "");
      echo "</tr>";
    }

    if (isset($this->prices["extras"]) && !empty($this->prices["extras"])) {
      foreach ($this->prices["extras"] as $item) {
        $label = $item["label"];

        if ($item["addition"] == "per_day" && $item["multiplication"]) {
          $label .= "&nbsp;<strong>&times;&nbsp;" . $this->prices["quantity"] * $item["multiplication"] . "</strong>";
        } elseif ($item["addition"] == "per_booking" && $item["multiplication"]) {
          $label .= "&nbsp;<strong>&times;&nbsp;" . $item["multiplication"] . "</strong>";
        }

        $this->print_row($label, $this->format_price($item["price"]), $this->format_price($item["total"]));
      }
    }

    if (isset($this->prices["discount"]) && !empty($this->prices["discount"])) {
      foreach ($this->prices["discount"] as $item) {
        $this->print_row($item["description"], "-", $this->format_price($item["value"]));
      }
    }
  }
}
