<?php namespace App\Models;

use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;

class ScheduleModel extends Model
{
    protected $table = 'courseschedules';

    protected $allowedFields = ['courseID','centerID','location','trainerID','startDate','fees','endDate','createdBy','modifiedBy','modifiedAt'];

public function getScheduleDetails($scheduleId){
  $db      = \Config\Database::connect();
  $builder = $db->table('courseschedules');

  $builder->select('*');
  $builder->join('courses', 'courseschedules.courseID = courses.courseID');
  $builder->join('durationtypes', 'courses.durationType = durationtypes.typeID');
  $builder->join('centers', 'courseschedules.centerID = centers.centerID');
  $builder->join('users', 'courseschedules.trainerID = users.userID');
  $query = $builder->getWhere(['scheduleID' => $scheduleId]);

  return $query->getRow();



}
public function getCourseSchedules($courseId,$dateFrom,$dateTo){
  $db      = \Config\Database::connect();
  $builder = $db->table('courseschedules');

  $builder->select('*');
  $builder->join('centers', 'courseschedules.centerID = centers.centerID');
  $builder->join('users', 'courseschedules.trainerID = users.userID');
  $query = $builder->getWhere(['courseID' => $courseId, 'startDate'>=$dateFrom,'endDate'<=$dateTo]);

  return $query->getResult();

}

public function getUpcomingSchedules(){
  $db      = \Config\Database::connect();
  $builder = $db->table('courseschedules');
  $date=date('Y-m-d', time());


  $builder->select('*');
  $builder->join('courses', 'courseschedules.courseID = courses.courseID');
  $builder->join('durationtypes', 'courses.durationType = durationtypes.typeID');
  $where= "startDate >='$date'";
  $builder->limit(5)->where($where);
  $query = $builder->get();

  return $query->getResult();
  //$query->getResult();;


}



public function updateSchedule($scheduleId,$data){

    $db= \Config\Database::connect();
   $builder = $db->table('courseschedules');

    $builder->where('scheduleID', $scheduleId);
    $res=$builder->update($data);
    return $res;

}



}
