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

class Track_model extends CI_Model
{
    protected $_table = 'tracks';
    protected $track_user = 'track_user';
    protected $track_artists = 'track_artists';

    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 add_singer($user_id, $track_id)
    {
        $this->db->select('full_name');
        $this->db->where('id', $user_id);
        $query = $this->db->get('users')->result_array();
        $this->db->set('user_id', $user_id);
        if ($query) {
            $this->db->set('name', $query[0]['full_name']);
        }
        $this->db->set('track_id', $track_id);
        $this->db->insert($this->track_user);
    }

    public function add_artist($user_id, $track_id)
    {
        $this->db->select('full_name');
        $this->db->where('id', $user_id);
        $query = $this->db->get('users')->result_array();
        $this->db->set('user_id', $user_id);
        if ($query) {
            $this->db->set('name', $query[0]['full_name']);
        }
        $this->db->set('track_id', $track_id);
        $this->db->insert($this->track_artists);
    }

    public function update($id, $dataUpdate, $art_path)
    {
        $this->db->set('title', $dataUpdate);
        $this->db->set('art', $art_path);
        $this->db->where('id', $id);
        $this->db->update($this->_table);
    }

    public function update_track_artists($id_track, $user_id, $id, $active)
    {
        // die($dataUpdate);
        $this->db->select('full_name');
        $this->db->where('id', $user_id);
        $query = $this->db->get('users')->result_array();
        $this->db->set('user_id', $user_id);
        if ($query) {
            $this->db->set('name', $query[0]['full_name']);
        }
        if ($active == 'insert') {
            $this->db->set('track_id', $id_track);
            $this->db->insert($this->track_artists);
        } else {
            $this->db->where('track_id', $id_track);
            $this->db->where('id', $id);
            $this->db->update($this->track_artists);
        }
    }

    public function update_track_user($id_track, $user_id, $id, $active)
    {
        $this->db->select('full_name');
        $this->db->where('id', $user_id);
        $query = $this->db->get('users')->result_array();
        $this->db->set('user_id', $user_id);
        if ($query) {
            $this->db->set('name', $query[0]['full_name']);
        }
        if ($active == 'insert') {
            $this->db->set('track_id', $id_track);
            $this->db->insert($this->track_user);
        } else {
            $this->db->where('track_id', $id_track);
            $this->db->where('id', $id);
            $this->db->update($this->track_user);
        }
    }

    public function getById($id)
    {
        $this->db->select($this->_table . '.*, track_user.name AS name_singer, 
            track_user.id AS id_singer,
            track_artists.name AS artist, 
            track_artists.id AS id_track_artist');
        $this->db->from($this->_table);
        $this->db->join($this->track_user, 'track_user.track_id = tracks.id', 'left');
        $this->db->join($this->track_artists, 'track_artists.track_id = tracks.id', 'left');
        $this->db->where($this->_table . '.id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }
     
    public function getByIdUser($table, $id) {
        if ($id != 0) {
            $this->db->select("*");
            $this->db->where('id', $id);
            return $this->db->get($table)->result_array();
        } else
            return false;
        
    }
    /**
     * Lay cuoc thi hien hanh
     * @return mixed
     */
    public function getCurrentContest()
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where('is_active', 1);
        $this->db->order_by('created_at', 'desc');
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function cmsCountAll($name = null, $fBySinger = null, $fByComposer = null)
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        if ($name != null) {
            $this->db->like('title', $name, 'both');
            $this->db->or_like('description', $name, 'both');
            $this->db->or_like('slug', $name, 'both');
            $this->db->or_like('slug', MyHelper::genSlug($name), 'both');
            $this->db->or_like('tag', $name, 'both');
            $this->db->or_where('id', intval($name));
        }
        if ($fBySinger == 1) {
            $this->db->where('singer_list is NOT NULL', null, false);
        } else if ($fBySinger == 2) {
            $this->db->where('singer_list', null);
        }
        if ($fByComposer == 1) {
            $this->db->where('composer_list is NOT NULL', null, false);
        } else if ($fByComposer == 2) {
            $this->db->where('composer_list', null);
        }
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    public function cmsGetPaginationBk($record, $start, $name = null)
    {
        $this->db->select($this->_table . '.*, u1.full_name AS singer_name, u2.full_name AS composer_name,
            track_user.name AS singer_name2, track_user.id AS id_user,
            track_artists.name AS composer_name2, track_artists.id AS id_artist');
        $this->db->from($this->_table);
        $this->db->join($this->track_user, 'track_user.track_id = tracks.id', 'left');
        $this->db->join($this->track_artists, 'track_artists.track_id = tracks.id', 'left');
        $this->db->join('users u1', 'u1.id = track_user.user_id', 'left');
        $this->db->join('users u2', 'u2.id = track_artists.user_id', 'left');
        if ($name != null) {
            $this->db->like('tracks.title', $name, 'both');
            $this->db->or_like('tracks.description', $name, 'both');
            $this->db->or_like('tracks.slug', $name, 'both');
            $this->db->or_like('tracks.tag', $name, 'both');
        }
        // $where = 'track_user.user_id !=0 AND track_artists.user_id != 0';
        // $this->db->where($where);
        $this->db->order_by($this->_table . '.title', 'asc');
        $this->db->limit($record, $start);
        $query = $this->db->get();

        return $query->result_array();
    }

    public function cmsGetPagination($record, $start, $name = null, $fBySinger = null, $fByComposer = null)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        if ($name != null) {
            $this->db->like('tracks.title', $name, 'both');
            $this->db->or_like('tracks.description', $name, 'both');
            $this->db->or_like('tracks.slug', $name, 'both');
            $this->db->or_like('tracks.slug', MyHelper::genSlug($name), 'both');
            $this->db->or_like('tracks.tag', $name, 'both');
            $this->db->or_where('id', intval($name));
        }
        if ($fBySinger == 1) {
            $this->db->where('singer_list is NOT NULL', null, false);
        } else if ($fBySinger == 2) {
            $this->db->where('singer_list', null);
        }
        if ($fByComposer == 1) {
            $this->db->where('composer_list is NOT NULL', null, false);
        } else if ($fByComposer == 2) {
            $this->db->where('composer_list', null);
        }
        $this->db->order_by($this->_table . '.title', 'asc');
        $this->db->limit($record, $start);
        $query = $this->db->get();

        return $query->result_array();
    }

    public function getListForSelectBox()
    {
        $this->db->select('id, fullname, username, sid');
        $this->db->from($this->_table);
        $this->db->where('sid !=', '');
        $this->db->order_by($this->_table . '.username', 'asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getListUsers()
    {
        $this->db->select('*');
        $this->db->from('users');
        $this->db->order_by('users.full_name', 'asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Hàm lấy dữ liệu BXH ngày trong bảng report_views_daily
     * @param $daily_at
     * @param int $limit
     * @param int $offset
     *
     * @return mixed
     */
    public function getReportDaily($daily_at, $limit = 100, $offset = 0)
    {
        $this->db->from('report_views_daily');
        $this->db->where('date =', $daily_at);
        $this->db->limit($limit, $offset);
        $query = $this->db->get();

        return $query->result_array();
    }

    /**
     * Hàm lấy dữ liệu BXH tuần trong bảng report_views_weekly (cũ) và bảng report_views_weekly_new (từ 29/3-4/4)
     *
     * @param $week
     * @param $year
     * @param int $limit
     * @param int $offset
     *
     * @return mixed
     */
    public function getReportWeekly($week, $year, $limit = 100, $offset = 0)
    {
        //$sql = "SELECT * FROM report_views_weekly WHERE `week` = ? AND `year` = ? ORDER BY total_point DESC LIMIT {$offset}, {$limit} ";
        //$query  = $this->db->query($sql, $week, $year);

        if (date('W') >= 14 && date('Y' >= 2020)) {
            $this->db->from('report_views_weekly_new');
        } else {
            $this->db->from('report_views_weekly');
        }

        $this->db->where('week =', $week);
        $this->db->where('year =', $year);
        $this->db->limit($limit, $offset);
        $query = $this->db->get();

        return $query->result_array();
    }

    /**
     * Hàm lấy dữ liệu BXH tháng
     *
     * @param $month
     * @param $year
     * @param int $limit
     * @param int $offset
     *
     * @return mixed
     */
    public function getReportMonthly($month, $year, $limit = 100, $offset = 0)
    {
        //$sql = "SELECT * FROM reports_views_monthly WHERE `month` = ? AND `year` = ? ORDER BY total_point DESC LIMIT {$offset}, {$limit} ";
        //$query  = $this->db->query($sql, $month, $year);

        if (date('m') >= 4 && date('Y' >= 2020)) {
            $this->db->from('report_views_monthly_new');
        } else {
            $this->db->from('reports_views_monthly');
        }

        $this->db->where('month =', $month);
        $this->db->where('year =', $year);
        $this->db->limit($limit, $offset);
        $query = $this->db->get();

        return $query->result_array();
    }

    public function getUserByTrack($track_id) {
        //$query = $this->db->query("SELECT * FROM track_user WHERE track_id=?", $track_id);
        //if ($query) $users = $query->result_array();
        //return $users;

        $this->db->from($this->track_user);
        $this->db->where('track_id =', $track_id);
        $query = $this->db->get();

        return $query->result_array();
    }

    public function getArtistsByTrack($track_id) {
        //$query = $this->db->query("SELECT * FROM track_artists WHERE track_id=?", $track_id);
        //if ($query) $users = $query->result_array();
        //return $users;

        $this->db->from($this->track_artists);
        $this->db->where('track_id =', $track_id);
        $query = $this->db->get();

        return $query->result_array();
    }

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

    public function getByTitle($title, $id)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.title', $title);
        if ($id) {
            $this->db->where_not_in('id', $id);
        }
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */