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

class Dashboard extends CI_Controller {

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

        if(!isset($_SESSION['login'])){
        	
        	redirect('Welcome');
        }
    }

    public function index(){
        $this->load->model('order_model');
        $data['reports'] = $this->order_model->dash_reports();
        $this->load->view('user/dashboard', $data);
    }

    public function front_countdown(){

        $this->load->model('front_countdown_model');
        $data['results'] = $this->front_countdown_model->show_front_countdown();

        $this->load->view('user/front-countdown', $data);
    }

    public function submit_front_countdown(){
        $this->form_validation->set_rules('total_experience', 'Total Experience', 'required');
		$this->form_validation->set_rules('happy_clients', 'Happy Clients', 'required');
        $this->form_validation->set_rules('projects_done', 'Projects Done', 'required');
        
        if ($this->form_validation->run() == FALSE){

			$this->session->set_flashdata('error', 'Set Total Experience, Happy Clients, and Projects Done Field');

        	redirect("user/Dashboard/front_countdown");
                
        }
        else{
            $data = array(
                'total_experience' => $this->input->post('total_experience'),
                'happy_clients' => $this->input->post('happy_clients'),
                'projects_done' => $this->input->post('projects_done')
            );

            $this->load->model('front_countdown_model');
            $res = $this->front_countdown_model->insert_front_countdown($data);
            
            if($res == TRUE){
                $this->session->set_flashdata('success', 'Successfully Added');
                redirect("user/Dashboard/front_countdown");
            }
            else{
                $this->session->set_flashdata('error', 'Something went wrong');
                redirect("user/Dashboard/front_countdown");
            }
        }
    }

    public function update_front_countdown(){
        $this->form_validation->set_rules('total_experience', 'Total Experience', 'required');
		$this->form_validation->set_rules('happy_clients', 'Happy Clients', 'required');
        $this->form_validation->set_rules('projects_done', 'Projects Done', 'required');
        
        if ($this->form_validation->run() == FALSE){

			$this->session->set_flashdata('error', 'Set Total Experience, Happy Clients, and Projects Done Field');

        	redirect("user/Dashboard/front_countdown");
                
        }
        else{
            $data = array(
                'total_experience' => $this->input->post('total_experience'),
                'happy_clients' => $this->input->post('happy_clients'),
                'projects_done' => $this->input->post('projects_done'),
                'front_countdownid' => $this->input->post('front_countdownid')
            );

            $this->load->model('front_countdown_model');
            $res = $this->front_countdown_model->update_front_countdown($data);
            
            if($res == TRUE){
                $this->session->set_flashdata('success', 'Successfully Updated');
                redirect("user/Dashboard/front_countdown");
            }
            else{
                $this->session->set_flashdata('error', 'Something went wrong');
                redirect("user/Dashboard/front_countdown");
            }
        }
    }

    public function logout(){
		unset($_SESSION['login']);
		redirect('Login');
	}


}