// app/Http/Livewire/MemberRegistration.php
namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Member;
use Livewire\WithFileUploads;

class MemberRegistration extends Component
{
    use WithFileUploads;
    
    public $member_id;
    public $name;
    public $phone;
    public $photo;
    public $isLoading = false;
    public $success = false;
    
    protected $rules = [
        'member_id' => 'required|unique:members,member_id|max:20',
        'name' => 'required|max:100',
        'phone' => 'required|unique:members,phone|max:15',
        'photo' => 'nullable|image|max:2048',
    ];
    
    public function save()
    {
        $this->validate();
        
        $this->isLoading = true;
        
        try {
            $member = Member::create([
                'member_id' => $this->member_id,
                'name' => $this->name,
                'phone' => $this->phone,
                // ... অন্যান্য ফিল্ড
            ]);
            
            if ($this->photo) {
                $path = $this->photo->store('members', 'public');
                $member->photo = $path;
                $member->save();
            }
            
            $this->success = true;
            $this->resetForm();
            
        } catch (\Exception $e) {
            session()->flash('error', 'এরর: ' . $e->getMessage());
        }
        
        $this->isLoading = false;
    }
    
    private function resetForm()
    {
        $this->reset(['member_id', 'name', 'phone', 'photo']);
    }
    
    public function render()
    {
        return view('livewire.member-registration');
    }
}