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

class Software extends CI_Controller {

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

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


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

    public function editSoftware(){

        $data = array(
            'softwareid'=> $this->input->get('softwareid')
        );

        $this->load->model('software_model');
        $data['edit'] = "editsoftware";
        $data['results'] = $this->software_model->single_software($data);
        $this->load->view('user/add-software', $data);

    }

    public function submitSoftware(){

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

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

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

        	redirect('user/Software/insertSoftware');
                
        }
        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'),
                    'description' => $this->input->post('description'),
                    'picture_path' => $image
                );

                $this->load->model('software_model');
                $res = $this->software_model->insert_software($data);

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

    }

    public function updateSoftware(){

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('description', 'Description', 'required');
        // if (empty($_FILES['picture_path']['name']))
        // {
        //     $this->form_validation->set_rules('picture_path', 'Picture', 'required');
        // }

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

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

        	redirect('user/Software/insertSoftware');
                
        }
        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(
                    'softwareid' => $this->input->post('softwareid'),
                    'title' => $this->input->post('title'),
                    'description' => $this->input->post('description')
                );

                $this->load->model('software_model');
                $res = $this->software_model->update_software($data);

                if($res == TRUE){
                    $this->session->set_flashdata('success', 'Successfully Updated');
                    redirect("user/Software/editSoftware?softwareid=".$this->input->post('softwareid'));
                }
                else{
                    $this->session->set_flashdata('error', 'Something went wrong');
                    redirect("user/Software/editSoftware?softwareid=".$this->input->post('softwareid'));
                }
            // }
        }

    }

    public function showSoftware(){
        $this->load->model('software_model');
        $data['results'] = $this->software_model->show_software();
        $this->load->view('user/list-software', $data);
    }

    public function deleteSoftware(){
        $data = array(
            'softwareid'=> $this->input->get('softwareid'),
            'picture_path' => $this->input->get('picture_path')
        );
        $this->load->model('software_model');
        $res = $this->software_model->delete_software($data);
        
        if($res == TRUE){
            $this->session->set_flashdata('success', 'Successfully Deleted');
            redirect("user/Software/showSoftware");
        }
        else{
            $this->session->set_flashdata('error', 'Something went wrong');
            redirect("user/Software/showSoftware");
        }
    }

}