<?php

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

class Newscontrol extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
    }

    public function index()
    {
        $this->load->model('News_model');
        $data['query'] = $this->News_model->select();

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

    }

    public function detail()
    {
        $this->load->model('News_model');
        $this->News_model->view();
        $data['detail'] = $this->News_model->detail();
        $data['select'] = $this->News_model->select();
  
        $this->load->view('detail', $data);
    }

    public function listnews()
    {
        $this->load->model('News_model');
        $data['detail'] = $this->News_model->detail();
        $data['select'] = $this->News_model->select();
        $this->load->view('listnews', $data);
    }

    public function login()
    {

        if (!$this->session->userdata('used')) {
            if ($this->input->post('used') & $this->input->post('pwd')) {
                $this->load->model('News_model');
                $data['login'] = $this->News_model->login();
                if ($data['login']) {
                    $value;
                foreach ($data['login'] as $value) 
                    $newdata = array(
                        'id' => $value->id,
                        'used' => $this->input->post('used'),
                        'pwd' => $this->input->post('pwd'),
                        'logged_in' => true,
                    );
                    $this->session->set_userdata($newdata);
                    $id = $this->session->userdata('id');
                    $used = $this->session->userdata('used');
                    $pwd = $this->session->userdata('pwd');

                    redirect(base_url() . 'index.php/Newscontrol/listedit');

                } elseif (!$data['login']) {
                    $data['swal'] = "
                              swal({
                              type: 'error',
                              title: 'พบข้อผิดพลาด',
                              text: 'กรุณากรอกชื่อผู้ใช้หรือรหัสผ่านให้ถูกต้อง'
                          });";

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

            } else {
                $this->load->view('login');
            }
        } else {
            redirect(base_url() . 'index.php/Newscontrol/listedit');
        }

    }

    public function logout()
    {

        $this->session->unset_userdata('used');
        $this->session->unset_userdata('pwd');

        redirect(base_url() . 'index.php/Newscontrol/login');
    }

    
    public function admin()
    {

      
if($this->session->userdata('used')){
        $this->load->model('News_model');
        $data['group'] = $this->News_model->group();
        $this->load->view('admin', $data);
    }else{
        redirect(base_url() . 'index.php/Newscontrol/login');
    }
}
public function upload()
{
    $this->load->model('News_model');
    $this->News_model->uplaod();
    redirect(base_url() . 'index.php/Newscontrol/listedit?group='.$this->input->post('group'));
}


public function listedit()
{
    if($this->session->userdata('used')){
        $this->load->model('News_model');
        $data['group'] = $this->News_model->group();
        $data['listedit'] = $this->News_model->listedit();

        $this->load->view('listedit', $data);
      
    }else{
        redirect(base_url() . 'index.php/Newscontrol/login');
    }
}

public function delete()
{
    if($this->session->userdata('used')){
        $this->load->model('News_model');
        $this->News_model->delete();


        $data['group'] = $this->News_model->group();
        $data['listedit'] = $this->News_model->listedit();
       
        $this->load->view('listedit', $data);
      
    }else{
        redirect(base_url() . 'index.php/Newscontrol/login');
    }
}

public function edit()
{
    if($this->session->userdata('used')){
        $this->load->model('News_model');
        $data['edit'] = $this->News_model->edit();
        $data['group'] = $this->News_model->group();

        $this->load->view('edit', $data);
      
    }else{
        redirect(base_url() . 'index.php/Newscontrol/login');
    }
   
}

public function update()
{
    if($this->session->userdata('used')){
        $this->load->model('News_model');
        $this->News_model->update();
        redirect(base_url() . 'index.php/Newscontrol/listedit?group='.$this->input->post('group'));
    }else{
        redirect(base_url() . 'index.php/Newscontrol/login');
    }
   
}

}
