<?php

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

class Project extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->permission->is_admin();
        //$this->output->enable_profiler(TRUE);
        $this->load->model('Information_model');
        $this->load->model('InfoStructures_model');
        $this->load->model('Operator_model');
        $this->load->model('InfoSMS_model');
        $this->load->model('Logs_model');
        $this->load->model('Infoprojects_model');
        $this->load->model('Infoprojecttasks_model');
    }

    public function index() {
        $admin_info = $this->session->userdata('session_admin');
        $data['project_list'] = $this->Infoprojects_model->all();
        $data['nav_view'] = $this->load->view('bootstrap/project/nav', array('nav_active' => 'none'), true);
        $this->load->view('bootstrap/header', $data);
        $this->load->view('bootstrap/project/welcome');
        $this->load->view('bootstrap/footer');
    }


    public function edit($p_id=false) {
		if(!empty($p_id))
		{
			$data['project']=$this->Infoprojects_model->detail($p_id);
			$data['nav_view'] = $this->load->view('bootstrap/project/nav', array('nav_active' => 'none'), true);
		}
		else
		{
			$data['nav_view'] = $this->load->view('bootstrap/project/nav', array('nav_active' => 'create'), true);
		}
		
		$data['task_list'] = array();
		if(!empty($data['project']))
		{
			$data['task_list']=$this->Infoprojecttasks_model->task_list($p_id);
		}
		
		$data['operator_list']=$this->Operator_model->all();
        
        
        $this->load->view('bootstrap/header', $data);
        $this->load->view('bootstrap/project/edit');
        $this->load->view('bootstrap/footer');
    }
	
	
	public function create()
	{
		$this->edit();
	}

	public function edit_submit()
	{
		$admin_info = $this->session->userdata('session_admin');
		
        $this->form_validation->set_rules('p_title', 'lang:p_title', 'required');
        $this->form_validation->set_rules('p_bonus', 'lang:p_bonus', 'numeric');
        if ($this->form_validation->run() == FALSE) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
        } else {
            //任务ID，用来判断当前任务是新增还是更新
            $p_id = $this->input->post('p_id');
            if (!empty($p_id)) {
                //更新
                $this->Infoprojects_model->update($p_id, $this->input->post('p_title'),$this->input->post('p_type'),$this->input->post('p_manager')
				,$this->input->post('p_bonus'),$this->input->post('p_memo'),$this->input->post('p_requirer')
				,$this->input->post('p_solution'),$this->input->post('p_state'));
				$data[] = array('name' => 'ok', 'value' => $this->lang->line('project_update_success'));
            } else {
				//新建
                $this->Infoprojects_model->add($this->input->post('p_title'),$this->input->post('p_type'),$this->input->post('p_manager')
				,$this->input->post('p_bonus'),$this->input->post('p_memo'),$this->input->post('p_requirer')
				,$this->input->post('p_solution'),$this->input->post('p_state'));
				$p_id=$this->Infoprojects_model->insert_id;
				$data[] = array('name' => 'ok', 'value' => $this->lang->line('project_create_success'));
				$data[] = array('name' => 'ok_go', 'value' => site_url('project/edit/'.$p_id));
            }
			$this->Logs_model->backup_project($p_id,$this->input->post('p_solution'));
        }
        echo json_encode($data);
	}

}

