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

class Donate extends CI_Controller {

	function __construct() 

	{
       // error_reporting(0);
        parent::__construct();
        $this->load->model('CommonModel');
        $this->load->helper('Security');
        $this->load->library('instamojo');
    }
	public function index()
	{
		$data['content'] = 'donate';
		$this->load->view('layout_home', $data);
	}

	public function paynow(){
	date_default_timezone_set('Asia/Kolkata');
  $input_data = $this->input->post();
  $input_data = $this->security->xss_clean($input_data);
		if ($this->session->userdata('user_id')) {

      $edit_data['name'] = $input_data['donate_name'];
        $edit_data['email'] = strtolower($input_data['donate_email']);
        $edit_data['mobile'] = $input_data['donate_mobile'];
      $this->db->where('c_id',$this->session->userdata('user_id'));
      $this->db->update('customer',$edit_data);
      $this->session->set_userdata('user_id', $this->session->userdata('user_id'));            
      $this->session->set_userdata('name', $input_data['donate_name']);
      $this->session->set_userdata('email', strtolower($input_data['donate_email']));
      $this->session->set_userdata('mobile', $input_data['donate_mobile']);
      $this->session->set_userdata('login_session', true);
      }
      else
      {
      $query = $this->db->query("select * from `customer` where   `email`='".$input_data['email']."'"); 
  $row=$query->result_array();

  if ($row) {
      $edit_data['name'] = $input_data['donate_name'];
      $edit_data['email'] = strtolower($input_data['donate_email']);
      $edit_data['mobile'] = $input_data['donate_mobile'];
      $this->db->where('c_id',$row['0']['c_id']);
      $this->db->update('customer',$edit_data);
      $this->session->set_userdata('user_id', $row['0']['c_id']);            
      $this->session->set_userdata('name', $input_data['donate_name']);
      $this->session->set_userdata('email', strtolower($input_data['donate_email']));
      $this->session->set_userdata('mobile', $input_data['donate_mobile']);
      $this->session->set_userdata('login_session', true);
  
  }
  else {
  
        
  
  $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
  $password = substr( str_shuffle( $chars ), 0, 8 );
  $customer_data['name'] = $input_data['donate_name'];
  $customer_data['email'] = strtolower($input_data['donate_email']);
  $customer_data['pass'] = base64_encode($password);
  $customer_data['mobile'] = $input_data['donate_mobile'];
  $customer_data['reg_date'] = date('Y-m-d');
  $customer_data['veryfied'] = '0'; 
  $this->db->insert('customer',$customer_data);
  $customer_id=$this->db->insert_id();

  $this->session->set_userdata('user_id', $customer_id);            
  $this->session->set_userdata('name', $input_data['donate_name']);
  $this->session->set_userdata('email', strtolower($input_data['donate_email']));
  $this->session->set_userdata('mobile', $input_data['donate_mobile']);
  $this->session->set_userdata('login_session', true);
  

  

  }

      }

		
		$date = date('Y-m-d H:i:s', time());
		
        
        $insert_data['c_id'] = $this->session->userdata('user_id');
        $insert_data['temp_id'] = $input_data['donate_purpose'];
        $insert_data['donation_type'] = $input_data['donate_type'];
        $insert_data['amount'] = $input_data['totalAmount']/100;
        $insert_data['buyer_name'] = $input_data['donate_name'];
        $insert_data['phone'] = $input_data['donate_mobile'];
        $insert_data['email'] = $input_data['donate_email'];
        $insert_data['transction_id'] = $input_data['razorpay_payment_id'];
        $insert_data['date'] = $date;
        $this->db->insert('donate',$insert_data);

        $donate_id = $this->db->insert_id();
        $edit_ord['invoice'] = 'TDM'.date('Ymd').'00'.$donate_id;
        $this->db->where('id',$donate_id);
        $this->db->update('donate',$edit_ord);
        echo 'success';
        
	}
	public function thanks()
	{
		echo 'thanks';
	}





}
