<?php

use app\libraries\CommonHelper;
use app\libraries\DateHelper;
use app\libraries\Tenants;
use app\libraries\Userstamp;

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

class Location_model extends MY_Model
{
    public $database = 'default';

    public function __construct()
    {
        parent::__construct();
    }

    public function get()
    {
        return $this->db->select("*")
            ->from("mas_room")
            ->where("display", "1")
            ->get()
            ->result();
    }

    public function get_id($id)
    {
        return $this->db->select('*')
            ->from('mas_room')
            ->where('id', $id)
            ->get()
            ->row_array();
    }

    public function insert($params)
    {
        Userstamp::$timestamp = true;
        if (isset($params['created_by'])) {
            $params['created_at'] = $params['create_time'];
            unset($params['id']);
            unset($params['create_time']);
            $this->db->insert("mas_room", $params);
        } else {
            Userstamp::$timestamp = true;
            $this->db->insert('mas_room', Userstamp::create($params));
        }

        $params['id'] = $this->db->insert_id();
        return $params;
    }

    public function update($params, $id)
    {
        if (isset($params['updated_by'])) {
            $params['updated_at'] = $params['update_time'];
            unset($params['id']);
            unset($params['update_time']);
            $this->db->update("mas_room", $params, ['id' => $id]);
        } else {
            Userstamp::$timestamp = true;
            $this->db->update("mas_room", Userstamp::update($params), ['id' => $id]);
        }

        $params['id'] = $id;
        return $params;
    }

    public function delete($id)
    {
        $this->db->from("mas_room")
            ->where('id', $id)
            ->delete();
    }

    public function count()
    {
        return $this->db->from("mas_room")
            ->where("display", "1")
            ->count_all_results();
    }

    public function count_filter($param)
    {

        $db = $this->load->database($this->database, TRUE);
        $searchVal = trim($param['search']['value']);

        $search = [];
        if (trim($searchVal) !== "") {
            foreach ($param['columns'] as $column) {
                if ($column['searchable'] === "true") {
                    $search["L" . "." . $column['data']] = $searchVal;
                }
            }
        }

        $db->or_like($search);
        $db->from("mas_room L");
        return $db->count_all_results();
    }

    public function listing($params)
    {
        $fields = ['id', 'name', 'capacity', "create_time", 'update_time', 'status'];
        $orders = [];
        $start = $params['start'];
        $length = $params['length'];
        $joins = [];
        $searchVal = trim($params['search']['value']);
        $search = [];
        if (trim($searchVal) !== "") {

            foreach ($params['columns'] as $column) {

                if ($column['searchable'] === "true") {

                    $search["L ". "." . $column['data']] = $searchVal;
                }
            }
        }
        $result = $this->getFromDB($this->database, "room L", $fields, $start, $length, $orders, $search, $joins);
        $data = [];
        foreach ($result as $row) {
            $data[] = [
                'id' => $row['id'],
                'name' => $row['name'],
                'capacity' => $row['capacity'],
                'create_time' => gmdate('Y-m-d', $row['create_time']),
                'update_time' => $row['update_time'] == null ? "未有更新記錄" : gmdate('Y-m-d', $row['update_time']),
                'status' => $row['status'] == "1" ? "啟用" : "無法使用",
                'edit' => '<a href="/backend/location/edit?id=' . $row['id'] . '" class="btn btn-secondary">詳細</a>',
                'delete' => '<input type="checkbox" class="checkbox-delete" id="' . $row['id'] . '">'

            ];
        }
        return $data;
    }
}
