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

class WebPackage extends CI_Controller {


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

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


    public function insertWebPackage(){
        $this->load->view('user/add-web-package');
    }

    public function submitWebPackage(){
        $this->form_validation->set_rules('title', 'Title', 'required');
		$this->form_validation->set_rules('price', 'Price', 'required');
        $this->form_validation->set_rules('type', 'Type', 'required');
        $this->form_validation->set_rules('description', 'Description', 'required');
        
        if ($this->form_validation->run() == FALSE){

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

        	redirect("user/WebPackage/insertWebPackage");
                
        }
        else{
            $data = array(
                'title' => $this->input->post('title'),
                'price' => $this->input->post('price'),
                'type' => $this->input->post('type'),
                'description' => $this->input->post('description')
            );

            $this->load->model('web_package_model');
            $res = $this->web_package_model->insert_web_package($data);

            $var['vars'] = array(

                's_msg' => 'Successfully Updated',
                'type' => $this->input->post('type')

            );

            if($res == TRUE){
                $this->session->set_flashdata('success', array( 's_msg' => 'Successfully Updated','type' => $this->input->post('type')));
                redirect("user/WebPackage/insertWebPackage");
            }
            else{
                $this->session->set_flashdata('error', 'Something went wrong');
                redirect("user/WebPackage/insertWebPackage");
            }
            
        }
    }

    public function showWebPackage(){
        $this->load->model('web_package_model');
        $data['results'] = $this->web_package_model->show_web_package();
        $this->load->view('user/list-web-package', $data);
    }

    public function editWebPackage(){

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

        $this->load->model('web_package_model');
        $data['edit'] = "editWebPackage";
        $data['results'] = $this->web_package_model->single_web_package($data);
        $this->load->view('user/add-web-package', $data);
    }

    public function updateWebPackage(){
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('price', 'Price', 'required');
        $this->form_validation->set_rules('type', 'Type', 'required');
        $this->form_validation->set_rules('description', 'Description', 'required');

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

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

        	redirect('user/Software/insertSoftware');
                
        }
        else{
            $data = array(
                'webpackageid' => $this->input->post('webpackageid'),
                'title' => $this->input->post('title'),
                'price' => $this->input->post('price'),
                'type' => $this->input->post('type'),
                'description' => $this->input->post('description')
            );

            $this->load->model('web_package_model');
            $res = $this->web_package_model->update_web_package($data);

            $var['vars'] = array(

                's_msg' => 'Successfully Updated',
                'type' => $this->input->post('type')

            );
            
            if($res == TRUE){
                $this->session->set_flashdata('success', array( 's_msg' => 'Successfully Updated','type' => $this->input->post('type')));
                redirect("user/WebPackage/editWebPackage?webpackageid=".$this->input->post('webpackageid'));
            }
            else{
                $this->session->set_flashdata('error', 'Something went wrong');
                redirect("user/WebPackage/editWebPackage?webpackageid=".$this->input->post('webpackageid'));
            }
        }
    }


    public function deleteWebPackage($webpackageid){
        $data = array(
            'webpackageid'=> $webpackageid
        );
        $this->load->model('web_package_model');
        $res = $this->web_package_model->delete_web_package($data);
        
        if($res == TRUE){
            $this->session->set_flashdata('success', 'Successfully Deleted');
            redirect("user/WebPackage/showWebPackage");
        }
        else{
            $this->session->set_flashdata('error', 'Something went wrong');
            redirect("user/WebPackage/showWebPackage");
        }
    }

}