<?php
/**
 * @var \Illuminate\Database\Eloquent\Collection $card
 */

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode;
use Endroid\QrCode\Writer\PngWriter;
use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
use kernel\widgets\IconBtn\IconBtnEditWidget;
use kernel\widgets\IconBtn\IconBtnListWidget;

$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($card, [
    'params' => ["class" => "table table-bordered", "border" => "2"],
    'baseUrl' => "/admin/card",
]));
$table->beforePrint(function () use ($card) {
    $btn = IconBtnListWidget::create(['url' => '/admin/card'])->run();
    $btn .= IconBtnEditWidget::create(['url' => '/admin/card/update/' . $card->id])->run();
    $btn .= IconBtnDeleteWidget::create(['url' => '/admin/card/delete/' . $card->id])->run();
    return $btn;
});

$table->rows([
    'card_file_id' => function ($data) {
        $file = \kernel\app_modules\card\models\CardFile::find($data)->file;
        return $file ? \kernel\helpers\Html::img($file) : "";
    },
    'status' => function ($data) {
        return \kernel\app_modules\card\models\Card::getStatus()[$data];
    }
]);

$table->create();
$table->render();

//$writer = new PngWriter();
//
//// Create QR code
//$qrCode = new QrCode(
//    data: $card->id,
//    encoding: new Encoding('UTF-8'),
//    errorCorrectionLevel: ErrorCorrectionLevel::Low,
//    size: 120,
//    margin: 2,
//    roundBlockSizeMode: RoundBlockSizeMode::Margin,
//    foregroundColor: new Color(0, 0, 0),
//    backgroundColor: new Color(255, 255, 255)
//);
//
//
//$result = $writer->write($qrCode);
//echo "<img src='".$result->getDataUri()."'>";
//
//$path = $card->cardFile->file;
//$mainImg = new \kernel\helpers\ImageGD(ROOT_DIR . $path);
//$qrImg = new \kernel\helpers\ImageGD($result->getDataUri());
//$mainImg->addImg($qrImg->getImg(), 200, 15, 0, 0, 124, 124, 100);
//$mainImg->save(RESOURCES_DIR . "/tmp/with_qr.png");

//$cardFile = \kernel\app_modules\card\services\CardService::createCardPNG($card);
//echo \kernel\helpers\Html::img("/resources/tmp/card_tpl.png");
//if ($cardFile) {
//    echo \kernel\helpers\Html::img($cardFile);
//}

