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

class Homepage_settings extends AtadminController {

    public function __construct()
    {
        parent::__construct(); // Construct CI's core so that you can use it

        $this->load->model('at-admin/Security','security_model');

        //check user have login or not
        $this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
    }

    function index()
    {
        $uri = 'home';
        $this->load->model('at-admin/Landingmain_Model', 'landingmain');
        $this->load->model('at-admin/Page_settings_model', 'page_settings');
        $main = $this->landingmain->search_by_uri($uri);
        if ($this->input->method() == 'post') {
            $request = $this->input->post();
            $this->load->model('at-admin/Upload_model', 'upload');
            foreach (['banner_desktop', 'banner_mobile', 'background_image'] as $item) {
                $request[$item] = '';
                if ($_FILES[$item]['error'] == 0) {
                    $result = $this->upload->uploadfile($item, false);
                    if ($result['status'] == 'success') {
                        $request[$item] = $result['url'];
                    }
                    else {
                        $request[$item] = '';
                    }
                }
                else {
                    $request[$item] = '';
                }
            }
            $this->page_settings->update_page_settings($request, $main['id']);
            $landingmain = $this->landingmain->get_id($main['id']);
            $data = [
                'title' => $landingmain[0]->title,
                'uri' => $landingmain[0]->uri,
                'display_in_menu' => $landingmain[0]->display_in_menu,
                'single_page' => $landingmain[0]->single_page,
            ];
            if ($request['banner_desktop'] != '') {
                $data['banner_desktop'] = $request['banner_desktop'];
            }
            if ($request['banner_mobile'] != '') {
                $data['banner_mobile'] = $request['banner_mobile'];
            }
            $this->landingmain->update($main['id'], $data);
            header('location:'.base_url().'at-admin/'.$landingmain[0]->edit_url);
            exit;
        }
        $data['users'] = $this->db->get('_users')->num_rows();
        $data['selectedmenu']="inquiry_settings";
        $data['_inactive_loading'] = true;
        $data['page'] = 'contact_settings';
        $data['controller'] = $this->router->fetch_class();
        $data['_view'] = 'contact_view';
        $data['landingmain'] = $main;
        $data['settings'] = $this->page_settings->get_page_settings($main['id'], 0);
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $this->load->view('at-admin/index', $data);
    }

}
