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

class Import_products extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
         if (! $this->session->userdata ( 'user_logged_in' ) || !company_url($this->session->userdata ('company_slag'))) {
            redirect ( 'login', 'refresh' );
        }
        $this->load->vars ( array (
                'global' => 'Available to all views',
                'header' => 'common/header',
                'left' => 'common/left',
                'footer' => 'common/footer' 
        ) ); 
        $this->load->model('Color_model');
        $this->load->model('Product_model');
        $this->load->library('Fileupload');
    } 
    
    public function index(){
          $data['import_list'] = $this->Color_model->getAllImportData();			
          $data['vendor_list'] = $this->Product_model->get_vendor();
//       echo "<pre>";
//        print_r($data);
//        exit();		
		
        $this->load->view('imports_product',$data);
    }
    
   
    public function addimports(){
        $data=$this->input->post(); 
        $name = explode(".", $_FILES['file']['name']);
        if($name[1]=='csv'){
         if ($_FILES['file']['size'] > 0) {
                    $file_name = "file";                   
                    $path = 'uploads/imports';
                     $data['file'] = $this->fileupload->upload_file($file_name, $path, $name[0]);
                }
              $data['file_name']=$_FILES['file']['name'];
        
//        if($data['id']!=''){
//             $rest=$this->Color_model->Edit("color",$data,'id',$data['id']);
//            echo $rest;
//            exit;
//             
//        }else{
            unset($data['id']);           
            $rest=$this->Color_model->Add("import_products",$data);
            redirect(base_url().'Import_products');
            
//        }
        }else{           
            redirect(base_url().'Import_products');
        }
    }
    public function deluploads(){
        $token=$this->input->post('token');
        $upload_row=$this->Color_model->GetARecordWithWhereRowDESC("import_products",array('id'=>$token));
        if(!empty($token)){
            $rest=$this->Color_model->DelWhere('import_products',array('id'=>$token));         
            if($rest){
                unlink("$upload_row->file");
            }
        }
        
        redirect(base_url().'Import_products');
    }
}