<?php

	defined('BASEPATH') or exit('No Direct Script is Allowed');

	/**
	 * 
	 */
    class PricingController extends CI_Controller
	    {
		    function __construct(){
                parent::__construct();
                    if(!$this->session->id){
                    redirect(site_url('admin/login'));
                }
            }
            
            public function index(){
                $this->db->select('*');
                $this->db->from('tbl_pricing a');
                $data['pricings'] = $this->db->get()->result();
                
                    $this->load->view('backend/global/header');
                    $this->load->view('backend/global/menu');
                    $this->load->view('backend/pricing/list',$data);
                    $this->load->view('backend/global/footer');
            } 
            
            public function add(){
                
                $this->db->where('adm_id', $this->session->id);
			    $user['data'] = $this->db->get('tbl_admin')->row();	
                    $this->load->view('backend/global/header',$user);
                    $this->load->view('backend/global/menu');
                    $this->load->view('backend/pricing/add');
                    $this->load->view('backend/global/footer');
            }
            
            
            public function add_action() {
                            $data = [
                                'title' => $this->input->post('title'),
                                'price' => $this->input->post('price'),
                                'popular' => $this->input->post('popular'),
                                'description' => $this->input->post('description'),
                               
                            ];
                        
                            $query = $this->db->insert('tbl_pricing', $data);
                            if ($query) {
                                $this->session->set_flashdata('success', 'Sucessfully Added');
                                redirect('admin/pricing');
                            } else {
                                $this->session->set_flashdata('error', 'Something went wrong. Error!!');
                                redirect('admin/pricing');
                            }
                        }

            
            
            
            public function edit(){
                $this->db->where('id', $this->input->get('bid'));
				$data['pricingsE'] = $this->db->get('tbl_pricing')->row();
                $this->db->where('adm_id', $this->session->id);
			    $user['data'] = $this->db->get('tbl_admin')->row();	
                    $this->load->view('backend/global/header',$user);
                    $this->load->view('backend/global/menu');
                    $this->load->view('backend/pricing/add',$data);
                    $this->load->view('backend/global/footer');
                
            } 
            
            public function update() {
                   $data = [
                        'title'       => $this->input->post('title'),
                        'description' => $this->input->post('description'),
                        'popular'        => $this->input->post('popular'),
                        'price'        => $this->input->post('price'),
                        'status'        => $this->input->post('status'),
                    ];
                    
                    // Update query
                    $this->db->where('id', $this->input->get('id'));
                    $updated = $this->db->update('tbl_pricing', $data);
                    
                    if ($updated) {
                        $this->session->set_flashdata('success', 'Successfully Updated');
                    } else {
                        $this->session->set_flashdata('error', 'Update Failed');
                    }
                    
                    redirect('admin/pricing');
                    }

            public function delete(){
                 $this->db->where('id', $this->input->get('bid'));
			    $this->db->delete('tbl_pricing');
				redirect('admin/pricing');
               
            }
	}