<?php

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

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

    protected $table = 'admin_videos';

   
    var $order = array('id', 'link', 'target', 'buffered_count', 'remaining_count', 'completed_count', 'created_at');

    private function _get_data_query($where)
    {
        $this->db->select("$this->table.*");
        $this->db->from($this->table);
        $this->db->where("$this->table.type", "view");   // only for admin video - view

        if (isset($_POST['search']['value'])) {
            $searchTerm = $_POST['search']['value'];
            $this->db->group_start();
            $this->db->like('' . $this->table . '.link', $searchTerm);
            $this->db->or_like('' . $this->table . '.target', $searchTerm);
            $this->db->or_like('' . $this->table . '.completed_count', $searchTerm);
            $this->db->or_like('' . $this->table . '.created_at', $searchTerm);
            $this->db->group_end();
        }

        
        // print_r($_POST);
        $this->db->where($where);
        if (isset($_POST['order'])) {
            $this->db->order_by($this->order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } else {

            $this->db->order_by('id', 'desc');
        }
    }

    public function get_data_table($where)
    {

        $this->_get_data_query($where);
        if ($_POST['length'] != -1) {
            $this->db->limit($_POST['length'], $_POST['start']);
        }

        $query = $this->db->get();

        return $query->result_array();
    }

    public function count_filtered_data($where)
    {
        $this->_get_data_query($where);
        $query = $this->db->get();
        return $query->num_rows();
    }

    public function count_all_data()
    {
        $this->db->from($this->table);
        $this->db->where("$this->table.type", "view");   // only for admin video - view
        $this->db->where("$this->table.remove", "0");   
        return $this->db->count_all_results();
    }


    // Delete function
    public function delete_video($id) {
        $this->db->where('id', $id);
        return $this->db->update($this->table, ['remove' => '1']);
    }
    

    public function get_video_by_id($id) {
        return $this->db->get_where('admin_videos', ['id' => $id])->row_array();
    }
    
    public function update_video($id, $data) {
        $this->db->where('id', $id);
        return $this->db->update('admin_videos', $data);
    }
    
  
}
