<?php

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

class Taskhall extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->permission->is_admin();
        //$this->output->enable_profiler(TRUE);
        $this->load->model('Infotasks_model');
        $this->load->model('Infotaskarticles_model');
        $this->load->model('InfoSMS_model');
    }

    public function index($page = 1) {
        $data = array();
        $data['author_rank_list'] = $this->Infotasks_model->get_author_rank_list();
        $data['article_rank_list'] = $this->Infotasks_model->get_article_rank_list();

        $t_td_type = $start_date = $tasktitle = false;
        $end_date = date('Y-m-d');
        if ($this->input->get('tasktype') && $this->input->get('tasktype') != '-')
            $t_td_type = $this->input->get('tasktype');
        if ($this->input->get('startdate') && $this->input->get('startdate') != '')
            $start_date = $this->input->get('startdate');
        if ($this->input->get('enddate') && $this->input->get('enddate') != '')
            $end_date = $this->input->get('enddate');
        if ($this->input->get('tasktitle') && $this->input->get('tasktitle') != '')
            $tasktitle = $this->input->get('tasktitle');

        //分页
        $page_task = $this->Infotasks_model->get_tasks_list(false, 0, $t_td_type, $start_date, $end_date, $tasktitle, $page);
        $pages['total'] = count($page_task);
        $pages['pageSize'] = 20;
        $pages['url'] = site_url('taskhall/index');
        $pages['current'] = $page;
        //$this->load->library('mypage',$pages);
        //$data['page']=$this->mypage->pages_html();
        $data['page'] = show_page($pages);

        //获取任务列表
        $task_id_str = 0;
        $page_task = array_slice($page_task, $pages['pageSize'] * ($page - 1), $pages['pageSize']);
        foreach ($page_task as $v) {
            $task_id_str.=',' . $v->t_id;
        }
        $data['task_list'] = $this->Infotasks_model->get_list_by_taskids($task_id_str);

        $data['nav_view'] = $this->load->view('nav_hall', array('nav_active' => 'hall') + $data, true);
        $data['rank_view'] = $this->load->view('rank', $data, true);
        $this->load->view('header', array('navbar_active' => 'taskhall') + $data);
        $this->load->view('task_hall');
        $this->load->view('footer');
    }

    public function view($t_id) {
        $data['task'] = $this->Infotasks_model->detail($t_id);
        if (empty($data['task'])) {
            show_404();
            return false;
        }
        $data['task_detail'] = $this->Infotaskarticles_model->detail($data['task']->t_id);

        $data['nav_view'] = $this->load->view('nav_hall', array('nav_active' => 'hall') + $data, true);
        $this->load->view('header', array('navbar_active' => 'taskhall') + $data);
        $this->load->view('read_task');
        $this->load->view('footer');
    }

    //?н?????
    public function undertake() {
        $admin_info = $this->session->userdata('session_admin');
        $t_id = $this->input->post('t_id');
        $task = $this->Infotasks_model->detail($t_id);
        if (empty($task)) {
            $data[] = array('name' => 'no_modal', 'value' => $this->lang->line('task_error_notfound'));
            echo json_encode($data);
            return false;
        }
        if ($task->t_a_id<=0) {
            //?н?????
            $this->Infotasks_model->undertake($t_id, $admin_info->a_id);
            $data[] = array('name' => 'ok_modal', 'value' => $this->lang->line('task_undertake_success'));
            $data[] = array('name' => 'ok_go', 'value' => site_url('welcome/view/' . $t_id));
            echo json_encode($data);
            return true;
        } else {
            $data[] = array('name' => 'no_modal', 'value' => $this->lang->line('task_has_undertake'));
            echo json_encode($data);
            return false;
        }
    }

    //ȡ??????
    public function canceltake() {
        $admin_info = $this->session->userdata('session_admin');
        $t_id = $this->input->post('t_id');
        $task = $this->Infotasks_model->detail($t_id);
        if (empty($task)) {
            $data[] = array('name' => 'no_modal', 'value' => $this->lang->line('task_error_notfound'));
            echo json_encode($data);
            return false;
        }
        if ($task->t_a_id == $admin_info->a_id) {
            //ȡ??????
            $this->Infotasks_model->canceltake($t_id);
            $data[] = array('name' => 'ok_modal', 'value' => $this->lang->line('task_canceltake_success'));
            $data[] = array('name' => 'ok_go', 'value' => site_url('welcome/view/' . $t_id));
            echo json_encode($data);
            return true;
        } else {
            $data[] = array('name' => 'no_modal', 'value' => $this->lang->line('task_canceltake_error'));
            echo json_encode($data);
            return false;
        }
    }

    public function notice($announce_id = '', $announce_type = 'notice') {
        $data = array();
        $data['type'] = $announce_type;
        if ($announce_id == '') {
            $data['announce_list'] = $this->Infotaskarticles_model->get_announce($announce_id, $announce_type);
        } else {
            $announce = $this->Infotaskarticles_model->get_announce($announce_id, $announce_type);
            $data['announce_detail'] = $announce[0];
        }
        $data['author_rank_list'] = $this->Infotasks_model->get_author_rank_list();
        $data['article_rank_list'] = $this->Infotasks_model->get_article_rank_list();
        $data['avatar'] = get_author_photo();
        $data['suer_weburl'] = get_user_web_url();

        $data['nav_view'] = $this->load->view('nav', array('nav_active' => 'notice') + $data, true);
        $data['rank_view'] = $this->load->view('rank', $data, true);
        $this->load->view('header', array('navbar_active' => 'notice') + $data);
        $this->load->view('notice');
        $this->load->view('footer');
    }

    public function announce($announce_id = '') {
        $this->notice($announce_id, 'announce');
    }

    public function help() {
        $data = array();
        $data['nav_view'] = $this->load->view('nav_hall', array('nav_active' => 'help') + $data, true);
        $this->load->view('header', array('navbar_active' => 'notice') + $data);
        $this->load->view('help');
        $this->load->view('footer');
    }

}

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