<?php
if(!defined('BASEPATH')) exit('Hacking Attempt : Will Kill You Do Not Worry..!!');

class Contacts extends MY_Controller
{

	var $model ='Contacts_model';
	
	public function __construct(){
		parent::__construct();
		if($this->session->userdata('isLogin') == FALSE ){ redirect(base_url('admin/login/login_form'));}	
		$this->load->model('admin/'.$this->model); 
		$this->load->model('admin/Merchants_model');
		$config['upload_path']          = 'uploads/merchants/';
		$config['allowed_types']        = 'gif|jpg|png|jpeg';
		$config['max_size'] 			= 12000;
        $config['max_width'] 			= 13500;
        $config['max_height'] 			= 13500;
		$this->upload->initialize($config);
	}
	
	public function index(){
			
	}

/*##########################################################*/
/*################ Contacts List ####################*/
/*##########################################################*/

	
	
	public function lists(){
			$data['pagetitle']='Contacts List';
			if(isset($_POST['status'])){$data['status'] = $_POST['status'];}else{$data['status']='-1';}
			
			$data['response'] = $this->Contacts_model->get_contacts_list($data['status']);
			$data['updatelink']=base_url('admin/contacts/listspublish');
			$data['deletelink']=base_url('admin/contacts/listsdelete');
			$this->adminview('contacts/lists',$data);
	}
	
	public function listspublish($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'title',
											'label' => 'List Title',
											'rules' => 'required'
									)
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				
				if(!$formerror){
					
					$id = $_POST['id'];
					$data['title']=$_POST['title'];
					$data['description']=$_POST['description'];
					$data['merchantId']=$_POST['merchantId'];
					if(isset($_POST['status'])){$data['status']=1;}else{$data['status'] = 0;}
					if($id ==''){$data['createdon']=date('Y-m-d H:i:s');}					
					$data['updatedon']=date('Y-m-d H:i:s');
					$id = $this->Contacts_model->set_contacts_list($data,$id);
					
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Contact List Updated Successfully...</div>');
					redirect(base_url('admin/contacts/lists'));
				}else{
					$data['title']=$_POST['title'];
					$data['description']=$_POST['description'];
					if(@$_POST['status'] == 'on'){$data['status']=1;}else{$data['status'] = 0;}
				}
			
			}
			
			if($para >0){
					
					$response = $this->Contacts_model->get_single_contacts_list($para);
					$data['id']=$response[0]->listId;
					$data['title']=$response[0]->title;
					$data['description']=$response[0]->description;
					$data['merchantId']=$response[0]->merchantId;
					$data['status']=$response[0]->status;
			}
			$data['pagetitle']='Update Contacts list';	
			$data['merchants'] = $this->Merchants_model->get_merchant(1);
			$this->adminview('contacts/listspublish',$data);
	}

	public function groupdelete($para = null){
		$this->Contacts_model->delete_merchant_group($para);
		$this->session->set_flashdata('page_message', '<div class="alert alert-danger text-center"> Selected Entry Deleted Successfully</div>');
		redirect(base_url('admin/merchants/groups'));
	}
/*#######################################################################*/

	
/*##########################################################*/
/*################ Contacts ####################*/
/*##########################################################*/	

public function contacts(){
			$data['pagetitle']='Contacts';
			if(isset($_POST['status'])){$data['status'] = $_POST['status'];}else{$data['status']='-1';}
			
			$data['response'] = $this->Contacts_model->get_contacts($data['status']);
			$data['updatelink']=base_url('admin/contacts/contactspublish');
			$data['deletelink']=base_url('admin/contacts/contactsdelete');
			$this->adminview('contacts/contacts',$data);
	}
	
	public function publishcontactsjs(){
		$js='';
		$js.="$(document).ready(function(){";
 
		$js.= "$( window ).on( 'load', function(){
					var merchantId=0;
					$.ajax({
						url:baseurl+'helper/fetch_merchant',
						method:'POST',
						data:{merchantId:merchantId},
						success:function(data)
					{
					 $('#getmerchant').html(data);
					}
				   });
				 });"; 
		$js.="$('#getmerchant').change(function(){
				  var merchant_id = $('#getmerchant').val();
				  if(merchant_id != '')
				  {
				   $.ajax({
					url:baseurl+'helper/fetch_contact_list',
					method:'POST',
					data:{merchant_id:merchant_id},
					success:function(data)
					{
					 $('#getcontactlist').html(data);
					}
				   });
				  }
				  else
				  {
				   $('#getcontactlist').html('<option value=\"\">Select Merchant List</option>');
				  }
				 });
				";				
		$js.="});";				 
		return $js;		 
	}
	
	public function contactspublish($para=null){
		
			$formerror=0;
			
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'merchantId',
											'label' => 'Merchant',
											'rules' => 'required'
									),
									array(
											'field' => 'listId',
											'label' => 'Merchant List',
											'rules' => 'required'
									),
									array(
											'field' => 'first_name',
											'label' => 'First Name',
											'rules' => 'required'
									),
									array(
											'field' => 'contact',
											'label' => 'Contact No.',
											'rules' => 'required'
									)
							);

				$this->form_validation->set_rules($rules);
								
				if($_POST['id'] ==''){
					if(checkunique('contact',array('contact'=>$_POST['contact'],'merchantId'=>$_POST['merchantId']),'contacts')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> Contact No Already In Database...</div>');
							$formerror = 1; 
					}
				}
				
				if(!$formerror){
					
					$id = $_POST['id'];
				
					$data['merchantId']=$_POST['merchantId'];
					$data['listId']=$_POST['listId'];
					$data['first_name']=$_POST['first_name'];
					$data['last_name']=$_POST['last_name'];
					$data['contact']=$_POST['contact'];
					$data['email']=$_POST['email'];
					$data['source']=$_POST['source'];
					$data['country']=$_POST['country'];
					$data['state']=$_POST['state'];
					$data['city']=$_POST['city'];
					$data['postcode']=$_POST['postcode'];
					if(isset($_POST['notify_sms'])){$data['notify_sms']=1;}else{$data['notify_sms'] = 0;}
					if(isset($_POST['notify_whatsapp'])){$data['notify_whatsapp']=1;}else{$data['notify_whatsapp'] = 0;}
					if(isset($_POST['status'])){$data['status']=1;}else{$data['status'] = 1;}	
					if($id ==''){$data['createdon']=date('Y-m-d H:i:s');}					
					$data['updatedon']=date('Y-m-d H:i:s');
					
					$id = $this->Contacts_model->set_contacts($data,$id);
					
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> Contact Details Updated Succesfully...</div>');
					redirect(base_url('admin/contacts/contacts'));
				}else{
					$data['merchantId']=$_POST['merchantId'];
					$data['listId']=$_POST['listId'];
					$data['first_name']=$_POST['first_name'];
					$data['last_name']=$_POST['last_name'];
					$data['contact']=$_POST['contact'];
					$data['email']=$_POST['email'];
					$data['source']=$_POST['source'];
					$data['country']=$_POST['country'];
					$data['state']=$_POST['state'];
					$data['city']=$_POST['city'];
					$data['postcode']=$_POST['postcode'];
					if(isset($_POST['notify_sms'])){$data['notify_sms']=1;}else{$data['notify_sms'] = 0;}
					if(isset($_POST['notify_whatsapp'])){$data['notify_whatsapp']=1;}else{$data['notify_whatsapp'] = 0;}
					if(isset($_POST['status'])){$data['status']=1;}else{$data['status'] = 1;}	
				}
			
			}
			
			if($para >0){
					
					$response = $this->Contacts_model->get_single_contacts($para);
					$data['id']=$response[0]->contactId;
					$data['merchantId']=$response[0]->merchantId;
					$data['listId']=$response[0]->listId;
					$data['first_name']=$response[0]->first_name;
					$data['last_name']=$response[0]->last_name;
					$data['contact']=$response[0]->contact;
					$data['email']=$response[0]->email;
					$data['source']=$response[0]->source;
					$data['country']=$response[0]->country;
					$data['state']=$response[0]->state;
					$data['city']=$response[0]->city;
					$data['postcode']=$response[0]->postcode;
					$data['notify_sms'] =$response[0]->notify_sms;
					$data['notify_whatsapp'] = $response[0]->notify_whatsapp;
					$data['status'] = $response[0]->status;				
			}
			$data['pagetitle']='Update Contacts';
			$footer=$header=array();
			$footer['pagejs']=$this->publishcontactsjs();
			$this->adminview('contacts/contactspublish',$data,$header,$footer);
	}

	public function contactsdelete($para = null){
		$this->Contacts_model->delete_contacts($para);
		$this->session->set_flashdata('page_message', '<div class="alert alert-danger text-center"> Selected Entry Deleted Successfully</div>');
		redirect(base_url('admin/contacts/contacts'));
	}

}	

?>