<?php

namespace Modules\Team\Models;

use CodeIgniter\Model;

class TeamModel extends Model
{

  protected $table = 'team';
    protected $primaryKey = 'id';

    protected $allowedFields = ['title', 'alias', 'published_date', 'email', 'photo', 'is_block', 'position', 'category_id', 'content', 'excerpt', 'birthday', 'died_at', 'achievements', 'relationship_status', 'status', 'hits', 'urls', 'metadesc', 'metakey', 'facebook', 'twitter', 'instagram', 'tiktok', 'linkedin', 'snapchat',  'created_by', 'updated_at', 'updated_by'];

    protected $useTimestamps  = true;
    protected $useSoftDeletes = true;
    
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';


    //Get search for Team // Innovators
    public function searchTeam($searchTerm)
    {
        $builder = $this->db->table('team as t');

        if (!empty($searchTerm)) 
        {
          $builder->like('t.title', $searchTerm)
                  ->orWhere('t.alias', $searchTerm)
                  ->orWhere('t.photo', $searchTerm)
                  ->orWhere('t.position', $searchTerm)
                  ->orWhere('t.excerpt', $searchTerm);
                  //->groupBy('t.id');
        }

        $query = $builder->get();
        return $query->getResultArray(); 
    }

    //Get all Menus
    public function getAll()
    {
      $builder = $this->db->table('team as t');
      $builder->select('t.*, tc.name as category_name');
      $builder->join('team_category as tc', 'tc.id=t.category_id');
      $builder->where('t.deleted_at', null);
      $builder->orderBy('t.id', 'DESC');
	    $result = $builder->get()->getResult();
	    return $result;
    }

    //List only Members NOT Team Blocks
    public function getAllNotTeamBlockMembers()
    {
      $builder = $this->db->table('team as t');
      $builder->select('t.*, tc.name as category_name');
      $builder->join('team_category as tc', 'tc.id=t.category_id');
      $builder->where('t.deleted_at', null);
      $builder->where('is_block','No');
      $builder->orderBy('t.id', 'DESC');
	    $result = $builder->get()->getResult();
	    return $result;
    }

    //List only Members created by Team Blocks
    public function getAllTeamBlockMembers()
    {
      $builder = $this->db->table('team as t');
      $builder->select('t.*, tc.name as category_name');
      $builder->join('team_category as tc', 'tc.id=t.category_id');
      $builder->where('t.deleted_at', null);
      $builder->where('is_block','Yes');
      $builder->orderBy('t.id', 'DESC');
	    $result = $builder->get()->getResult();
	    return $result;
    }

    public function getSingleMember($id)
    {
      $builder = $this->db->table('team as t');
      $builder->select('t.*, tc.name as category_name');
      $builder->join('team_category as tc', 'tc.id=t.category_id');
      $builder->where('t.id', $id);
      $builder->orWhere('t.alias', $id);
	    $result = $builder->get()->getRow();
	    return $result;
    }

    //Get Team members in Category
    public function getTeamMembersInCategory($category)
    {
      $builder = $this->db->table('team as t');
      $builder->select('t.*, tc.name as category_name');
      $builder->join('team_category as tc', 'tc.id=t.category_id');
      $builder->where('t.deleted_at', null);
      $builder->where('t.status', 'published');
      $builder->where('t.category_id', $category);
      $builder->orWhere('tc.alias', $category);
      $builder->orderBy('t.published_date', 'ASC');
	    $result = $builder->get()->getResult();
	    return $result;
    }

    //Get Other Innovators - exclusive of current
    public function getOtherInnovators($id, $category)
    {
      $builder = $this->db->table('team as t');
      $builder->select('t.*, tc.name as category_name');
      $builder->join('team_category as tc', 'tc.id=t.category_id');
      $builder->where('t.deleted_at', null);
      $builder->where('t.status', 'published');
      $builder->where('t.category_id', $category);
      $builder->where('tc.alias', $category);
      
      // Exclude the current post from the list
      $builder->where('t.id !=', $id);
      $builder->orWhere('t.alias !=', $id);

      $builder->orderBy('RAND()');
      $builder->limit(10);
		  $result = $builder->get()->getResult();
		  return $result;
    }
   
    //Show One Team Member per day Randomly
    public function showMemberonPages()
    {
      $builder = $this->db->table('team as t');
      $builder->select('t.*, tc.name as category_name');
      $builder->join('team_category as tc', 'tc.id=t.category_id');
      $builder->where('t.deleted_at', null);
      $builder->where('t.status', 'published');
      $builder->orderBy('RAND()');
      $builder->limit(1);
		  $result = $builder->get()->getResult();
		  return $result;
    }


}
