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

class Report_violation_model extends CI_Model
{
    protected $_table='report_violations';

    public function __construct(){
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
    }

    public function add($dataInsert)
    {
        $this->db->insert($this->_table, $dataInsert);
        $insert_id = $this->db->insert_id();
        return $insert_id;
    }

    public function getById($id, $status=0){
        $this->db->select($this->_table.'.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table.'.id', $id);
        $this->db->where($this->_table.'.status', $status);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function cmsCountAll($name = null)
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        if ($name != null) {
            $this->db->where('date', date('Y-m-d', strtotime($name)));
        }
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    /**
     * Hàm lấy dữ liệu phân trang
     *
     * @param $record
     * @param $start
     * @param null $name
     *
     * @return mixed
     */
    public function cmsGetPagination($record, $start, $name = null)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->order_by($this->_table . '.created_at', 'desc');
        $this->db->limit($record, $start);
        $query = $this->db->get();

        return $query->result_array();
    }

}