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

class Logo extends CI_Controller {

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

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


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

    public function submitLogo(){

        $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/Logo/insertLogo');
                
        }
        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'),
                    'picture_path' => $image,
                    'type' => $this->input->post('type')
                );

                $this->load->model('logo_model');
                $res = $this->logo_model->insert_logo($data);

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

    }

    public function showLogo(){
        $this->load->model('logo_model');
        $data['results'] = $this->logo_model->show_logo();
        $this->load->view('user/list-logo', $data);
    }

    public function deleteLogo(){
        $data = array(
            'logoid'=> $this->input->get('logoid'),
            'picture_path' => $this->input->get('picture_path')
        );
        $this->load->model('logo_model');
        $res = $this->logo_model->delete_logo($data);
        
        if($res == TRUE){
            $this->session->set_flashdata('success', 'Successfully Deleted');
            redirect("user/Logo/showLogo");
        }
        else{
            $this->session->set_flashdata('error', 'Something went wrong');
            redirect("user/Logo/showLogo");
        }
    }    

}