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

class HostingPackage extends CI_Controller {

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

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


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


    public function submitHostingPackage(){
        $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/HostingPackage/insertHostingPackage");
                
        }
        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('hosting_package_model');
            $res = $this->hosting_package_model->insert_hosting_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/HostingPackage/insertHostingPackage");
            }
            else{
                $this->session->set_flashdata('error', 'Something went wrong');
                redirect("user/HostingPackage/insertHostingPackage");
            }
        }
    }


    public function showHostingPackage(){
        $this->load->model('hosting_package_model');
        $data['results'] = $this->hosting_package_model->show_hosting_package();
        $this->load->view('user/list-hosting-package', $data);
    }


    public function editHostingPackage(){

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

        $this->load->model('hosting_package_model');
        $data['edit'] = "editHostingPackage";
        $data['results'] = $this->hosting_package_model->single_hosting_package($data);
        $this->load->view('user/add-hosting-package', $data);
    }

    public function updateHostingPackage(){
        $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/HostingPackage/insertHostingPackage');
                
        }
        else{
            $data = array(
                'packageid' => $this->input->post('packageid'),
                'title' => $this->input->post('title'),
                'price' => $this->input->post('price'),
                'type' => $this->input->post('type'),
                'description' => $this->input->post('description')
            );

            $this->load->model('hosting_package_model');
            $res = $this->hosting_package_model->update_hosting_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/HostingPackage/editHostingPackage?packageid=".$this->input->post('packageid'));
            }
            else{
                $this->session->set_flashdata('error', 'Something went wrong');
                redirect("user/HostingPackage/editHostingPackage?packageid=".$this->input->post('packageid'));
            }
        }
    }

    public function deleteHostingPackage($packageid){
        $data = array(
            'packageid'=> $packageid
        );
        $this->load->model('hosting_package_model');
        $res = $this->hosting_package_model->delete_hosting_package($data);
        
        if($res == TRUE){
            $this->session->set_flashdata('success', 'Successfully Deleted');
            redirect("user/HostingPackage/showHostingPackage");
        }
        else{
            $this->session->set_flashdata('error', 'Something went wrong');
            redirect("user/HostingPackage/showHostingPackage");
        }
    }

}