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

class Sub_services extends CI_Controller {
var $role = array();
public function __construct() {
        parent::__construct();
        date_default_timezone_set("Asia/Kolkata");
        $this->load->model('Subservices_model');

         if(!$this->session->userdata('logged_garage')) {
            redirect(base_url());
        }


         
    }




public function index()
    {
   

     $data['menu'] = 'Subservices Management';
     $data['smenu'] = 'View Subservices List';
     $data['pTitle'] = "Subservices Management";
     $data['pDescription'] = "View Subservices List";
     $data['page'] = 'Subservices/list';
     $data['datatable'] = 1;
     $data['subcatData']=$this->Subservices_model->getsubcatData();

     $this->load->view('template', $data);
 }

public function create()
 {
 
   $data['menu'] = 'Subservices Management';
   $data['smenu'] = 'Add Subservices';
   $data['pTitle'] = "Subservices Management";
   $data['pDescription'] = "Add Subservices";
   $data['page'] = 'Subservices/create';
   $data['subcatData'] = $this->Subservices_model->getsubcatData();
   $data['catData'] = $this->Subservices_model->getcatData();
   //echo json_encode($data);exit;
   $this->load->view('template', $data);
}


    
public function create_Subservices()
{
    $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'danger');
    if(!isset($_POST) || empty($_POST)){
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Sub_services/create'));
    }



    $err = 0;
    $errMsg = '';
    if($err == 0 && (!isset($_POST['service_id']) || empty($_POST['service_id']))){
        $err = 1;
        $errMsg = 'Provide a Services Name';
        
    // } else if($err == 0 && (!isset($_FILES) || empty($_FILES) || 
    //     !isset($_FILES['subservices']) || empty($_FILES['subservices']))){
    //     $err = 1;
    //     $errMsg = 'Provide a Subservices Image';
    }else if($err == 0 && (!isset($_POST['sub_services']) || empty($_POST['sub_services']))){
        $err = 1;
        $errMsg = 'Provide a Subservices';
      }




    if($err == 0){



       
        
        $subcatData = array('service_id' => $this->input->post('service_id'),
                             'sub_services' => $this->input->post('sub_services'));
                             
                        
                        

                       
                   
        
    }

    if($err == 1){
        $flashMsg['message'] = $errMsg;
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Sub_services'));
    }



    $qry = $this->Subservices_model->add_subservices($subcatData);
    if($qry)
    {
        
       $this->session->set_flashdata('message', array('message' => "Successfully added.",'class' => 'success'));
       redirect(base_url().'Sub_services');
       
       
   }else{
     $this->session->set_flashdata('message',$flashMsg);
     
 }


 


 
}

    
function edit($subcat_id=''){

    $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'danger');
    if(empty($subcat_id) || empty(decode_param($subcat_id))){
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Sub_services/list'));
    }
    $data['page'] = 'Subservices/create';
    $data['menu'] = 'Subservices Management';
    $data['smenu'] = 'Edit Subservices';
    $data['pTitle'] = "Edit Subservices";
    $data['pDescription'] = "Update Subservices Data";
     $data['catData'] = $this->Subservices_model->getcatData();
    $data['subcat_id'] = $subcat_id;
    $data['subcatData'] = $this->Subservices_model->getsubcatData(decode_param($subcat_id));
    
    $this->load->view('template',$data);
}



public function edit_Subservices($subcat_id='')
    {

          $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'danger');

    if(!isset($_POST) || empty($_POST) || empty($subcat_id)){
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Sub_services'));
    }

 if(isset($_FILES['subservices']['name']) && !empty($_FILES['subservices']['name']))
                {

                    $files = $_FILES;
                    $cpt = count($_FILES['subservices']['name']);

                    $_FILES['userfile']['name']= time().$files['subservices']['name'];
                    $_FILES['userfile']['type']= $files['subservices']['type'];
                    $_FILES['userfile']['tmp_name']= $files['subservices']['tmp_name'];
                    $_FILES['userfile']['error']= $files['subservices']['error'];
                    $_FILES['userfile']['size']= $files['subservices']['size'];
                    $config = array();
                    $config['upload_path'] = './uploads/subservices';
                    $config['allowed_types'] = 'gif|jpg|jpeg|png|flv|f4v';
                    $config['max_size']      = '';
                    $config['overwrite']     = FALSE;
                    $this->load->library('upload', $config);
                    $this->upload->initialize($config);
                    $upload_img = $this->upload->do_upload();
                    
                    if(!$upload_img){

                        $this->session->set_flashdata('message', array('message' => $this->upload->display_errors(),'class' => 'danger'));
                       
                    } else{
                        $uploading_file = $this->upload->data();
                        $image_file = $config['upload_path'].'/'.$uploading_file['file_name'];

                    }
                   
                    $SubcatData = array('service_id' => $this->input->post('service_id'),
                             'sub_services' => $this->input->post('sub_services'));

                       
                    
                    }else{
                    $SubcatData = array('service_id' => $this->input->post('service_id'),
                             'sub_services' => $this->input->post('sub_services'));
                        
                        }

       
                     $result = $this->Subservices_model->update_subservices(decode_param($subcat_id),$SubcatData);
                    if($result)
                    {
                        
                         $this->session->set_flashdata('message', array('message' => "Successfully Updated.",'class' => 'success'));
                          redirect(base_url().'Sub_services');
                        
                        
                    }else{
                        $this->session->set_flashdata('message', array('message' => "Please try again.",'class' => 'danger'));
                        
                    }


 
}

public function delete($subcat_id='') {
    
    $result = $this->Subservices_model->delete_subservices(decode_param($subcat_id));
    $this->session->set_flashdata('message', array('message' => 'Subservices Deleted Successfully','class' => 'success'));
    redirect(base_url().'Sub_services');
}





}