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

class Add_product extends CI_Controller {

	
	public function __construct()
        {
                parent::__construct();
                $this->load->library('session');
                $this->load->model('admin/Add_product_model');
                if(!isset($_SESSION['admin'])){redirect('admin/Login');}
        }

	

	public function index()
	{
		$this->load->view('admin/header');
		$this->load->view('admin/add-product');
		$this->load->view('admin/footer');
	}

	public function insert()
	{
		// 
		$data = $this->Add_product_model->db_insert();
		if($data == 1){
			$this->session->set_flashdata("success"," Congratulations Your Product Added Successfully");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_product"));
		}elseif($data == 2){
			$this->session->set_flashdata("error"," ERRO ERROr.");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_product"));
		}elseif($data == 3){
			$this->session->set_flashdata("same"," slug already exist.");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_product"));
		}
	}
public function fetch_sub(){
    	if($this->input->post('id')){
    			echo $this->Add_product_model->fetch_sub($this->input->post('id'));
    	}
    }

    public function checkbox()
    {
        $id = $this->input->post('id');
        $data = array(
            'status' => 1
        );
        $in = $this->db->where('id', $id);
        $in = $this->db->update('product', $data);
        if($in){
            echo "Successfully Added to Feature Products";
        }else{
            echo "Error";
        }
    }

    public function uncheckbox()
    {
        $id = $this->input->post('id');
        $data = array(
            'status' => 0
        );
        $in = $this->db->where('id', $id);
        $in = $this->db->update('product', $data);
        if($in){
            echo "Successfully Removed from Feature Products";
        }else{
            echo "Error";
        }
    }

	public function view(){
		$data['view_all'] = $this->Add_product_model->get_all();
		$this->load->view('admin/header');
		$this->load->view('admin/product-table', $data);
		$this->load->view('admin/footer');
	}

	public function edit($id){
		$data['data'] = $this->Add_product_model->select($id);
		$data['id'] = $id;
		$this->load->view('admin/header');
		$this->load->view('admin/add-product', $data);
		$this->load->view('admin/footer');
	}

	public function update()
	{
		$data = $this->Add_product_model->update($_POST);
		if($data == 1){
			$this->session->set_flashdata("same"," This Product Already Exist, :) Your Other Data is Updated.");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_product/view"));
		}elseif($data == 2){
			$this->session->set_flashdata("error"," Something is Wrong");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_product/view"));
		}elseif ($data == 3) {
			$this->session->set_flashdata("success"," Your All Date Sucessfuly Updated");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_product/view"));
		}else{
			$this->session->set_flashdata("error"," Something is Wrong");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_product/view"));
		}
	}

	public function delete($id)
	{
		$data = $this->Add_product_model->delete($id);
		if($data == 1){
			$this->session->set_flashdata("success"," Category Deleted Done");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_product/view"));
		}else{
			$this->session->set_flashdata("error"," Something is Wrong");
				$this->output->set_header('refresh:1; url='.site_url("admin/Add_product/view"));
		}
	}
}
