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

class Portfolio extends CI_Controller {

    public function __construct(){
        parent::__construct();

        if(!isset($_SESSION['login'])){
        	
        	redirect('Welcome');
        }
    }


    public function insertPortfolio(){
        $this->load->view('user/add-portfolio');
    }

    public function submitPortfolio(){

        $this->form_validation->set_rules('title', 'Title', 'required');

        if ($this->form_validation->run() == FALSE){

			$this->session->set_flashdata('error', 'Set Title, and Picture');

        	redirect('user/Portfolio/insertPortfolio');
                
        }
        else{
            $config['upload_path']          = './image/';
            $config['allowed_types']        = 'gif|jpg|png';

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

            if ($this->upload->do_upload('picture_path')){

                $image_path = $this->upload->data();
                $image = "image/".$image_path["file_name"];

                $data = array(
                    'title' => $this->input->post('title'),
                    'link' => $this->input->post('link'),
                    'picture_path' => $image,
                    'type' => $this->input->post('type')
                );

                $this->load->model('portfolio_model');
                $res = $this->portfolio_model->insert_portfolio($data);

                if($res == TRUE){
                    $this->session->set_flashdata('success', 'Successfully Inserted');
                    redirect("user/Portfolio/insertPortfolio");
                }
                else{
                    $this->session->set_flashdata('error', 'Something went wrong');
                    redirect("user/Portfolio/insertPortfolio");
                }
            }
        }

    }

    public function showPortfolio(){
        
        $this->load->model('portfolio_model');
        $data['results'] = $this->portfolio_model->show_portfolio();
        $this->load->view('user/list-portfolio', $data);
    }


    public function deletePortfolio(){
        $data = array(
            'logoid'=> $this->input->get('logoid'),
            'picture_path' => $this->input->get('picture_path')
        );
        $this->load->model('portfolio_model');
        $res = $this->portfolio_model->delete_portfolio($data);
        
        if($res == TRUE){
            $this->session->set_flashdata('success', 'Successfully Deleted');
            redirect("user/Portfolio/showPortfolio");
        }
        else{
            $this->session->set_flashdata('error', 'Something went wrong');
            redirect("user/Portfolio/showPortfolio");
        }
    }

}