<?php

class News_model extends CI_Model
{

    public function select()
    {
        $this->load->database();
        $query = $this->db->query("select * from news_data order by date_n desc");
        return $query->result();
    }

    public function detail()
    {
        $this->load->database();
        $id = $this->input->get('id');

        $query = $this->db->query("select * from news_data n
        LEFT JOIN news_user u on  n.emp = u.id
        where n.id = $id");
        return $query->result();
    }

    public function view()
    {
        $this->load->database();
        $view = $this->input->get('id');
        $query = $this->db->query(" update news_data SET view = view+1 where id =$view");
      
    }

    public function login()
    {
        $this->load->database();
        $used = $this->input->post('used');
        $pwd = $this->input->post('pwd');

        $query = $this->db->query("select * from news_user where used = " . "'$used'" . " and pwd = " . "'$pwd'");

        return $query->result();

    }

    public function group()
    {
        $this->load->database();
        $query = $this->db->query("select * from news_group");
        return $query->result();

    }


// 
    public function edit()
    {
        $key = $this->input->get('id');
        $this->load->database();
        $query = $this->db->query("select * from news_data where id ='$key'");
        return $query->result();  
    }
// 

public function update()
{ 
    $this->load->library('upload');
    $this->load->database();

  $id = $this->input->post('id');


    if($_FILES['userfile1']['size'] > 0){
            $config1['upload_path'] = './files/';
            $config1['allowed_types'] = 'zip|rar|';
            $config1['encrypt_name'] = true;
            $data['upload_file'] = $this->upload->data();
            $this->upload->initialize($config1);
            $this->upload->do_upload('userfile1');
            $file_news =$this->upload->data('file_name');

            $data = array(
                'file_news' => $file_news
            );
            $this->db->where('id', $id);
            $this->db->update('news_data', $data);
           
    }  
    
    if($_FILES['userfile2']['size'] > 0){     
    $config2['upload_path'] = './images/';
    $config2['allowed_types'] = 'gif|jpg|png';
    $config2['encrypt_name'] = true;

    $data['upload_img'] = $this->upload->data();
    $this->upload->initialize($config2);
    $this->upload->do_upload('userfile2');
    $img_news =$this->upload->data('file_name');

    $data = array(
   'img_news' => $img_news
    );
   $this->db->where('id', $id);
  $this->db->update('news_data', $data);
    }
    
    $data = array(
        'group_news' => $this->input->post('group'),
        'name_news' => $this->input->post('news'),
        'det_news' => $this->input->post('editor'),
        'emp' => $this->session->userdata('id')
);
$this->db->where('id', $id);
$this->db->update('news_data', $data);

}



    public function delete()
    {
  
        $key = $this->input->get('id');
    
        $this->load->database();
        $query = $this->db->query("delete from news_data where id = '$key' ");
        

    }

    public function listedit()
    {
        if($this->input->post('group')){
            $key = $this->input->post('group');
        }else if($this->input->get('group')){
            $key = $this->input->get('group');
        }else{
            $key = 0;
        }
        $this->load->database();
        $query = $this->db->query("select g.name,d.* from news_data d LEFT join news_group g on g.id = d.group_news where d.group_news = '$key' ORDER BY d.date_n desc limit 10");
        return $query->result();
    }

    public function uplaod(){
        $this->load->library('upload');
        $this->load->database();
        $group_news = $this->input->post('group');
        $name_news = $this->input->post('news');
        $det_news = $this->input->post('editor');
        $emp = $this->session->userdata('id');
        $img;
                if($this->input->post('group')==1){
                    $img = 'ข่าวประชาสัมพันธ์.png';
                }else if($this->input->post('group')==2){
                    $img = 'ข่าวการประชุม.png';
                }else if($this->input->post('group')==3){
                    $img = 'ปชส_อบรม_สัมมนา.png';
                }else if($this->input->post('group')==4){
                    $img = 'รายงานการประชุม.png';
                }else if($this->input->post('group')==5){
                    $img = 'ข่าวประกวดราคา.png';
                }else if($this->input->post('group')==6){
                    $img = 'รอบรั้วสสจ.png';
                }else if($this->input->post('group')==7){
                    $img = 'ข่าว43แฟ้ม.png';
                }

        $data = array(
        'group_news' => $group_news,
        'name_news' => $name_news,
        'det_news' => $det_news,
        'emp' => $emp,
        'img_news' => $img
        );
        $this->db->insert('news_data', $data);
   

        if($_FILES['userfile2']['size'] > 0){
            $config2['upload_path'] = './images/';
            $config2['allowed_types'] = 'gif|jpg|png';
            $config2['encrypt_name'] = true;
            $data['upload_img'] = $this->upload->data();
            $this->upload->initialize($config2);
            $this->upload->do_upload('userfile2');
            $img_news =$this->upload->data('file_name');

          
    $this->db->query("update news_data set img_news  = '$img_news' where group_news = $group_news and name_news ='$name_news' and emp ='$emp' and  det_news='$det_news' ;");    
    }  


    if($_FILES['userfile1']['size'] > 0){
        $config1['upload_path'] = './files/';
        $config1['allowed_types'] = 'zip|rar|';
        $config1['encrypt_name'] = true;
        $data['upload_file'] = $this->upload->data();
        $this->upload->initialize($config1);
        $this->upload->do_upload('userfile1');
        $file_news =$this->upload->data('file_name');

        $file_news = $file_news;
        $this->db->query("update news_data set file_news  = '$file_news' where group_news = $group_news and name_news ='$name_news' and emp ='$emp' and  det_news='$det_news' ;");    
       
}  
 
    
    }

}
