<?php 

defined('BASEPATH') or exit ('Direct script access not allowed');

require_once APPPATH . "core/My_Admin.php";

class Banner extends My_Admin{

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

    public $table = 'banners';


    // * banners list
    public function index(){

        if($this->input->post()){

            $data = $_POST;
            $data['banner'] = upload_media($_FILES['banner']);
            $data['datetime'] = date('Y-m-d H:i:s');
            $this->cm->save_data($data);

            redirect(site_url() .'/admin/banners/list_ban');
            
        }

        $this->load->view('admin/includes/header');
        $this->load->view('admin/banner/create_banner');
        $this->load->view('admin/includes/footer');

    }


    
    public function list_ban(){

        $this->load->view('admin/includes/header');
        $this->load->view('admin/banner/view_banner');
        $this->load->view('admin/includes/footer');

    }

    public function edit(){

        if (isset($_GET['id'])) {

            $data['details'] = $this->cm->search_single_where(['id' => $_GET['id']]);

            if (empty($data['details'])) {

                redirect(site_url() . '/admin/banner/list_ban');

            } else {

                if ($this->input->post()) {

                    $id = $_GET['id'];

                    $data = $_POST;
                    if(!!$_FILES['banner']['name']){

                        $data['banner'] = upload_media($_FILES['banner']);

                    }

                    $this->cm->update_where($data, ['id' => $id]);

                    $this->session->set_flashdata('success', 'Data updated successfuly.');
                    redirect(site_url() . "/admin/banner/edit?id=$id");
                }
                
                $this->load->view('admin/includes/header', $data);
                $this->load->view('admin/banner/edit_banner');
                $this->load->view('admin/includes/footer');
            }
        } else {
            
            redirect(site_url() . '/admin/banner/list_ban');
        }
        
    }

    public function delete(){

        if (isset($_GET['id'])) {

            $data['details'] = $this->cm->search_single_where(['id' => $_GET['id']]);
            
            if (empty($data['details'])) {
                
                redirect(site_url() . '/admin/banner/list_ban');
                
            }else{
                
                $this->cm->remove_where(['id' => $_GET['id']]);

                $this->session->set_flashdata('success', 'Data deleted successfuly.');
                redirect(site_url() . '/admin/banner/list_ban');
                
            }

        }else{

            redirect(site_url() . '/admin/banner/list_ban');
            
        }
        
    }
    
}
