<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SettingModel;
use App\Models\HomeModel;
use App\Models\UploadModel;

class HomeSetting extends BaseController
{
    public function get_all_home_slider()
    {
        $setting = new SettingModel();
        $home = new HomeModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_home_slider'] = $home->get_all_slider();
        echo view('admin/include/header', $data);
        echo view('admin/home_setting/view_home_slider');
        echo view('admin/include/footer');
    }

    function add_new_slider()
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        echo view('admin/include/header', $data);
        echo view('admin/home_setting/add_new_slider');
        echo view('admin/include/footer');
    }
    function new_slider_add()
    {
        $home = new HomeModel();
        $upload = new UploadModel();
        $data['content_1']      =  $this->request->getVar('content_1');
        $data['content_2']      =  $this->request->getVar('content_2');
        $data['btn_name']       =  $this->request->getVar('btn_name');
        $data['btn_link']       =  $this->request->getVar('btn_link');
        $data['status']    =  1;
        if($this->request->getFile('bg_image')->isValid() && !$this->request->getFile('bg_image')->hasMoved())
        {
            $data['bg_image'] = $upload->upload_image_2($this->request->getFile('bg_image'), 1200, 500);
        }
        if($home->new_slider_add($data))
        {
            session()->set('success', "Slider Successfully Added");
            return redirect()->to('admin/home_setting/slider');
        }
        else
        {
            session()->set('error', "Slider Added Failed");
            return redirect()->to('admin/home_setting/slider');
        }
    }

    function edit_slider($id)
    {
        $setting = new SettingModel();
        $home = new HomeModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['slider'] = $home->get_slider_by_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/home_setting/edit_slider');
        echo view('admin/include/footer');
    }

    function update_slider($id)
    {
        $home = new HomeModel();
        $upload = new UploadModel();
        $data['content_1']      =  $this->request->getVar('content_1');
        $data['content_2']      =  $this->request->getVar('content_2');
        $data['btn_name']       =  $this->request->getVar('btn_name');
        $data['btn_link']       =  $this->request->getVar('btn_link');
        $slider_img = $home->get_slider_by_id($id);
        if($this->request->getFile('bg_image')->isValid() && !$this->request->getFile('bg_image')->hasMoved())
        {
            if (file_exists(substr($slider_img->bg_image, 1))) {
                unlink(substr($slider_img->bg_image, 1));
            }
            $data['bg_image'] = $upload->upload_image_2($this->request->getFile('bg_image'), 1200, 500);
        }
        if($home->update_slider($data, $id))
        {
            session()->set('success', "Slider Successfully Updated");
            return redirect()->to('admin/home_setting/slider');
        }
        else
        {
            session()->set('error', "Slider Updated Failed");
            return redirect()->to('admin/home_setting/slider');
        }
    }

    function delete_slider($id)
    {
        $home = new HomeModel();
        $slider_img = $home->get_slider_by_id($id);
        if (file_exists(substr($slider_img->bg_image, 1))) {
                unlink(substr($slider_img->bg_image, 1));
            }
        if($home->delete_slider($id))
        {
            session()->set('success', "Slider Successfully Deleted");
            return redirect()->to('admin/home_setting/slider');
        }
        else
        {
            session()->set('error', "Slider Deleted Failed");
            return redirect()->to('admin/home_setting/slider');
        }
    }

    function change_status($id, $status)
    {
        $home = new HomeModel();
        $data['status'] = $status;
        if($home->change_status($data, $id))
        {
            session()->set('success', "Slider Staus Successfully Changed");
            return redirect()->to('admin/home_setting/slider');
        }
        else
        {
            session()->set('error', "Slider Deleted Failed");
            return redirect()->to('admin/home_setting/slider');
        }
    }

   
    function pages()
    {
        $setting = new SettingModel();
        $home = new HomeModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['get_all_page'] = $home->get_all_page();
        echo view('admin/include/header', $data);
        echo view('admin/home_setting/page_setting');
        echo view('admin/include/footer');
    }

    function delete_page($id)
    {
        $home = new HomeModel();
        if($home->delete_page($id))
        {
            session()->set('success', "Page Successfully Deleted");
            return redirect()->to('admin/home_setting/pages');
        }
        else
        {
            session()->set('error', "Page Deleted Failed");
            return redirect()->to('admin/home_setting/pages');
        }
    }

    function update_page($id)
    {
        $home = new HomeModel();
        $upload = new UploadModel();
        $data['name'] = $this->request->getVar('name');
        $data['slug'] = slug($this->request->getVar('name'));
        $data['content'] = $this->request->getVar('content');
        $data['meta_descrition'] = $this->request->getVar('meta_descrition');
        $data['meta_keyword'] = $this->request->getVar('meta_keyword');
        $data['status'] = $this->request->getVar('status');
        $data['important'] = $this->request->getVar('important');
        
        $page_img = $home->get_page_by_id($id);
        if($this->request->getFile('image')->isValid() && !$this->request->getFile('image')->hasMoved())
        {
            if (file_exists(substr($page_img->image, 1))) {
                unlink(substr($page_img->image, 1));
            }
            $data['image'] = $upload->upload_image($this->request->getFile('image'));
        }

        if($home->update_page($data, $id))
        {
            session()->set('success', "Page Successfully Updated");
            return redirect()->to('admin/home_setting/pages');
        }
        else
        {
            session()->set('error', "Page Updated Failed");
            return redirect()->to('admin/home_setting/pages');
        }
    }

    function add_new_page()
    {
        $home = new HomeModel();
        $upload = new UploadModel();
        $data['name'] = $this->request->getVar('name');
        $data['slug'] = slug($this->request->getVar('name'));
        $data['content'] = $this->request->getVar('content');
        $data['meta_descrition'] = $this->request->getVar('meta_descrition');
        $data['meta_keyword'] = $this->request->getVar('meta_keyword');
        $data['status'] = 1;
        $data['important'] = $this->request->getVar('important');
        
       
        if($this->request->getFile('image')->isValid() && !$this->request->getFile('image')->hasMoved())
        {
            $data['image'] = $upload->upload_image($this->request->getFile('image'));
        }
        if($home->add_new_page($data))
        {
            session()->set('success', "Page Successfully Added");
            return redirect()->to('admin/home_setting/pages');
        }
        else
        {
            session()->set('error', "Page Added Failed");
            return redirect()->to('admin/home_setting/pages');
        }
    }

    function all_banner()
    {
        $setting = new SettingModel();
        $home = new HomeModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_banner'] = $home->get_all_banner();
        echo view('admin/include/header', $data);
        echo view('admin/home_setting/all_banner');
        echo view('admin/include/footer');
    }

    function add_new_banner()
    {
        $setting = new SettingModel();
        $home = new HomeModel();
        $data['app_setting'] = $setting->get_application_setting();
        // $data['all_banner'] = $home->get_all_banner();
        echo view('admin/include/header', $data);
        echo view('admin/home_setting/add_new_banner');
        echo view('admin/include/footer');
    }

    function add_banner()
    {
        $home = new HomeModel();
        $upload = new UploadModel();
        $data['banner_position'] = $this->request->getVar('banner_position');
        $data['banner_name'] = $this->request->getVar('banner_name');
        $data['banner_url'] = $this->request->getVar('banner_url');
        if($this->request->getFile('banner_image')->isValid() && !$this->request->getFile('banner_image')->hasMoved())
        {
            $data['banner_image'] = $upload->upload_image($this->request->getFile('banner_image'));
        }
        if($home->add_banner($data))
        {
            session()->set('success', "Banner Successfully Added");
            return redirect()->to('admin/home_setting/all_banner');
        }
        else
        {
            session()->set('error', "Banner Added Failed");
            return redirect()->to('admin/home_setting/all_banner');
        }
    }

    function delete_banner($id)
    {
        $home = new HomeModel();
        $img = $home->get_banner_by_id($id);
        if(file_exists(substr($img->banner_image, 1)))
        {
            unlink(substr($img->banner_image, 1));
        }
        if($home->delete_banner($id))
        {
            session()->set('success', "Banner Successfully Deleted");
            return redirect()->to('admin/home_setting/all_banner');
        }
        else
        {
            session()->set('error', "Banner Deleted Failed");
            return redirect()->to('admin/home_setting/all_banner');
        }
    }

    function edit_banner($id)
    {
        $setting = new SettingModel();
        $home = new HomeModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['banner'] = $home->get_banner_by_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/home_setting/edit_banner');
        echo view('admin/include/footer');
    }

    function update_banner($id)
    {
        $home = new HomeModel();
        $upload = new UploadModel();
        $data['banner_name'] = $this->request->getVar('banner_name');
        $data['banner_url'] = $this->request->getVar('banner_url');
        $img = $home->get_banner_by_id($id);
        if($this->request->getFile('banner_image')->isValid() && !$this->request->getFile('banner_image')->hasMoved())
        {
            if (file_exists(substr($img->banner_image, 1))) {
                unlink(substr($img->banner_image, 1));
            }
            $data['banner_image'] = $upload->upload_image($this->request->getFile('banner_image'));
        }

        if($home->update_banner($data, $id))
        {
            session()->set('success', "Banner Successfully Updated");
            return redirect()->to('admin/home_setting/all_banner');
        }
        else
        {
            session()->set('error', "Banner Updated Failed");
            return redirect()->to('admin/home_setting/all_banner');
        }
    }

    function delete_testimonials($id)
    {
        $home = new HomeModel();
        $img = $home->get_testimonials_by_id($id);
        if(file_exists(substr($img->testi_image, 1)))
        {
            unlink(substr($img->testi_image, 1));
        }
        if($home->delete_testimonials($id))
        {
            session()->set('success', "Testimonials Successfully Deleted");
            return redirect()->to('admin/home_setting/all_testimonials');
        }
        else
        {
            session()->set('error', "Testimonials Deleted Failed");
            return redirect()->to('admin/home_setting/all_testimonials');
        }
    }
}
