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

class Listing extends CI_Controller {

	function __construct(){
		//session_start();
		parent::__construct();
		if(!isset($_SESSION['logged'])){
			redirect('admin');
		}
		$this->load->library("pagination");
	}
	public function index()
	{
		$this->load->model('categories_model');
		$this->load->model('postings_model');
		$config = array();
		$config["base_url"] = base_url() . "admin/listing";
		$config["total_rows"] = $this->categories_model->getPostingsCount();
		$config["per_page"] = 20;
		$config["uri_segment"] = 3;
		$config["num_links"] = 5;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$listings=$this->categories_model->getPostings('',$config["per_page"], $page);
		$data["links"] = $this->pagination->create_links();
		$data['listings']=$listings;
		$data['main_content']='admin/listing/index';
		$data['title']='Manage Listings :: Website Control panel';
		$data['bodyid']='listings';
		$this->load->view('admintemplate',$data);
	}
	public function add()
	{
		
		if(isset($_POST) && !empty($_POST)){
			$title=$this->input->post('title');
			$description=$this->input->post('description');
			$category_id=$this->input->post('category');
			$price=$this->input->post('price');
			$original_price=$this->input->post('original_price');
			$user_id= $_SESSION['user_id'];
			$data = array(
			   'title' => $title ,
			   'description' => $description ,
			   'user_id' => $user_id,
			   'category_id' => $category_id,
			   'price' => $price,
		           'original_price' => $original_price,
			   'published' => 1,
			   'views' => 0,
			   'dt_created' => date('Y:m:d h:i:s'),
			   'dt_modified'=> date('Y:m:d h:i:s')
			);

			$result=$this->db->insert('postings', $data); 

			if($result){
				$posting_id= $this->db->insert_id();
				$config['upload_path'] = dirname($_SERVER['SCRIPT_FILENAME']).'/assets/adimages/';
				$config['allowed_types'] = 'gif|jpg|png|jpeg';
				
				$this->load->library('upload', $config);
				$field_name = "filename";

				if ( ! $this->upload->do_upload($field_name))
				{
					$error = array('error' => $this->upload->display_errors());
						//echo '<div id="status">error</div>';
						//echo '<div id="message">'.$error['error'].'</div>';
				}
				else
				{
					$data = array('upload_data' => $this->upload->data());
						//echo '<div id="status">success</div>';
						//then output your message
						//echo '<div id="message">Your file success message</div>';
						//print_r($data);
						$dataarray = array(
						   'posting_id'=>$posting_id,
						   'image_url' => $data['upload_data']['file_name'] ,
						   'is_main'=>1
						);
						$result=$this->db->insert('postingimages', $dataarray); 

					//$this->load->view('upload_success', $data);
				}

					redirect('admin/listing');
			}
		}else{
		$this->load->model('categories_model');
		$categories=$this->categories_model->getParentCategories();
		$data['categories']=$categories;
		$data['main_content']='admin/listing/add';
		$data['title']='Post Your ad';
		$data['bodyid']='postad';
		$this->load->view('admintemplate',$data);
		}
	}
}

?>
