<?php
namespace App\Controllers;

class Notifications extends BaseController
{
    public function index()
    {
        $uid = session()->get('user_id');
        $notifications = $this->db->table('notifications')->where('user_id',$uid)->orderBy('created_at','DESC')->limit(50)->get()->getResultArray();
        $this->db->table('notifications')->where('user_id',$uid)->update(['is_read'=>1]);
        return view('notifications/index', $this->viewData(['title'=>'Notifications','notifications'=>$notifications]));
    }

    public function markRead(int $id)
    {
        $this->db->table('notifications')->where('id',$id)->where('user_id',session()->get('user_id'))->update(['is_read'=>1]);
        return $this->response->setJSON(['status'=>true]);
    }

    public function markAllRead()
    {
        $this->db->table('notifications')->where('user_id',session()->get('user_id'))->update(['is_read'=>1]);
        return $this->response->setJSON(['status'=>true]);
    }
}
