<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 25/12/2015
 * Time: 2:22 CH
 */
if (!defined('BASEPATH')) exit('No direct script access allowed');

class CmsTrack extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
        //
        $this->load->model('track_model');
        $this->load->model('user_model');
        $this->load->model('actionlog_model');
    }

    public function listTrack()
    {
        $this->_data['functionName'] = 'Track';
        $this->_data['action'] = 'listTrack';
        $this->_data['titlePage'] = 'Track';
        $this->_data['loadPage'] = 'backend/cms_track/list_track_view';
        $this->load->view('backend/admin_template_view', $this->_data);
    }

    public function listTrackAjax()
    {
        $data = array();
        $fByName = $this->input->post('fByName');
        $pageId = $this->input->post('pageId');
        //
        $pageId = ($pageId == 0) ? 1 : $pageId;
        //
        $limit = 100;
        $offset = ($pageId - 1) * $limit;
        $data['offset'] = ($pageId - 1) * $limit;
        $totalRecord = $this->track_model->cmsCountAll($fByName);
        $data['pagination'] = MyHelper::genPaginationLink($totalRecord, $limit, $pageId);
        $data['listData'] = $this->track_model->cmsGetPagination($limit, $offset, $fByName);
        //
        $this->load->view('backend/ajax/cms_track/list_track_view', $data);
    }

    public function editTrack($track_id, $id_user = '', $id_artist = '')
    {
        $this->_data['contest'] = $contest = $this->track_model->getById($track_id, $id_user, $id_artist);
        $track_user = '';
        $this->_data['track_artists'] = $track_artists = '';
        if ($id_user != 0) {
            $track_user = $this->track_model->getByIdUser('track_user', $id_user);
        $this->_data['track_user'] = $track_user;
        }
        if ($id_artist != 0) {
            $track_artists = $this->track_model->getByIdUser('track_artists', $id_artist);
            $this->_data['track_artists'] = $track_artists;
        }
        $this->_data['list_user'] = $list_user = $this->track_model->getListUsers();
        // print_r($id_user.'Nhạc sĩ'.$id_artist);
        // Kiểm tra trùng giá trị nếu thay đổi ở form

        //Thiết lập validate
        $this->form_validation->set_rules(
            'id',
            'ID',
            'trim'
        );

        $this->form_validation->set_rules(
            'title',
            'Số ngày diễn ra cuộc thi',
            'trim|required|xss_clean'
        );

        $this->form_validation->set_message('required', '<li>Bắt buộc nhập, chọn.</li>');
        $this->form_validation->set_message('is_unique', '<li>Đã tồn tại.</li>');
        
        // Xử lý form đăng nhập
        if($this->form_validation->run($this) == false) {
            // $this->_data['date_limit'] = $this->input->post('name');
            $this->_data['title'] = $this->input->post('title');
            $this->_data['update_singer'] = $this->input->post('update_singer');
            $this->_data['singer'] = $this->input->post('singer');
            $this->_data['artist'] = $this->input->post('artist');
            $this->_data['update_artist'] = $this->input->post('update_artist');
            //
            
            $this->_data['functionName'] = 'Cập nhật thông tin';
            $this->_data['action'] = 'editTrack';
            $this->_data['titlePage'] = 'Cập nhật thông tin';
            $this->_data['loadPage'] = 'backend/cms_track/edit_track_view';
            $this->load->view('backend/admin_template_view', $this->_data);
        } else {
            
            $title = $this->input->post('title');
            $update_singer = $this->input->post('update_singer');
            $singer = ($track_user != '') ? $track_user[0]['user_id']: '';
            $artist = ($track_artists != '') ? $track_artists[0]['user_id']: '';
            $update_artist = $this->input->post('update_artist');
            $id_singer = ($update_singer > 0) ? $update_singer: $singer;
            $id_artists = ($update_artist > 0) ? $update_artist: $artist;
            ($singer || $id_user) ? $active_singer = 'update': $active_singer = 'insert';
            ($artist || $id_artist) ? $active_artist = 'update': $active_artist = 'insert';
            // die($active_artist.$active_singer.'ca sĩ'.$id_singer.'/'.$singer.'nhạc sĩ'.$id_artists.'/'.$artist);

            // Xử lý upload: avatar
            $db_art_path = '';
            $upload_path = 'uploads/tracks/arts';
            $upload_full_path = '/home/aseanvn/public_html/vmusicchart.dcv.vn/' . $upload_path;
            $config['upload_path']          = $upload_full_path;
            $config['file_name'] =  md5($track_id.time());
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 20480;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;

            $this->load->library('upload', $config);

            if ($this->upload->do_upload('avatar')) {
                $upload_data = $this->upload->data();
                $db_art_path = $upload_path . '/' . $upload_data['file_name'];
            }

            $this->track_model->update_track_artists($track_id, $id_artists, $id_artist, $active_artist);
            $this->track_model->update_track_user($track_id, $id_singer, $id_user, $active_singer);
            $this->track_model->update($track_id, $title, $db_art_path);

            $this->session->set_flashdata('success', 'Cập nhật bài hát thành công');
            redirect(base_url().'backend/cmsTrack/showTrack/'.$track_id);
        }
    }
    
    public function showTrack($track_id)
    {
        $this->_data['track']= $this->track_model->getById($track_id);
        //
        $this->_data['functionName'] = 'Thông tin Bài Hát';
        $this->_data['action'] = 'showTrack';
        $this->_data['titlePage'] = 'Thông tin Bài Hát';
        $this->_data['loadPage'] = 'backend/cms_track/show_track_view';
        $this->load->view('backend/admin_template_view', $this->_data);
    }

}