<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Coupon extends CI_Controller {
	function __construct()
    {
          parent::__construct();
          $this->load->database();
		   $this->load->model('Coupon_model');
          $this->load->helper(array('form', 'url','string'));
          $this->load->library('form_validation');
          $this->load->library('session');
          $this->load->model('comman_model');
    }
	
	public function list()
	{
		$data['coupon'] = $this->Coupon_model->get_coupons();
		$this->template->template_render('coupons_add',$data);
	}
	public function Coupon_delete()
	{
		$cu_id = $this->uri->segment(3);
		$returndata = $this->Coupon_model->Coupon_delete($cu_id);
		if($returndata) {
			$this->session->set_flashdata('successmessage', 'Coupon deleted successfully..');
			redirect('Coupon/list');
		} else {
			$this->session->set_flashdata('warningmessage',  'Something went wrong..Try again');
		    redirect('Coupon/list');
		}
	}
      	
	public function addcoupon()
	{
		$lat = $this->db->select('c_mobile')->from('customers')->get()->result_array();
		$token = $this->db->select('device_token')->from('customers')->get()->result_array();

		// print_r($token);
		// exit();
		
	$data = array('cu_name'=>$this->input->post('cu_name'),'cu_type'=>$this->input->post('cu_type'),'cu_description' => $this->input->post('cu_description'),'discount' =>$this->input->post('discount'),'cu_start_date' =>$this->input->post('cu_start_date'),'cu_end_date' =>$this->input->post('cu_end_date'),'cu_coupon_code' =>$this->input->post('cu_name').'-'.$this->input->post('discount'));
		$response = $this->db->insert('coupons',$data);
		$apiKey = ('NTkzMjRkMzI3MzMyNDI2ZTczNjI1MDY3NzM1MTZmNzM=');
                // Message details
		foreach($lat as $row){
			
        			$row1= $row['c_mobile'];
  
        		 
    					$name = $this->input->post('cu_name').'-'.$this->input->post('discount');
    					
    					$numbers = $row['c_mobile'];
    					$sender = urlencode("TDP");
    					// $message = rawurlencode('Dear User, your OTP for Volta App Login is '.$OTP.'.Do not share your OTP with anyone .');  
    					  $message= rawurlencode('Dear '.$name.', ManaTDP wishes you a very Happy Birthday! May the year ahead be full of joy and good health.');  
                // Prepare data for POST request
    					$data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender"=> $sender, "message" => $message);
                // Send the POST request with cURL
    					$ch = curl_init('https://api.textlocal.in/send/');
    					curl_setopt($ch, CURLOPT_POST, true);
    					curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    					$response = curl_exec($ch);
    					curl_close($ch);
    				}
foreach($token as $row){
			
        			
    				$device_id=$row['device_token']; 
						$title='Volta Coupons';
						$message='We have added Latest Coupons For You';
						$this->comman_model->fcm_notification($device_id,$title,$message);
					}
		if($response) {
			$this->session->set_flashdata('successmessage', 'Coupons added successfully..');
		    redirect('Coupon/list');
		} else
		{
			$this->session->set_flashdata('warningmessage', 'Something went wrong..Try again');
		    redirect('Coupon/list');
		}
	}
		public function Coupon_edit()
	{
		
		$id = $this->uri->segment(3);
		$data['coupon'] = $this->Coupon_model->get_coupon($id);
		$this->template->template_render('coupons_edit',$data);
	}
	public function updatecoupon()
	{
		$testxss = xssclean($_POST);
		if($testxss){
			$response = $this->Coupon_model->edit_coupon($this->input->post());

				if($response) {
					$this->session->set_flashdata('successmessage', ' Coupon updated successfully..');
				    redirect('Coupon/list');
				} else
				{
					$this->session->set_flashdata('warningmessage', 'Something went wrong..Try again');
				    redirect('Coupon/list');
				}
		} else {
			$this->session->set_flashdata('warningmessage', 'Error! Your input are not allowed.Please try again');
			redirect('Coupon/list');
		}
	}
	
}
