$session_bid, 'causes.isactive' => 1]; } else { $where = ['causes.isactive !=' => NULL]; } $model = new BooksModel(); $model->setTable('causes'); $data['page_name'] = 'Causes List'; $data['details'] = $model->where('causes.business_id', $session_bid)->where('causes.isactive', 1)->findAll(); // echo '
';
            // print_r($data); die;
            $this->render_page('book_list', $data);
        } else {
            // Session is not active or user is not logged in
            return redirect()->to('login');
        }
    }

    ## To Load Book Form (Add/Update)
    public function add_causes($id)
    {
        helper('session');
        $data['session_bid'] = get_business_id();
    
        // Fetch categories from the model
        $model = new BooksModel();
    
        if ($id === '0') {
            $data['page_name'] = 'Add Causes';
            $data['details'] = [];
        } elseif ($id !== '0') {
            $data['page_name'] = 'Edit Causes';
            $data['details'] = $model->where('causes.causes_id', $id)->findAll();
           
        }
        
        $this->render_page('book_form', $data);
    }
    
    ## For inserting/updating details of book
    public function insert_books()
    {
     try{
        helper('session');
        $session_uid = get_logged_user_id();
        $session_bid = get_business_id();

        $BooksModel = new BooksModel();
        $data = [

            'name' => $this->request->getPost('name'),
            'description' => $this->request->getPost('description'),
            'business_id'=>$session_bid
            
        ];
        $causes_id = $this->request->getPost('causes_id'); // Get the book ID for update purpose

        if (empty($causes_id)) {

            // It's an insert operation
            $data['isactive'] = 1;
            $data['created_by'] = $session_uid;
            $causes_id = $BooksModel->insert($data);
            
        
            if ($causes_id) {
                session()->setFlashdata('success', 'causes successfully created.');
                $this->logger->info("Books: has been added successfully. Inserted ID = " . $BooksModel->insertID());
            } else {
                session()->setFlashdata('error', 'causes could not be created. Please try again..');
                $this->logger->error("Books: Err could not be added. Please try again.");
            }
        } else {

            // It's an update operation
            $isactive = $this->request->getPost('isactive');
            $data['isactive'] = ($isactive == 'on') ? 1 : 0;
            $data['updated_by'] = $session_uid;
            $BooksModel->update($causes_id, $data);
    
           
            if ($BooksModel->update($causes_id, $data)) {
                session()->setFlashdata('success', 'causes successfully updated.');
                $this->logger->info("Books: has been updated successfully. Updated Book ID = " . $causes_id);
            } else {
                session()->setFlashdata('error', 'causes updation failed. Please try again.');
                $this->logger->error("Books: Err Failed to update ID =" . $causes_id);
            }
        }
        } catch (\Exception $e) {
            $this->logger->error("Book: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine());
            session()->setFlashdata('error', 'Message: ' . $e->getMessage());
        }
    
        return redirect()->route('causes_list');
    }

    ## For delete the book details (Which means inactive the details)
    public function delete_books($id)
    {
        try {
            helper('session');
            $session_uid = get_logged_user_id();
            // print_r($session_uid);
            $BooksModel = new BooksModel();
                $data = ['isactive' => 0 , 'updated_by' => $session_uid];
                // print_r($data); die;

                if ($BooksModel->update($id, $data)) {
                    session()->setFlashdata('success', 'causes successfully deleted.');
                    $this->logger->info("BOOK: has been Inactived successfully. Inactived ID = " . $id);
                } else {
                    $this->logger->error("BOOK: Not able to Inactive ID =" . $id);
                    throw new \Exception("Data Not able to Deleted");
                }
        } catch (\Exception $e) {
            $this->logger->error("Book: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine());
            session()->setFlashdata('error', 'Message: ' . $e->getMessage());
        }
        return redirect()->route('causes_list');
    }


}