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

class Products extends MX_Controller
{
	function __construct()
   	{
		parent::__construct();
        $this->load->model('product_model');
        $this->load->model('product_image_model');
        $this->load->model('product_category_model');
        $this->load->model('lang_model');

        checklogin();
   	}

    public function index()
    {
        $data['lists'] = $this->product_model->with_category()->get_all();
        $this->template->write_view('content', 'products',$data);
        $this->template->write_view('level_script', 'products/listscript');
        $this->template->add_css('assets/css/layout-datatables.css');
        $this->template->render();
    }
    public function lists()
    {
        $data['lists'] = $this->product_model->with_category()->get_all();

//        pre($data);exit;
        $this->template->write_view('content', 'products',$data);
        $this->template->write_view('level_script', 'products/listscript');
        $this->template->add_css('assets/css/layout-datatables.css');
        $this->template->render();
    }
    public function form($item_id='')
    {
        $data['item'] = $this->product_model->get($item_id);
        $data['category'] = $this->product_category_model->as_dropdown('title_en')->get_all();
        $data['langs'] = $this->lang_model->where("is_active",'Y')->get_all();

        $this->template->write_view('content', 'productsform',$data);
        $this->template->write_view('level_script', 'products/formscript',$data);
        $this->template->add_css('assets/plugins/dropzone/css/dropzone.css');
        $this->template->add_js('assets/js/progress.js');
        $this->template->render();
    }
    public function add(){

        $data ="" ;
        foreach(@$_POST as $key =>$value) {
            if ($value != "") {
                $data[$key] = $value;
            }
        }

        unset($data['is_ajax']);

        $product_id = '';
        if(isset($data) && $data !="" && @$data['id'] =="")
        {
            $product_id=$this->product_model->insert($data) ;
        }
        else if(  @$data['id'])
            $product_id = $this->product_model->update($data,$data['id']);


        if($product_id)
        {
            $product = $this->product_model->get($data['id']);

//            pre($product); exit;

            $data_path['thumbnail1_image'] = $product->thumbnail1_image;
            $data_path['thumbnail2_image'] = $product->thumbnail2_image;

            if(isset($_FILES) && @$_FILES['thumbnail1_image']['name'] !=""){
                $path = './upload/images/products/'.$product->id.'/';

                if (!is_dir($path)) {
                    mkdir($path, 0777, TRUE);
                }
                $file_name = time().'_thumbnail1_image_'.@$_FILES['thumbnail1_image']['name'];
                $data_path['thumbnail1_image'] ="products/".$product->id.'/'.$file_name;
                $upload = $this->upload_image('thumbnail1_image',$path,$file_name);

                if(is_array($upload))
                {
                    unlink(realpath('./upload/images/'.$product->thumbnail1_image));
                }
            }
            if(isset($_FILES) && @$_FILES['thumbnail2_image']['name'] !=""){
                $path = './upload/images/products/'.$product->id.'/';

                if (!is_dir($path)) {
                    mkdir($path, 0777, TRUE);
                }
                $file_name = time().'_thumbnail2_image_'.@$_FILES['thumbnail2_image']['name'];
                $data_path['thumbnail2_image'] ="products/".$product->id.'/'.$file_name;
                $upload = $this->upload_image('thumbnail2_image',$path,$file_name);

                if(is_array($upload))
                {
                    //Delete Previous Files
                    unlink(realpath('./upload/images/'.$product->thumbnail2_image));
                }
            }
            $this->product_model->update($data_path,$product->id);

        }
    }
    public function dropzone_load($product_id='')
    {
        $imagesByProduct = $this->product_image_model->where('product_id',$product_id)->get_all();

        $items =array();
        foreach ($imagesByProduct as $key=>$value)
        {
            $items[] = array(   "name"=>$value->filename,
                                "size"=> $value->filesize,
                                "path"=> $value->path);
        }
        echo json_encode($items);

    }
    public function dropzone_add($product_id='')
    {
//        $product = $this->dbmod->getByID($product_id,"product");
        $imagebyname = $this->product_image_model
            ->where(array('filename'=>$_FILES['file']['name'],'product_id'=>$product_id))
            ->get_all();


        if(!$imagebyname)
        {
            if($product_id!='')
            {
                if(isset($_FILES) && @$_FILES['file']['name'] !=""){


                    $path = './upload/images/products/'.$product_id.'/detail_image/';

                    if (!is_dir($path)) {
                        mkdir($path, 0777, TRUE);
                    }
                    $file_name = @$_FILES['file']['name'];
                    $image_path ="products/".$product_id.'/detail_image/'.$file_name;
                    $upload = $this->upload_image('file',$path,$file_name);

                    if(is_array($upload))
                    {
                        $data['path'] = $image_path;
                        $data['filename'] = $file_name;
                        $data['product_id'] = $product_id;
                        $data['filesize'] = $_FILES['file']['size'];

                        if(isset($data) && $data !="" && @$data['product_id'] !="")
                            $this->product_image_model->insert($data) ;
                    }
                }
            }
            else
            {
                http_response_code (406);
            }
        }
        else
        {
            http_response_code (409);
        }
    }

    public function dropzone_remove($product_id='')
    {

        $imagebyname = $this->product_image_model->where(array('filename'=>$_FILES['file']['name'],'product_id'=>$product_id))->get();
        if($imagebyname)
        {
            if(isset($imagebyname) && @$imagebyname->id !="")
                $deleted = $this->dbmod->delete($imagebyname->id) ;

            if($deleted)
            {
                //Delete Files
                unlink(realpath('./upload/images/'.$imagebyname->path));
            }
        }
    }
    function upload_image($image='',$path='', $new_name='') {

        // define parameters
        $config['upload_path'] = $path;
        $config['allowed_types'] = 'jpg|png|jpeg';
        $config['file_name'] = $new_name;

        $this -> load -> library('upload');
        $this->upload->initialize($config);

        // upload the image
        if ($this -> upload -> do_upload($image)) {
            return $this -> upload -> data();
        } else {
            print_r($this -> upload -> display_errors());
            return FALSE;
        }
    }
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */