<?php

namespace App\Livewire\Studio\Manage\Restock;

use App\Models\Restock;
use App\Traits\Authorization\WithAuthorization;
use App\Traits\Components\WithCloseModal;
use App\Traits\Components\WithConfirmation;
use App\Traits\Components\WithToast;
use App\Traits\Notification\WithSubscribeNotification;
use App\Traits\Restock\WithRestockStock;
use App\Traits\Utilities\WithRequestNotification;
use Flux\Flux;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Restock Toko')]
class Index extends Component
{
    use WithAuthorization, WithCloseModal, WithConfirmation, WithRequestNotification, WithRestockStock, WithSubscribeNotification, WithToast;

    public function delete(Restock $restock): void
    {
        $this->canOrAbort('delete restock');

        DB::transaction(function () use ($restock) {
            foreach ($restock->items as $item) {
                $this->reverseStockTransfer($restock->warehouse, $restock->outlet, $item);
            }

            $restock->delete();
        });

        $this->dispatch('refreshDatatable');

        $this->toast('Restock berhasil dihapus.');

        Flux::modals()->close();
    }

    public function render(): View
    {
        return view('livewire.studio.manage.restock.index', [
            'pageTitle' => 'Restock Toko',
        ]);
    }
}
