<?php

declare(strict_types=1);

namespace app\models;

use yii;
use yii\base\Model;

use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
// use PhpOffice\PhpSpreadsheet\Writer\Html;
use PhpOffice\PhpSpreadsheet\Reader\Html as HtmlReader;

use Exception;

class Invoice extends Model
{
    public static function generate(string|int $order_key, string $html): void
    {
        // Инициализация директории
        $dir = YII_PATH_PUBLIC . '/../assets/invoices/' . $order_key;

        // Сохранение на диск
        if (!file_exists($dir) && !mkdir($dir, 0775, true)) throw new Exception('Не удалось записать директорию:' . $dir);

        $reader = new HtmlReader();
        $spreadsheet = $reader->loadFromString($html);

        $writer = new Xlsx($spreadsheet);
        $writer->save($dir . '/invoice.xlsx');

        // $reader = new XlsxReader();
        // $reader->setReadDataOnly(true);
        // $spreadsheet = $reader->load('../views/invoice/order/original.xlsx');

        // $writer = new Html($spreadsheet);
        // $writer->save('hello world.html');
    }
}
