<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email_log extends MY_Controller{
	public function index(){
		$this->load->view($this->router->fetch_class()."/index");
	}
	
	public function edit($id=""){
		$data['id'] = $id != ""? $id:0;
		
		$this->load->view($this->router->fetch_class()."/edit", $data);
	}
	
	public function load_edit(){
		$this->load->model("email_model");
		$this->load->model("group_model");
		$post = $this->input->post();
		
		//To prevent loss---
		$this->fm->keep_all_page_filter();
		//------------------
		
		if($this->input->post('ID') != ""){
			$data = array();
			if($post['ID'] != "0"){
				$data_array = $this->email_model->get_data("ID = '".$post['ID']."'", "", 0, 0, "", "", "", "CREATE_USER.NAME AS CREATE_USER_NAME");
				
				if(count($data_array) > 0){
					$data['data'] = $data_array[0];
				}
				else{
					header("HTTP/1.1 500 Internal Server Error");
					header("Content-Type: application/json; charset=UTF-8");
					return;
				}
			}
			
			$data['all_group'] = $this->group_model->get_data("", "", 0, 0, "<T>.NAME");
			
			$this->load->view($this->router->fetch_class()."/load_edit", $data);
		}
	}
	
	public function load_data(){
		$this->load->model("email_model");
		$post = $this->input->post();
		
		//Config---
		$conf['page'] = $_POST['page'];
		$conf['cur_page'] = $conf['page'];
		$conf['page'] -= 1;
		$conf['per_page'] = 10;
		$conf['previous_btn'] = true;
		$conf['next_btn'] = true;
		$conf['first_btn'] = true;
		$conf['last_btn'] = true;
		$conf['start'] = $conf['page'] * $conf['per_page'];
		//---------
		
		$main_condition = "";
		$sec_condition = "";
			
		//Search---
		if($this->input->post('S_SUBJECT') != ""){
			if($main_condition != "") $main_condition .= " AND ";
			$main_condition .= "SUBJECT LIKE '%".$post['S_SUBJECT']."%'";
		}
		
		if($this->input->post('S_SUCCESS') != ""){
			if($main_condition != "") $main_condition .= " AND ";
			$main_condition .= "SUCCESS = '".$post['S_SUCCESS']."'";
		}
		//---------
		
		$data['list'] = $this->email_model->get_data($main_condition, $sec_condition, $conf['per_page'], $conf['start'], "WEB_EMAIL_LOG.ID DESC", "", "", "");
		$data['count'] = $this->email_model->data_count($main_condition, $sec_condition);
		$data['config'] = $conf;
		
		//Set parameter to avoid Filter Data lost---
		$this->session->set_flashdata($this->router->fetch_class().'_page_filter', $_SERVER['QUERY_STRING']);
		//------------------------------------------
		
		$this->load->view($this->router->fetch_class()."/load_data", $data);
	}
}