<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Student_makeup_model extends MY_Model
{

    public $short = "makeup";
    public $table;
    public $tbl;
    public $database;

    public function __construct()
    {
        parent::__construct();
        $this->tbl = 'student_makeup';
        $this->table = $this->tbl . ' ' . $this->short;
    }

    public function create($params)
    {
        $this->db = $this->load->database($this->database, true);

        $existing = $this->db->from($this->table_prefix . $this->tbl)
            ->where('student_id', $params['student_id'])
            ->where('from_item_id', $params['from_item_id'])
            ->get()
            ->row_array();

        if (!$existing) {
            $res = [
                'student_id' => $params['student_id'],
                'from_item_id' => $params['from_item_id'],
                'to_item_id' => $params['to_timeslot_id'],
                'from_datetime' => (new DateTime($params['from_datetime']))->getTimestamp(),
                'to_datetime' => (new DateTime($params['to_datetime']))->getTimestamp(),
            ];
            $this->db->insert($this->table_prefix . $this->tbl, $res);

            $res['id'] = $this->db->insert_id();
            return $res;
        } else {
            return $existing;
        }
    }
}
