<?php

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

class Calendar_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function addEvent($data = [])
    {
        if ($this->db->insert('calendar', $data)) {
            $lastId = $this->db->insert_id();
            $customId = 'appo' . ($lastId);
            $this->db->where('real_id', $lastId)->update('calendar', array('id' => $customId));
            return true;
        }
        return false;
    }


    public function deleteEvent($id)
    {
        if ($this->db->delete('calendar', ['id' => $id])) {
            return true;
        }
        return false;
    }

    public function getEventByID($id)
    {
        $q = $this->db->get_where('calendar', ['id' => $id], 1);
        if ($q->num_rows() > 0) {
            return $q->row();
        }
        return false;
    }

    public function getEvents($start, $end)
    {
        $this->db->select('id, title, start, end, description, color, stylist_name, product_name');
        $this->db->where('start >=', $start)->where('start <=', $end);
        if ($this->Settings->restrict_calendar) {
            $this->db->where('user_id', $this->session->userdata('user_id'));
        }

        $q = $this->db->get('calendar');

        if ($q->num_rows() > 0) {
            foreach (($q->result_array()) as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    public function updateEvent($id, $data = [])
    {
        if ($this->db->update('calendar', $data, ['id' => $id])) {
            return true;
        }
        return false;
    }
    
    public function getAllProductList() {
        $query = $this->db->select('name')->get('products');
        if ($query->num_rows() > 0) {
            $result = $query->result();
            $data = array();
            foreach($result as $row) {
                $data[]= $row;
            }
            return $data;
        }
        return false;
    }
}
