<?php

namespace App\Components\LeadSource;

use App\Models\LeadSource\LeadSource;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class Edit extends Component {
    use AuthorizesRequests;

    public $display_name;
    public LeadSource $leadSource;

    public function mount() {
        $this->authorize('sources.edit', $this->leadSource);
        $this->display_name = $this->leadSource->display_name;
    }

    public function saveSource() {
        $this->authorize('sources.edit');
        $data = $this->validate([
            'display_name' => 'required|max:255|string|unique:lead_sources,display_name,' . $this->leadSource->id,
        ]);

        $this->leadSource->update($data);

        return redirect()->route('sources.show', $this->leadSource);
    }

    public function render() {
        return view('components.lead-source.edit');
    }
}
