<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email_distribution extends MY_Controller{
	public function index(){
		$this->load->view($this->router->fetch_class()."/index");
	}
	
	public function update(){
		$this->load->model("email_distribution_model");
		$post = $this->input->post();
		
		//To prevent loss---
		$this->fm->keep_all_page_filter();
		//------------------
		
//		print_r($post);
//		return;
		
		if(isset($post['ID'])){
			foreach($post['ID'] as $x=>$id){
				unset($data);
				if(isset($post['REPORT'][$x])) $data['REPORT'] = $post['REPORT'][$x];
				if(isset($post['EMAIL'][$x])) $data['EMAIL'] = $post['EMAIL'][$x];
				
				if($id == ""){
					$this->email_distribution_model->add($data);
				}
				else{
					$data['ID'] = $id;
					$this->email_distribution_model->edit($data);
				}
			}
		}
		
		if(isset($post['DEL_ID'])){
			foreach($post['DEL_ID'] as $id){
				$this->email_distribution_model->sql_delete($id);
			}
		}
		
		$this->session->set_flashdata('success', "Update Successful!");
		
		redirect(base_url($this->router->fetch_class()."?".$this->session->flashdata($this->router->fetch_class().'_page_filter')));
	}
	
	public function load_data(){
		$this->load->model("email_distribution_model");
		$post = $this->input->post();
		
//		print_r($post);

		$data = array();
		
		$main_condition = "";
		$sec_condition = "";
		
		$data['data_array'] = $this->email_distribution_model->get_data();
		
		//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);
	}
}