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

class Checkout extends CI_Controller
{
    // Session Authoriesed
    public function __construct()
    {
        parent::__construct();
        
        $customer = $this->session->userdata('customer');
        if (empty($customer)) {
            $this->session->set_flashdata('error', 'Please login to continue.');
            redirect(base_url('login'));
        }
        $column['cust_id'] = $customer['cust_id'];
        $table             = 'tbl_customer';
        
        // loading common model
        $this->load->model('Common_model');
        $result = $this->Common_model->Result($column, $table);
        if ($result['cust_password'] == $customer['cust_password']) {
            $login = true;
        } else {
            $login = false;
        }
        
        if (empty($customer) || $login == false) {
            $this->session->set_flashdata('error', 'Please login to continue.');
            redirect(base_url('login'));
        }
    }
    
    public function index()
    {
        
        // loading common model
        $this->load->model('Common_model');
        
        $column['id'] = '1';
        $table        = 'tbl_settings';
        
        // we are getting results from common modal
        $result = $this->Common_model->Result($column, $table);
        
        
         $i = 1;
        $result111 = $this->Common_model->Results('tbl_language');
        foreach ($result111 as $row)
        {
            define('LANG_VALUE_' . $i, $row['lang_value']);
            $i++;
        }
        
        
        $columntop_cities['status'] = '1';
        $tabletop_cities            = 'tbl_top_cities';
        // we are getting results from common modal
        $resulttop_cities           = $this->Common_model->ResultsWhere2($columntop_cities, $tabletop_cities);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_top_category';
        // we are getting results from common modal
        $top_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_mid_category';
        // we are getting results from common modal
        $mid_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_end_category';
        // we are getting results from common modal
        $end_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        
        $column3['status']      = '1';
        $column3['status_home'] = '1';
        $table3                 = 'tbl_top_category';
        // we are getting results from common modal
        $top_category           = $this->Common_model->ResultsWhere2($column3, $table3);
        
        $column32['status']    = '1';
        $table32               = 'tbl_mid_category';
        // we are getting results from common modal
        $data['mid_category2'] = $this->Common_model->ResultsWhere2($column32, $table32);
        
        $column33['status']    = '1';
        $table33               = 'tbl_end_category';
        // we are getting results from common modal
        $data['end_category2'] = $this->Common_model->ResultsWhere2($column33, $table33);
        
        $column31['status'] = 1;
        $table31            = 'tbl_top_cities';
        // we are getting results from common modal
        $top_cities         = $this->Common_model->ResultsWhere2($column31, $table31);
        
        
        $column31['status'] = 1;
        $table31            = 'tbl_social';
        // we are getting results from common modal
        $social             = $this->Common_model->ResultsWhere2($column31, $table31);
        
        
        /**************************************************/
        $customer             = $this->session->userdata('customer');
        $column123['cust_id'] = $customer['cust_id'];
        $table123             = 'tbl_customer';
        /**************************************************/
        // loading common model
        $this->load->model('Common_model');
        $result123        = $this->Common_model->Result($column123, $table123);
        $data['customer'] = $result123;
        /**************************************************/
        
        // we are setting result in data array as $data
        $data['data']         = $result;
        $data['top_cities']   = $top_cities;
        $data['top_category'] = $top_category;
        $data['mid_category'] = $mid_category;
        $data['end_category'] = $end_category;
        $data['social']       = $social;
        $data['page']         = 'checkout';
        
        // we are loading customer dashboard
        $this->load->view('website/checkout', $data);
    }
    
    
    
    public function saveaddress()
    {
      
      
    $cust_datetime = date('Y-m-d h:i:s');
                                                    
  
  $query11 = $this->db->where('user_id',$_SESSION['customer']['cust_id']);
  $query11 = $this->db->get('tbl_delivery_address');
	if ($query11->num_rows() > 0)  
        {
            
       $data=[
        'name'=>strip_tags($_POST['SH_name']),
        'phone'=>strip_tags($_POST['SH_phone']),
        'state'=>strip_tags($_POST['SH_state']),
        'district'=>strip_tags($_POST['SH_district']),
        'pin_code'=>strip_tags($_POST['SH_pin_code']),
        'address'=>strip_tags($_POST['SH_address']),
        'addon'=>$cust_datetime,
        'updated'=>$cust_datetime,
        'status'=>1
        ];
        
        
         $_SESSION['SH_name']=$_POST['SH_name'];
      $_SESSION['SH_address']=$_POST['SH_address'];
      $_SESSION['SH_district']=$_POST['SH_district'];
      $_SESSION['SH_state']=$_POST['SH_state'];
      $s_pin=$_SESSION['pin_code'];
      $_SESSION['SH_phone']=$_POST['SH_phone']; 
      
       $this->db->where('user_id',$_SESSION['customer']['cust_id']);
       $this->db->update('tbl_delivery_address',$data);
      
        }
        else
        {
    
    
    
    $data=[
        'user_id'=>$_SESSION['customer']['cust_id'],
        'name'=>strip_tags($_POST['SH_name']),
        'phone'=>strip_tags($_POST['SH_phone']),
        'state'=>strip_tags($_POST['SH_state']),
        'district'=>strip_tags($_POST['SH_district']),
        'pin_code'=>strip_tags($_POST['SH_pin_code']),
        'address'=>strip_tags($_POST['SH_address']),
        'addon'=>$cust_datetime,
        'updated'=>$cust_datetime,
        'status'=>1
        ];
        
       
            
      $_SESSION['SH_name']=$_POST['SH_name'];
      $_SESSION['SH_address']=$_POST['SH_address'];
      $_SESSION['SH_district']=$_POST['SH_district'];
      $_SESSION['SH_state']=$_POST['SH_state'];
      $s_pin=$_SESSION['pin_code'];
      $_SESSION['SH_phone']=$_POST['SH_phone']; 
      
      unset($_SESSION['add_sess']);
       $this->db->insert('tbl_delivery_address',$data);
        }
        
    redirect(base_url('checkout'));
    }
    
    public function test(){
        $this->load->view('website/checkout1');
    }
    
}