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

class DeleteAccount extends CI_Controller
{
	public function __construct()
	{ 
		parent:: __construct();
		$this->load->model('common_model');
		$this->load->model('users_model');
		$this->load->library('Utility');
		if(get_cookie('user_id'))
		{
			$this->session->set_userdata('user_id',get_cookie('user_id'));
		}
		if(!$this->session->userdata('user_id'))
		{
			redirect('home/login');
		}
		
	}
	public function index(){
		$userId=$this->session->userdata('user_id');
		$userdetails = $this->common_model->getAllRow('feu_userdetails',array('user_id'=>$userId));
		$useremaildata = $this->common_model->getAllRow('usr_users',array('id'=>$userId));
		$userImagesData = $this->common_model->getAll('sys_files',array('user_id'=>$userId));
		$inputPassword  = $this->common_model->generateHash($this->utility->info_cleanQuery($this->input->post('deletepassword')));
		$inputEmail = $this->utility->info_cleanQuery($this->input->post('deleteemail'));
		//echo $inputPassword.'=='.$userdetails['password'].'|'.$inputEmail.'=='.$userdetails['email']; die;
		if($inputPassword == $useremaildata['password'] && $inputEmail == $useremaildata['email'])
		{
			/*print_r($userId);
			print_r($userdetails);
			print_r($useremaildata);
			print_r($userImagesData);
			die;*/
			if(!empty($userdetails))
			{
				//$to = 'sayan@digitalaptech.com';
				$to = $useremaildata['email'];
				$sub = getAdminLang('Your_account_has_been_deleted',$userdetails['userlanguage']);
				$body= '<p>'.getAdminLang('profile_castleevents_has_been_deleted',$userdetails['userlanguage']).'</p>';
				$body.= '<p>'.getAdminLang('All_content_information_as_well',$userdetails['userlanguage']).'</p>';
				$body.= '<p>'.getAdminLang('We_hope_you_had_a_good_time',$userdetails['userlanguage']).'</p>';
				
				
				$bodys=' <table cellspacing="0" cellpadding="0" border="0" align="center" bgcolor="#22222c" width="100%" style="max-width: 600px;">
	                    <tr>
	                       <td>
	                          <table cellspacing="0" cellpadding="0" border="0" width="100%">
	                             <tr>
	                                <td align="center" style="padding: 40px; font-family: sans-serif; font-size: 15px; mso-height-rule: exactly; line-height: 20px; color: #b3975b;">
	                                   <h2>Dear <span style="text-transform:capitalize">'.$userdetails['username'].'</span> </h2>
	                                </td>
	                             </tr>
	                             <tr>
	                                <td style="font-family: sans-serif; color: #fff; padding: 0px 40px 40px; font-size: 16px; line-height: 30px;">
	                                   '.$body.'
	                                </td>
	                             </tr>
	                             <tr>
	                             	<td align="center" style="font-family: sans-serif; color: rgb(179, 151, 91); padding: 20px 40px; border-top: 1px solid rgb(79, 79, 79); background: rgb(19, 18, 24) none repeat scroll 0px 0px;">
	                             		'.getAdminLang('Best_regards',$userdetails['userlanguage']).' <br>
	                             		<p style="margin:5px 0 0">'.getAdminLang('Your_team_of_castleevents',$userdetails['userlanguage']).'</p> 
	                             	</td>
	                             </tr>
	                          </table>
	                       </td>
	                    </tr>
	                </table>';
				$this->utility->sendCIMail($to,$sub,$bodys);
			}
			$to = 'alorenc@me.com';
			$sub = 'User delete his/her account';
			$body= '<p>'.$userdetails['username'].' deleted his/her account</p>';
			$body.= '<p>User details are followed:</P>';
			$body.= '<p>Username: '.$userdetails['username'].'</P>';
			$body.= '<p>Email: '.$useremaildata['email'].'</P>';
			$bodys=' <table cellspacing="0" cellpadding="0" border="0" align="center" bgcolor="#22222c" width="100%" style="max-width: 600px;">
			            <tr>
			               <td>
			                  <table cellspacing="0" cellpadding="0" border="0" width="100%">
			                     <tr>
			                        <td style="font-family: sans-serif; color: #fff; padding: 0px 40px 40px; font-size: 16px; line-height: 30px;">
			                           '.$body.'
			                        </td>
			                     </tr>
			                  </table>
			               </td>
			            </tr>
			        </table>';
			$this->utility->sendOrderMail($to,$sub,$bodys);
			$arr = array(
						'username'=>$userdetails['username'],
						'email'=>$useremaildata['email'],
						'created_date'=>$userdetails['created'],
						'delete_date'=>strtotime($currentDate)
					);
			$inst = $this->common_model->insertData('deleted_users',$arr);
			$comments = $this->common_model->getAll('cmt_comments',array('alias_path'=>'community/profil/'.$userdetails['alias']));
			$this->common_model->deleteData('usr_users',array('id'=>$userId));
			//$this->common_model->deleteData('event_guest',array('username'=>$userdetails['username']));
			$this->common_model->deleteData('feu_userdetails',array('user_id'=>$userId));
			if(count($comments)>0)
			{
				foreach ($comments as $value) {
					$this->common_model->deleteData('cmt_comments',array('id'=>$value['id']));
				}
			}
			$this->common_model->deleteData('feu_addresses',array('user_id'=>$userId));
			$this->common_model->deleteData('feu_auth_requests',array('user_id'=>$userId));
			$this->common_model->deleteData('feu_profilevisits',array('from_user_id'=>$userId));
			$this->common_model->deleteData('feu_profilevisits',array('to_user_id'=>$userId));
			$this->common_model->deleteData('feu_relationships',array('user_id_1'=>$userId));
			$this->common_model->deleteData('feu_relationships',array('user_id_2'=>$userId));
			$this->common_model->deleteData('feu_sexual_preference',array('user_id'=>$userId));
			$this->common_model->deleteData('feu_user_discounts',array('user_id'=>$userId));
			$this->common_model->deleteData('feu_user_steps',array('user_id'=>$userId));
			$this->common_model->deleteData('like_likes',array('user_id'=>$userId));
			$this->common_model->deleteData('like_likes',array('identifier'=>'profile:'.$userId));
			$this->common_model->deleteData('msg_conversations_users',array('user_id'=>$userId));
			$this->common_model->deleteData('msg_messages',array('from_user_id'=>$userId));
			$this->common_model->deleteData('newsletter_couple_lady',array('couple'=>$userId));
			$this->common_model->deleteData('newsletter_couple_lady',array('lady'=>$userId));
			$this->common_model->deleteData('newsletter_couple_lady',array('gentleman'=>$userId));
			$this->common_model->deleteData('notification',array('from_id'=>$userId));
			$this->common_model->deleteData('notification',array('to_id'=>$userId));
			
			$this->common_model->deleteData('sys_files',array('user_id'=>$userId));
			$this->common_model->deleteData('ugal_images',array('user_id'=>$userId));
			$this->common_model->deleteData('ugal_images_user_privacy',array('from_user_id'=>$userId));
			$this->common_model->deleteData('ugal_images_user_privacy',array('to_user_id'=>$userId));
			$this->common_model->deleteData('usr_log',array('user_id'=>$userId));
			$this->common_model->deleteData('usr_modules',array('user_id'=>$userId));
			$this->common_model->deleteData('usr_phones',array('user_id'=>$userId));
			$this->common_model->deleteData('usr_recurring_payment',array('user_id'=>$userId));
			$this->common_model->deleteData('usr_roles_users',array('user_id'=>$userId));
			if(count($userImagesData)>0)
			{
				foreach ($userImagesData as $value) {
					$filename = $value['filename'].'.'.$value['extension'];
					
					$filename_blur = $value['filename'].'_blur.'.$value['extension'];
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/'.$filename);
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/80/'.$filename);
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/160/'.$filename);
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/320/'.$filename);
					
					if($value['temp_name']!='')
					{
						$temp_name = $value['temp_name'].'.'.$value['extension'];
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/'.$temp_name);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/80/'.$temp_name);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/160/'.$temp_name);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/320/'.$temp_name);
					}

					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/'.$filename_blur);
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/250/'.$filename);
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/500/'.$filename);
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/750/'.$filename);
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/1000/'.$filename);

					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/250/'.$filename_blur);
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/500/'.$filename_blur);
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/750/'.$filename_blur);
					@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/1000/'.$filename_blur);
				}
			}
			$apiKey = "9a1d414f170b904492f76c4b4aaad59c-us14";
			$listId = "4b705bab4e";
			$batch_operations = array();
			$url = "https://us14.api.mailchimp.com/3.0/lists/".$listId."/members/".md5(strtolower($useremaildata['email']));
			$operation = array(
		            'email_address'=>$useremaildata['email'],
		            
		            );
			$request_encoded = json_encode($operation);
		    $curl = curl_init($url);
		    curl_setopt($curl, CURLOPT_USERPWD, 'user:' . $apiKey);    
		    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
		    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//raw output
		    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
		    curl_setopt($curl, CURLOPT_POSTFIELDS, $request_encoded); 
			$result = curl_exec($curl);
			//var_dump($result); die;
			$apiKey = "9a1d414f170b904492f76c4b4aaad59c-us14";
			$listIdEng = "a8efb2baef";
			$batch_operations = array();
			$urlEng = "https://us14.api.mailchimp.com/3.0/lists/".$listIdEng."/members/".md5(strtolower($useremaildata['email']));
			$operationEng = array(
		            'email_address'=>$useremaildata['email'],
		            
		            );
			$request_encodedEng = json_encode($operationEng);
		    $curl = curl_init($urlEng);
		    curl_setopt($curl, CURLOPT_USERPWD, 'user:' . $apiKey);    
		    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
		    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//raw output
		    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
		    curl_setopt($curl, CURLOPT_POSTFIELDS, $request_encodedEng); 
			$result = curl_exec($curl);

			$this->session->unset_userdata('user_id');
			$cookie = array(
			        'name'   => 'user_id',
			        'value'  => '',
			        'expire' => 0,
			        );
			 
			delete_cookie($cookie);
			redirect(base_url());
			//redirect(base_url('settings'));
		} else {
			$this->session->set_userdata('deleteAccountErr',1);
			redirect(base_url('settings'));
		}
	}

	
}
