<?php
namespace Foxstudio\Plugins\Template\Utils;
use Foxstudio\Plugins\Template\Repository as Repository;
class DeliveryTab {
    public function __construct()
    {
        add_filter('woocommerce_product_tabs', [$this, 'addCustomTab']);
    }

    function addCustomTab($tabs)
    {

        // Adds the new tab

        $tabs['delivery_tab'] = array(
            'title' => __('Informacja o dostępności', 'pandaGadzety'),
            'priority' => 50,
            'callback' => [$this, 'customTabData']
        );

        return $tabs;

    }
    
    function customTabData()
    {
        global $product;
        $sku = $product->get_sku();

        \wp_enqueue_style("deliveryStyle", plugins_url("/pandaGadzety/assets/css/delivery.css"));

        $product_repository = new Repository\ProductRepository(SERVER_URL);
        $results = $product_repository->getDeliveryInfo($sku);

        $new_tab = [];
        foreach ($results as $data) {
            if($data['type'] == "central_stock") {
                $new_tab[$data['sku']]["central_stock"] = [
                    "amount" => $data['amount'],
                    "arrival_date" => $data['arrival_date']
                ]; 
            }
            if($data['type'] == "external_stock") {
                $new_tab[$data['sku']]["external_stock"] = [
                    "amount" => $data['amount'],
                    "arrival_date" => $data['arrival_date']
                ]; 
            }
            if($data['type'] == "incoming_to_central_stock") {
                $new_tab[$data['sku']]["incoming_to_central_stock"] = [
                    "amount" => $data['amount'],
                    "arrival_date" => $data['arrival_date']
                ]; 
            }
            if($data['type'] == "incoming_to_external_stock") {
                $new_tab[$data['sku']]["incoming_to_external_stock"] = [
                    "amount" => $data['amount'],
                    "arrival_date" => $data['arrival_date']
                ]; 
            }
            
        }

        $tab = "<div id='delivery-tab'>";
        $tab .= "<table>";
        $tab .= "<tr>";
        $tab .= "<td>SKU</td>";
        $tab .= "<td colspan='2'>Magazyn centralny</td>";
        $tab .= "<td colspan='2'>Magazyn zewnętrzny</td>";
        $tab .= "</tr>";
        $tab .= "<tr>";
        $tab .= "<td>SKU</td>";
        $tab .= "<td>Na magazynie</td>";
        $tab .= "<td>Nadchodząca dostawa na magazyn</td>";
        $tab .= "<td>Dostępne w ciągu 6-8 dni</td>";
        $tab .= "<td>Nadchodząca dostawa na magazyn</td>";
        $tab .= "</tr>";
        foreach ($new_tab as $key => $value) {
            $tab .= "<tr>";
            $tab .= "<td>" . $key . "</td>";
            $tab .= "<td>" . $value["central_stock"]['amount'] . " szt.</td>";
            $tab .= "<td>";
            $tab .= !empty($value["incoming_to_central_stock"]['amount']) ? $value["incoming_to_central_stock"]['amount'] . "szt.": "0 szt.";
            $tab .= "<span>". (!empty($value["incoming_to_central_stock"]['arrival_date']) ? $value["incoming_to_central_stock"]['arrival_date'] : "") ."</span>";
            $tab .= "</td>";
            $tab .= "<td>";
            $tab .= !empty($value["external_stock"]['amount']) ? $value["external_stock"]['amount'] . "szt.": "0 szt.";
            $tab .= "</td>";
            $tab .= "<td>";
            $tab .= !empty($value["incoming_to_external_stock"]['amount']) ? $value["incoming_to_external_stock"]['amount'] . "szt.": "0 szt.";
            $tab .= "<span>" . (!empty($value["incoming_to_external_stock"]['arrival_date']) ? $value["incoming_to_external_stock"]['arrival_date'] : "") . "</span>";
            $tab .= "</td>";
            $tab .= "</tr>";
            
        }
        $tab .= "</table>";

        $tab  .= "</div>";


        echo $tab;
    }
    
}