<?php

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

	/**
	 * 
	 */
    class PackagesController extends CI_Controller
	    {
		    function __construct(){
                parent::__construct();
                    if(!$this->session->id){
                    redirect(site_url('admin/login'));
                }
                $this->load->library('upload');
            }
            
            public function index(){
                $this->db->select('*');
                $this->db->from('tbl_package a');
                $data['packages'] = $this->db->get()->result();
                
                
                
                $this->load->view('backend/global/header');
                $this->load->view('backend/global/menu');
                $this->load->view('backend/packages/list',$data);
                $this->load->view('backend/global/footer');
            } 
            
            public function detail(){
                $this->load->view('backend/global/header');
                $this->load->view('backend/global/menu');
                $this->load->view('backend/team/detail');
                $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/packages/add');
                    $this->load->view('backend/global/footer');
            }
            
            
            public function add_action() {
                            // === Upload main image ===
                            @mkdir('./uploads/package/');
                            $config['upload_path']   = './uploads/package/';
                            $config['allowed_types'] = 'gif|jpg|jpeg|png';
                            $this->load->library('upload', $config);
                        
                            $config['file_name'] = strtolower($_FILES['package_image']['name']);
                            $this->upload->initialize($config);
                            $this->upload->do_upload('package_image');
                            $upload_data = $this->upload->data();
                            $mainimagefilename = $upload_data['file_name'];
                        
                            $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $this->input->post('package_title'))));
                            $data = [
                                'package_title' => $this->input->post('package_title'),
                                'package_sub_title' => $this->input->post('package_sub_title'),
                                'package_type' => $this->input->post('package_type'),
                                'package_description' => $this->input->post('package_description'),
                                'package_meta_title' => $this->input->post('package_meta_title'),
                                'package_meta_keyword' => $this->input->post('package_meta_keyword'),
                                'package_meta_description' => $this->input->post('package_meta_description'),
                              
                                // itinerary...
                                'itinerary_day1' => $this->input->post('itinerary_day1'),
                                'itinerary_desc1' => $this->input->post('itinerary_desc1'),
                                'itinerary_day2' => $this->input->post('itinerary_day2'),
        					    'itinerary_desc2' => $this->input->post('itinerary_desc2'),
        					    'itinerary_day3' => $this->input->post('itinerary_day3'),
        					    'itinerary_desc3' => $this->input->post('itinerary_desc3'),
        					    'itinerary_day4' => $this->input->post('itinerary_day4'),
        					    'itinerary_desc4' => $this->input->post('itinerary_desc4'),
        					    'itinerary_day5' => $this->input->post('itinerary_day5'),
        					    'itinerary_desc5' => $this->input->post('itinerary_desc5'),
        					    'itinerary_day6' => $this->input->post('itinerary_day6'),
        					    'itinerary_desc6' => $this->input->post('itinerary_desc6'),
        					    'itinerary_day7' => $this->input->post('itinerary_day7'),
        					    'itinerary_desc7' => $this->input->post('itinerary_desc7'),
        					    'itinerary_day8' => $this->input->post('itinerary_day8'),
        					    'itinerary_desc8' => $this->input->post('itinerary_desc8'),
        					    
                                // ... repeat other itinerary fields
                                'package_slug' => $slug,
                                'package_image' => $mainimagefilename,
                               
                            ];
                        
                            $query = $this->db->insert('tbl_package', $data);
                            if ($query) {
                                $this->session->set_flashdata('success', 'Sucessfully Added');
                                redirect('admin/package');
                            } else {
                                $this->session->set_flashdata('error', 'Something went wrong. Error!!');
                                redirect('admin/package');
                            }
                        }

            
            
            
            public function edit(){
                $this->db->where('package_id', $this->input->get('bid'));
				$data['packageE'] = $this->db->get('tbl_package')->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/packages/add',$data);
                    $this->load->view('backend/global/footer');
                
            } 
            
            public function update() {
                        $package_id = $this->input->post('package_id');
                    
                        // Handle Main Image
                        $mainimagefilename = $this->input->post('existing_image'); // add a hidden input with existing image value
                        if (!empty($_FILES['package_image']['name'])) {
                            @mkdir('./uploads/package/');
                            $config['upload_path']   = './uploads/package/';
                            $config['allowed_types'] = 'gif|jpg|jpeg|png';
                            $config['file_name']      = strtolower($_FILES['package_image']['name']);
                            $this->load->library('upload', $config); 
                            $this->upload->initialize($config);
                            if ($this->upload->do_upload('package_image')) {
                                $upload_data       = $this->upload->data();
                                $mainimagefilename = $upload_data['file_name'];
                            }
                        }
                    
                    
                        $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $this->input->post('package_title'))));
                        $data = [
                            // same fields as before...
                            'package_title'            => $this->input->post('package_title'),
                            'package_sub_title'        => $this->input->post('package_sub_title'),
                            'package_type' => $this->input->post('package_type'),
                            'package_description'      => $this->input->post('package_description'),
                            // ... add itinerary fields similarly
                            // itinerary...
                                'itinerary_day1' => $this->input->post('itinerary_day1'),
                                'itinerary_desc1' => $this->input->post('itinerary_desc1'),
                                'itinerary_day2' => $this->input->post('itinerary_day2'),
        					    'itinerary_desc2' => $this->input->post('itinerary_desc2'),
        					    'itinerary_day3' => $this->input->post('itinerary_day3'),
        					    'itinerary_desc3' => $this->input->post('itinerary_desc3'),
        					    'itinerary_day4' => $this->input->post('itinerary_day4'),
        					    'itinerary_desc4' => $this->input->post('itinerary_desc4'),
        					    'itinerary_day5' => $this->input->post('itinerary_day5'),
        					    'itinerary_desc5' => $this->input->post('itinerary_desc5'),
        					    'itinerary_day6' => $this->input->post('itinerary_day6'),
        					    'itinerary_desc6' => $this->input->post('itinerary_desc6'),
        					    'itinerary_day7' => $this->input->post('itinerary_day7'),
        					    'itinerary_desc7' => $this->input->post('itinerary_desc7'),
        					     'itinerary_day8' => $this->input->post('itinerary_day8'),
        					    'itinerary_desc8' => $this->input->post('itinerary_desc8'),
        					   
                                // ... repeat other itinerary fields
                            'package_image'            => $mainimagefilename,
                            'package_slug'             => $slug,
                            'package_status'           => $this->input->post('package_status'),
                            'package_meta_title'       => $this->input->post('package_meta_title'),
                            'package_meta_keyword'      => $this->input->post('package_meta_keyword'),
                            'package_meta_description'  => $this->input->post('package_meta_description'),
                        ];
                        $this->db->where('package_id', $package_id);
                        $query = $this->db->update('tbl_package', $data);
                    
                        if ($query) {
                            $this->session->set_flashdata('success', 'Updated successfully.');
                        } else {
                            $this->session->set_flashdata('error', 'Error updating.');
                        }
                        redirect('admin/package');
                    }

            public function delete(){
                 $this->db->where('package_id', $this->input->get('bid'));
			    $this->db->delete('tbl_package');
				redirect('admin/package');
               
            }
	}