<?php

namespace App\Livewire\Studio\Loyalty\Voucher;

use App\Livewire\Forms\Studio\Loyalty\VoucherForm;
use App\Models\Outlet;
use App\Models\Voucher;
use App\Traits\WithAuthorization;
use App\Traits\WithOutletSelector;
use App\Traits\WithToast;
use App\Traits\WithUpdatedData;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Ubah Voucher')]
class Edit extends Component
{
    use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData;

    public VoucherForm $form;

    public array $outlets;

    public function mount(Voucher $voucher)
    {
        $this->form->setVoucher($voucher);

        $this->outlets = Outlet::pluck('name', 'id')->toArray();
    }

    public function save()
    {
        $this->canOrAbort('update voucher');

        $this->form->update();

        $this->toast('Voucher berhasil diperbarui.');

        $this->redirectRoute('studio.loyalty.voucher.index', navigate: true);
    }

    public function render()
    {
        return view('livewire.studio.loyalty.voucher.form', [
            'pageTitle' => 'Ubah Voucher',
        ]);
    }
}
