<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Common extends My_Controller {
	public function __construct()
	{
		parent::__construct();
		$data = array();
		$this->adminid = $this->session->userdata('adminid');
		$this->load->model("common_model","common");
	}
	public function index()
	{
		echo "common";
	}
	public function rmdirRecursive($dir)
	{
	    foreach(scandir($dir) as $file) {
	        if ('.' === $file || '..' === $file) continue;
	        if (is_dir("$dir/$file")) $this->rmdirRecursive("$dir/$file");
	        else unlink("$dir/$file");
	    }
	    rmdir($dir);
	}
	public function ajaxDelete()
	{
		$table = $_POST['table'];
		unset($_POST['table']);
		$fields = $this->common->getField($table,$_POST);
		$key = key($fields);
		//print_r($fields);print_r($key);die;
		$this->common->deleteData($table,$fields);
		if($this->db->affected_rows())
		{
			$dir = $_POST['image'];
			//echo $dir;
			if(is_dir($dir))
			{
				$this->rmdirRecursive($dir);
				//unlink($dir);
			}
			if(is_file($dir)){
				unlink($dir);
			}
			echo "1";
		}else{
			echo "0";
		}die;
	}

	public function setCurrency()
	{
		if($_POST['currency']){
			$this->session->set_userdata('currency',$_POST['currency']);
			echo 1;
		}else{
			echo 0;
		}
	}

	public function getCartProductInfo()
    {
    	$uc = $this->common->getData('user_cart',array('send_mail'=>0));
    	if($uc){
    		foreach ($uc as $key => $value) {
		        $cart = $this->common->getData('user_cart_product',array('userid'=>$value['userid']));
		        if($cart){
                    $user = $this->common->getsData("user",array("profileid"=>$value['userid']),'email');
                    // $user['email'] = "vinod.starwebindia@gmail.com";
                    $data['cart'] = $cart;
                    $subject = 'Still thinking about it?';
                    $message = $this->load->view('email_temp/cart-product-notify',$data,true);

                    $res = $this->common->sendMailBySendGrid($user['email'],'','ben@websentials.com.sg','Aqua Staging',$subject,$message);
                    echo $res;
                    $this->common->updateData('user_cart',array('send_mail'=>1),array('id'=>$value['id']));
		        }else{
		            echo 0;
		        }
    		}
    	}
    }

	public function sendBirthdayDiscountMail($value='')
	{
		$user = $this->common->getData('user',array('verify'=>1,'dob'=>date('Y-m-d')));
		if($user){
			foreach ($user as $key => $value) {
				$data['name'] = $value['name'];
				$email = $value['email'];
				$html = $this->load->view('email_temp/birthday-email',$data,true);
				// print_r ($html); die;
				$this->email->from('ben@websentials.com.sg','Aqua Staging');
				$this->email->to($email);
				$this->email->subject('Happy Birthday - '.$value['name']);
				$this->email->message($html);
				$this->email->send();
			}
		}
	}

	public function checkWebhookResponse()
	{
		$res = $this->common->getData('omise_webhook',array('date(date)'=>"2022-03-29"));
		echo "<pre>";
		print_r ($res);
		echo "</pre>";
	}


	public function omiseWebhookResponseTest()
	{
		$response = file_get_contents('php://input');
		$this->common->insertData('omise_webhook',array('data'=>$response,'date'=>date('Y-m-d H:i:s')));
		$result = json_decode($response,true);
		$charge_id = $result['data']['id'];
		$txn_id = $result['data']['transaction'];
		$this->common->updateData('orders',array('payment_status'=>'Complete'),array('charge_id'=>$charge_id,'txn_id'=>$txn_id));
	}

	public function omiseWebhookResponseLive()
	{
		$response = file_get_contents('php://input');
		$this->common->insertData('omise_webhook',array('data'=>$response,'date'=>date('Y-m-d H:i:s')));
		$result = json_decode($response,true);
		$charge_id = $result['data']['id'];
		$txn_id = $result['data']['transaction'];
		$this->common->updateData('orders',array('payment_status'=>'Complete'),array('charge_id'=>$charge_id,'txn_id'=>$txn_id));
	}

	public function updateProducts($value='')
	{
		$p = $this->common->getData('products');
		foreach ($p as $key => $value) {
			$slug = url_title(strtolower($value['product_name']));
			$sku = 'AQ'.$value['pid'];
			$this->common->updateData('products',array('product_slug'=>$slug,'product_sku'=>$sku),array('pid'=>$value['pid']));
		}
	}



	public function hitPayWebhook()
	{
		$response = file_get_contents('php://input');
		$this->common->insertData('hitpay_webhook',array('data'=>$response,'date'=>date('Y-m-d H:i:s')));
		parse_str($response,$result);
		$payment_id = $result['payment_id'];
		$orderid = $result['reference_number'];
		$payment_request_id = $result['payment_request_id'];
		if($result['status']=='completed'){
			$this->common->updateData('orders',array('payment_status'=>'Complete','payment_id'=>$payment_id),array('orderid'=>$orderid,'txn_id'=>$payment_request_id));
		}
	}
	public function hitPayWebhookPackage()
	{
		$response = file_get_contents('php://input');
		$this->common->insertData('hitpay_webhook',array('data'=>$response,'date'=>date('Y-m-d H:i:s')));
		parse_str($response,$result);
		$payment_id = $result['payment_id'];
		$orderid = $result['reference_number'];
		$payment_request_id = $result['payment_request_id'];
		if($result['status']=='completed'){
			$this->common->updateData('user_package_order',array('payment_status'=>'Complete','payment_id'=>$payment_id),array('orderid'=>$orderid,'txn_id'=>$payment_request_id));
		}
	}

}