<?php
defined('BASEPATH') OR exit('No direct script access allowed');
Class Quotation Extends MY_Controller{
	function __construct()
	{
		parent::__construct();	
		$this->load->model('admin_api/Quotation_model');
		$this->config->load('my_constants');
	}

	public function add_quotation()
	{
		$data['active_quotation'] = 'open';
		$data['title'] = 'quotation List';
		$data['header_title'] = 'quotation';
		$data['quotation_info'] = $this->Quotation_model->get_quotation_list();
		$data['product_info'] = $this->Quotation_model->get_product_list();
		//print_r($data['package_info']);exit();
		$data['main_content'] = 'admin/Quotation/add_quotation';
        $this->load->view('admin/includes/template', $data);
	}	

	public function view_tender($tender_id)
	{
		//echo $tender_id;
		$data['active_quotation'] = 'open';
		$data['title'] = 'quotation List';
		$data['header_title'] = 'quotation';
		$data['quotation_info'] = $this->Quotation_model->get_tender_details($tender_id);
	//	print_r($data['quotation_info']);
		//print_r($data['package_info']);exit();
		//$data['main_content'] = 'admin/Quotation/add_quotation';
      //  $this->load->view('admin/includes/template', $data);
	  			$this->load->view('admin/Quotation/view_tender', $data);

	}	
	
	public function tender_list()
	{
		$data['active_tender'] = 'open';
		$data['title'] = 'Tender List';
		$data['header_title'] = 'Tender';
		$data['tender_info'] = $this->Quotation_model->get_tender_list();
		//print_r($data['package_info']);exit();
		$data['main_content'] = 'admin/Quotation/tender_list';
        $this->load->view('admin/includes/template', $data);
	}

	
	
	public function save_quotation()
	{
		$post_data = $this->input->post();
	$sql="select max(tender_id) as tender_id from quotation";
				$r=$this->db->query($sql);
				$rr=$r->row('tender_id');
				$tender_id=$rr+1;
				$tender_no=$post_data['tender_no'];
		
	$img=$_FILES["je"]["name"];
		$img2 = str_replace(' ','_',$img);
$extension=end(explode(".", $img2));
//$img=$tender_no .".".$extension;
$img4=$tender_no .".".$extension;
$img = str_replace('/','_',$img4);
	$upload_dir = "tender/$tender_id";


	if(!is_dir($upload_dir))
	{
		mkdir($upload_dir, 0755);
		chmod($upload_dir, 0755);
	}

		$path="http://pctest.spectracloud.net/spectra/tender/$tender_id/$img";

	move_uploaded_file($_FILES["je"]["tmp_name"], "$upload_dir/" . $img);
	
	
			$query=$this->Quotation_model->save_quotation($post_data,$path);
			if($query){
				$this->session->set_flashdata('success_message', 'quotation Added successfully!');

				redirect('admin/Quotation/add_quotation','refresh');
			}else{
				$this->session->set_flashdata('failure_message', 'You are fail to Add quotation!');
				redirect('admin/Quotation/add_quotation','refresh');
			}
	}
	
	
	

	public function delete_quotation($id=NULL)
	{
	
	//	$post_data = $this->input->post();
			$query=$this->Quotation_model->delete_quotation($id);
			if($query){
				$this->session->set_flashdata('success_message', 'Master Product Deleted successfully!');

				redirect('admin/Quotation/add_quotation','refresh');
			}else{
				$this->session->set_flashdata('failure_message', 'You are fail to Deleted Master Product!');
				redirect('admin/Quotation/add_quotation','refresh');
			}
	}
	

	
	
public function auo_product3()
		{
			$db = get_instance()->db->conn_id;
			$type = $_POST['type'];
			$name = $_POST['name_startsWith'];
			$category = $_POST["category"];
		
			$product = $_POST["product"];
			//echo $category
		//	echo $_POST["id"];
	$query = "SELECT DISTINCT component,price,component_no from component  where IsActive=0 and 
	category='$category' and master_product='$product' and UPPER($type) LIKE '".strtoupper($name)."%'";
		
		
	$result = mysqli_query($db, $query);
	$data = array();
	while ($row = mysqli_fetch_assoc($result)) {
		$component=$row['component'];
		$price=$row['price'];
		$component_no=$row['component_no'];
		
	$name = $component.'|'.$price.'|'.$component_no;
		
		array_push($data, $name);
	}	
	echo json_encode($data);
		}

	
	
	
	public function edit_tender($tender_id)
	{
	    
		$data['active_quotation'] = 'open';
		$data['title'] = 'quotation edit';
		$data['header_title'] = 'quotation';
		$data['quotation_info'] = $this->Quotation_model->get_quotation_list();
		$data['product_info'] = $this->Quotation_model->get_product_list();
		$data['tender_info'] = $this->Quotation_model->get_tender_info($tender_id);
		//print_r($data['package_info']);exit();
		$data['main_content'] = 'admin/Quotation/edit_quotation';
        $this->load->view('admin/includes/template', $data);
	}	
	
	public function update_quotation()
	{
		$post_data = $this->input->post();
			$query=$this->Quotation_model->update_quotation($post_data);
			if($query){
				$this->session->set_flashdata('success_message', 'Quotation Updated successfully!');

				redirect('admin/Quotation/tender_list','refresh');
			}else{
				$this->session->set_flashdata('failure_message', 'You are fail to Updated Quotation!');
				redirect('admin/Quotation/edit_tender','refresh');
			}
	}
	
	public function save_ask_price()
	{
		$post_data = $this->input->post();
			$query=$this->Quotation_model->save_ask_price($post_data);
			if($query){
				$this->session->set_flashdata('success_message', 'Quotation Updated successfully!');

				redirect('admin/Quotation/tender_list','refresh');
			}else{
				$this->session->set_flashdata('failure_message', 'You are fail to Updated Quotation!');
				redirect('admin/Quotation/tender_list','refresh');
			}
	}
	
	public function save_status()
	{
		$post_data = $this->input->post();
			$query=$this->Quotation_model->save_status($post_data);
			if($query){
				$this->session->set_flashdata('success_message', 'Status Updated successfully!');

				redirect('admin/Quotation/tender_list','refresh');
			}else{
				$this->session->set_flashdata('failure_message', 'You are fail to Updated Status!');
				redirect('admin/Quotation/tender_list','refresh');
			}
	}	
	
	
}?>