<?php

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

	/**
	 * 
	 */
    class IndustriesController 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_industries a');
                $data['industries'] = $this->db->get()->result();
                
                    $this->load->view('backend/global/header');
                    $this->load->view('backend/global/menu');
                    $this->load->view('backend/industries/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/industries/add');
                    $this->load->view('backend/global/footer');
            }
            
            
            public function add_action() {
                            // === Upload main image ===
                            @mkdir('./uploads/industries/');
                            $config['upload_path']   = './uploads/industries/';
                            $config['allowed_types'] = 'gif|jpg|jpeg|png';
                            $this->load->library('upload', $config);
                        
                            $config['file_name'] = strtolower($_FILES['icon']['name']);
                            $this->upload->initialize($config);
                            $this->upload->do_upload('icon');
                            $upload_data = $this->upload->data();
                            $mainimagefilename = $upload_data['file_name'];
                            
                            @mkdir('./uploads/industries/');
                            $config['upload_path']   = './uploads/industries/';
                            $config['allowed_types'] = 'gif|jpg|jpeg|png';
                            $this->load->library('upload', $config);
                        
                            $config['file_name'] = strtolower($_FILES['image']['name']);
                            $this->upload->initialize($config);
                            $this->upload->do_upload('image');
                            $upload_data1 = $this->upload->data();
                            $mainimagefile = $upload_data['file_name'];
                        
                            $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $this->input->post('title'))));
                            $data = [
                                'title' => strtoupper($this->input->post('title')),
                                'description' => $this->input->post('description'),
                              
                                // itinerary...
                                'faq1' => $this->input->post('faq1'),
                                'desc1' => $this->input->post('desc1'),
                                'faq2' => $this->input->post('faq2'),
                                'desc2' => $this->input->post('desc2'),
                                'faq3' => $this->input->post('faq3'),
                                'desc3' => $this->input->post('desc3'),
                                'faq4' => $this->input->post('faq4'),
                                'desc4' => $this->input->post('desc4'),
                                'faq5' => $this->input->post('faq5'),
                                'desc5' => $this->input->post('desc5'),
                                
        					    
                                // ... repeat other itinerary fields
                                'slug' => $slug,
                                'icon' => $mainimagefilename,
                                'image' => $mainimagefile,
                               
                            ];
                        
                            $query = $this->db->insert('tbl_industries', $data);
                            if ($query) {
                                $this->session->set_flashdata('success', 'Sucessfully Added');
                                redirect('admin/industries');
                            } else {
                                $this->session->set_flashdata('error', 'Something went wrong. Error!!');
                                redirect('admin/industries');
                            }
                        }

            
            
            
            public function edit(){
                $this->db->where('id', $this->input->get('bid'));
				$data['industriesE'] = $this->db->get('tbl_industries')->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/industries/add',$data);
                    $this->load->view('backend/global/footer');
                
            } 
            
            public function update() {
                    $id = $this->input->post('id'); // assuming you're passing the record ID

                    // Get existing image/icon from form
                    $existing_icon  = $this->input->post('existing_icon');
                    $existing_image = $this->input->post('existing_image');
                    
                    @mkdir('./uploads/industries/', 0777, true);
                    $this->load->library('upload');
                    
                    // Upload new icon if exists
                    $icon_filename = $existing_icon;
                    if (!empty($_FILES['icon']['name'])) {
                        $icon_config = [
                            'upload_path'   => './uploads/industries/',
                            'allowed_types' => 'gif|jpg|jpeg|png',
                            'file_name'     => strtolower($_FILES['icon']['name']),
                        ];
                        $this->upload->initialize($icon_config);
                        if ($this->upload->do_upload('icon')) {
                            $upload_data = $this->upload->data();
                            $icon_filename = $upload_data['file_name'];
                    
                            // Delete old icon if needed
                            if (!empty($existing_icon) && file_exists('./uploads/industries/' . $existing_icon)) {
                                @unlink('./uploads/industries/' . $existing_icon);
                            }
                        }
                    }
                    
                    // Upload new image if exists
                    $image_filename = $existing_image;
                    if (!empty($_FILES['image']['name'])) {
                        $image_config = [
                            'upload_path'   => './uploads/industries/',
                            'allowed_types' => 'gif|jpg|jpeg|png',
                            'file_name'     => strtolower($_FILES['image']['name']),
                        ];
                        $this->upload->initialize($image_config);
                        if ($this->upload->do_upload('image')) {
                            $upload_data2 = $this->upload->data();
                            $image_filename = $upload_data2['file_name'];
                    
                            // Delete old image if needed
                            if (!empty($existing_image) && file_exists('./uploads/industries/' . $existing_image)) {
                                @unlink('./uploads/industries/' . $existing_image);
                            }
                        }
                    }
                    
                   
                    
                    // Prepare data
                    $data = [
                        'title'       => strtoupper($this->input->post('title')),
                        'description' => $this->input->post('description'),
                        'faq1'        => $this->input->post('faq1'),
                        'desc1'       => $this->input->post('desc1'),
                        'faq2'        => $this->input->post('faq2'),
                        'desc2'       => $this->input->post('desc2'),
                        'faq3'        => $this->input->post('faq3'),
                        'desc3'       => $this->input->post('desc3'),
                        'faq4'        => $this->input->post('faq4'),
                        'desc4'       => $this->input->post('desc4'),
                        'faq5'        => $this->input->post('faq5'),
                        'desc5'       => $this->input->post('desc5'),
                        'icon'        => $icon_filename,
                        'image'       => $image_filename,
                    ];
                    
                    // Update query
                    $this->db->where('id', $id);
                    $updated = $this->db->update('tbl_industries', $data);
                    
                    if ($updated) {
                        $this->session->set_flashdata('success', 'Successfully Updated');
                    } else {
                        $this->session->set_flashdata('error', 'Update Failed');
                    }
                    
                    redirect('admin/industries');
                    }

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