<?php

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

class Products extends CI_Controller 
{
	 function __construct() 
	 {
        parent::__construct();
        $this->load->database();
        $this->load->model('User_Model');
        $this->load->model('Common');
         $x = $this->session->userdata('logged');
         if ($x['email'] == '') 
         {
                return redirect('Welcome/index', 'refresh');
          }
    }

    public function index()
    {
        $page =(isset($_GET['page'])) ? $_GET['page'] : 0;
        $query=$this->db->select('products.*')->from('products')->order_by('id','desc')->limit(20,($page))->get();
        $num_rows=$this->db->order_by('id', 'desc')->get('products')->num_rows();
        $data['products']=$query->result_array();
        $data['links']=$this->pagi->pagination1('admin/products',$num_rows,20);
        $this->load->view('products/index',$data);
    }

    public function add()
    {
        if ($this->input->server('REQUEST_METHOD') == 'POST') 
        {
         $this->load->helper(array('form'));
         $this->load->library('form_validation');
         $this->form_validation->set_rules('name', 'Name', 'required'); 
         $this->form_validation->set_rules('price', 'Price', 'required'); 
                  $this->form_validation->set_rules('category', 'Category', 'required'); 
         $this->form_validation->set_rules('subcategory', 'Subcategory', 'required'); 
            if ($this->form_validation->run() == FALSE) 
             { 
                 $this->load->view('products/add');
             } 
             else 
             { 
                $journalName = str_replace(' ', '_', $_FILES['image']['name']);
                $config['file_name'] = time() . $journalName;
                $config['upload_path'] = './uploads/products/';
                $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG|jpeg';
                $this->upload->initialize($config);
                $this->upload->do_upload('image');
                $_POST['image'] = $config['file_name'];
                if($this->db->insert('products',$_POST))
                {
                    $this->session->set_flashdata('msg', 'products Successfully Added');
                    return redirect(base_url('admin/products'),'refresh');
                }
                else
                {
                    $this->session->set_flashdata('err', 'Please try Again After SomeTimes');
                    return redirect(base_url('admin/products/add'),'refresh');
                }
             } 
        }
        else
        {
            $this->load->view('products/add');
        }
    }

    public function edit($id='')
    {
        if ($this->input->server('REQUEST_METHOD') == 'POST') 
        {
        $this->load->helper(array('form'));
         $this->load->library('form_validation');
         $this->form_validation->set_rules('name', 'Name', 'required'); 
         $this->form_validation->set_rules('price', 'Price', 'required'); 
                  $this->form_validation->set_rules('category', 'Category', 'required'); 
         $this->form_validation->set_rules('subcategory', 'Subcategory', 'required');
            if ($this->form_validation->run() == FALSE) 
             { 
                 $data['products']=$this->db->where('id',$id)->get('products')->row_array();
                 $this->load->view('products/edit',$data);
             } 
             else 
             { 
                if($_FILES['image']['name']!='')
                {
                     $journalName = str_replace(' ', '_', $_FILES['image']['name']);
                $config['file_name'] = time() . $journalName;
                $config['upload_path'] = './uploads/products/';
                $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG|jpeg';
                $this->upload->initialize($config);
                $this->upload->do_upload('image');
                $_POST['image'] = $config['file_name'];
                }
                if($this->db->where('id',$id)->update('products',$_POST))
                {
                    $this->session->set_flashdata('msg', 'products Successfully Added');
                    return redirect(base_url('admin/products'),'refresh');
                }
                else
                {
                    $this->session->set_flashdata('err', 'Please try Again After SomeTimes');
                    return redirect(base_url('admin/products/edit/'.$id),'refresh');
                }
             }
        }
        else
        {
            $data['products']=$this->db->where('id',$id)->get('products')->row_array();
            $this->load->view('products/edit',$data);
        }
    }

    public function view($id='')
    {
         $data['products']=$this->db->where('id',$id)->get('products')->row_array();
         $this->load->view('products/view',$data);
    }

    public function delete($id='')
    {
        if($this->db->where('id',$id)->delete('products'))
        {
            $this->session->set_flashdata('msg', 'Success Deleted products');
        }
        else
        {
            $this->session->set_flashdata('err', 'Please try After Sometimes...');
        }
        return redirect(base_url('admin/products'),'refresh');
    }

    public function getCategory()
    {
        $data=$this->db->where('category',$_POST['cat'])->where('status','Active')->get('category')->result_array();
        echo json_encode($data);
    }

}
