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

class Shipping_cost extends CI_Controller {
	// Session Authoriesed
	public function __construct() {
		parent::__construct();
		
		$admin = $this->session->userdata('admin');
		$column['id'] = $admin['id'];
		$table = 'tbl_user';
		
	    // loading common model
	    $this->load->model('Common_model');
		$result = $this->Common_model->Result($column,$table);
		if($result['password'] == $admin['password']){
		    $login = true;
		} else {
		    $login = false;
		}
		
		if (empty($admin) || $login==false) {
			$this->session->set_flashdata('msg','Your Session Has Been Expired');
			redirect(base_url().'master/login/index');
		}
		
	   // loading common model
	    $this->load->model('Common_model');
	}

	public function index()
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Shipping Cost';
		
		$table = 'tbl_shipping_cost_all';
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['shipping_cost'] = $result;
		
        // we are loading admin dashboard
		$this->load->view('master/shipping_cost/list',$data);	
	}


	public function edit($id)
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Shipping Cost';
		
		$table1 = 'tbl_shipping_cost_all';
		$column1['sca_id'] = $id;
        // we are getting results from common modal
		$result1 = $this->Common_model->Result($column1,$table1);
		
		$data['shipping_cost'] = $result1;
		
        // we are loading admin dashboard
		$this->load->view('master/shipping_cost/edit',$data);	
	}

	public function create()
	{
        // we are setting data for get site data
		$column['id'] = '1';
		$table = 'tbl_settings';
		
        // we are getting results from common modal
		$result = $this->Common_model->Result($column,$table);
		
        // we are setting result in data array as $data
		$data['data'] = $result;
		$data['page'] = 'Shipping Cost';
		
		
        // we are getting results from common modal
		$result = $this->Common_model->Results($table);
		
		$data['shipping_cost'] = $result;
		
        // we are loading admin dashboard
		$this->load->view('master/shipping_cost/create',$data);	
	}
	
	public function add(){
        $data = [];
        foreach ($_POST as $key => $value) {
            if($key == 'table'){$table = $value;}
            if($key == 'page'){$page = $value;}
            if (empty($value)) {
                $this->session->set_flashdata('error',''.strtoupper("$key").' Can not be Empty.');
			    redirect(base_url('master/'.$page));
			    exit();
            }
            if($key != 'table' && $key != 'page'){
                $data[$key] = $value;
            }
        }
        
        $where = "amount = '".$data['amount']."' OR min_amt = '".$data['min_amt']."' OR max_amt = '".$data['max_amt']."' OR min_amt = '".$data['max_amt']."'  OR max_amt = '".$data['min_amt']."' ";
        $result1 = $this->Common_model->Count_Where($where,$table);
        if($result1==true){
            $this->session->set_flashdata('error','Shipping Cost already exists.');
		    redirect(base_url('master/'.$page));
		    exit();
        }
        
        if($data['min_amt'] > $data['max_amt']){
            $this->session->set_flashdata('error','Minimum Amount can not be greater than Maximum Amount.');
            redirect(base_url('master/'.$page));
            exit();
        }
        
        if($data['min_amt'] == $data['max_amt']){
            $this->session->set_flashdata('error','Minimum Amount and Maximum Amount can not be same.');
            redirect(base_url('master/'.$page));
            exit();
        }
            
        
        // we are updating data from tbl_p_type
        $result  = $this->Common_model->Create($data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! Data is Submited Successfully.');
		    redirect(base_url('master/'.$page));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong .');
		    redirect(base_url('master/'.$page));
		    exit();
        }
           
	}

	
	public function edit_content($id){
	    
        $data = [];
        
        foreach ($_POST as $key => $value) {
            
            if($key == 'table'){$table = $value;}
            
            if($key == 'page'){$page = $value;}
            
            if($key != 'status'){
                if (empty($value)) {
                    $this->session->set_flashdata('error',''.strtoupper("$key").' Can not be Empty.');
    			    redirect(base_url('master/'.$page));
    			    exit();
                }
            }
            
            if($key != 'table' && $key != 'page' && $key != 'column'){
                $data[$key] = $value;
            }
            
            if($key=='column'){
                $column_name = $value;
            }
            
        }
        
            $column['id'] = $id;
        
        if(!empty($id) && !empty($column_name)){
            $column[$column_name] = $id;
            unset($column['id']);
        }
        
        if($data['min_amt'] > $data['max_amt']){
            $this->session->set_flashdata('error','Minimum Amount can not be greater than Maximum Amount.');
            redirect(base_url('master/'.$page));
            exit();
        }
        
        if($data['min_amt'] == $data['max_amt']){
            $this->session->set_flashdata('error','Minimum Amount and Maximum Amount can not be same.');
            redirect(base_url('master/'.$page));
            exit();
        }
                
                
        // we are updating data from tbl_p_type
        $result  = $this->Common_model->Edit($column,$data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! Data is Updated Successfully.');
		    redirect(base_url('master/'.$page));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong .');
		    redirect(base_url('master/'.$page));
		    exit();
        }
           
	}
	
}

?>