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

class Api extends CI_Controller {

	var $data=array();
	var $mailresponse=array();
	var $Fileconfig = array();
	
	function __construct(){
		
		parent::__construct();
		$this->load->library(array('form_validation'));

		$this->mailresponse['status']=0;
		$this->mailresponse['msg']='';
		
		 
	}
	
	function sendusermail($param=array()){
		$from=$param['fromemail'];
		$to=$param['toemail'];
		$subject=$param['subject'];
		$message=$param['message'];
		
		 $headers = 'From:'.$from. "\r\n" .
                 'Reply-To:'.$from. "\r\n" .
                 'X-Mailer: PHP/' . phpversion();

        if(mail($to, $subject, $message, $headers)){
            return 1;
        }else{
            return 0;
        }  
	}

	function contactus($ids=NULL){
		$param=array();
		$param1=array();
		
		$this->form_validation->set_rules('form_name', 'Name', 'required|trim');
		$this->form_validation->set_rules('form_email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('form_phone', 'Phone Number', 'required');

		if($this->form_validation->run()==FALSE)
		{
			$mailresponse['status']=0;
			$mailresponse['msg']= '<div class="alert alert-warning">'.validation_errors().'</div>';
		}else
		{
			$name =$this->input->post('form_name');
			$phone=$this->input->post('form_phone');
			$email=$this->input->post('form_email');
			$city=$this->input->post('form_city');
			$country=$this->input->post('form_country');
			$subject='Contact Us';
			$message =$this->input->post('form_message');
			
			$param['fromemail']="info@nitigupta.com";
			$param['toemail']=$email;
			$param['subject']="Thank you! We Have Received your Query";
			$param['message']= "Thank you! We Have Received your Query";
			
			$param1['fromemail']="info@nitigupta.com";
			$param1['toemail']="info@nitigupta.com";
			$param1['subject']="Contact Us Request";
			$param1['message']= "<strong>Name: ".$name."<br/>
								<strong>Email: ".$email."<br/>
								<strong>Contact: ".$phone." <br/>
								<strong>City: ".$city." <br/>
								<strong>Country: ".$country." <br/>
								<strong>Subject: ".$subject." <br/>
								<strong>Message: ".$message."";
			
			if($this->sendusermail($param)){
				$mailresponse['status']=1;
				$mailresponse['msg']= '<div class="alert alert-success">Message Received Succesfully</div>';
				$this->sendusermail($param1);
			}else{
				$mailresponse['status']=0;
				$mailresponse['msg']= '<div class="alert alert-danger">Something Went Wrong...</div>';
			} 
		}
			echo json_encode($mailresponse);
	}
	
	public function autocreatemerchant(){
		$this->load->model('admin/Merchants_model');
		
		$jsonArray = json_decode(file_get_contents('php://input'),true); 
		//print_r($jsonArray);
		
		$error=false;
		
		$rsp = array();
		$data=array();
		$data['accountId']=$jsonArray['AccountId'];
		$data['storename']=$jsonArray['Storename'];
		$data['name']=$jsonArray['Contactperson'];
		$data['email']=$jsonArray['Email'];
		$data['contact']=$jsonArray['Contact'];
		$data['whatsappno']=$jsonArray['Whatsappno'];
		$data['icon']=$jsonArray['Logo'];
		$data['password']=$jsonArray['Password'];
		$data['username']=$jsonArray['Username'];
		
		if($data['accountId'] =='' || $data['storename'] == "" || $data['username'] == '' || $data['password']==''){
			$error=true;
			$rsp['msg']='FIELDS ERROR';
		}elseif(checkunique('accountId',array('accountId'=>$data['accountId'],'username'=>$data['username'],'password'=>$data['password']),'merchant')){
			$error=true;
			$rsp['msg']='ALREADY EXISTS';
			
			$data['authlink'] = md5($data['username']."_".$data['password']."_".$data['accountId']);		
			$data['updatedon']=date('Y-m-d H:i:s');
			$id = $this->Merchants_model->update_merchant($data);
			$rsp['msg']='ACCOUNT UPDATED';
			
		}
		
		if(!$error){
			$data['authlink'] = md5($data['username']."_".$data['password']."_".$data['accountId']);
			$data['status']=1;
			$data['createdon']=date('Y-m-d H:i:s');				
			$data['updatedon']=date('Y-m-d H:i:s');
			$id = $this->Merchants_model->set_merchant($data,'');
			$rsp['msg']='ACCOUNT CREATED';
		}
		
		print_r(json_encode($rsp));
	}
	
}
?>