<?php

class Content extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function upload()
    {
        $config['upload_path'] = FCPATH . '/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 1024;
        $config['max_width'] = 1024;
        $config['max_height'] = 768;
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('userfile')) {
            $this->response(null, 500, $this->upload->display_errors('', ''));
        } else {
            $uploaded = $this->upload->data();
            $uploaded['url'] = $_SERVER['HTTP_ORIGIN'] . '/uploads/' . $uploaded['file_name'];
            $this->response($uploaded);
        }
    }

    public function dptime()
    {

    }

    public function config()
    {
        $this->load->model('config_model');
        if ($this->input->method() == 'post') {
            if ($this->param('type') == 'dptime') {
                $this->load->model('life_model');
                $this->db->where('idx', $this->param('postid'))->update($this->life_model->table, array('is_display' => 'N', 'display_date' => $this->param('dptime')));
                $config = $this->config_model->findByTypeAndPostId($this->param('type'), $this->param('postid'))->get()->row();
                if($config) {
                    if($this->db->where('Id', $config->Id)->update($this->config_model->table, array(
                        'PostId' => $this->param('postid'),
                        'Type' => $this->param('type'),
                        'DpTime' => $this->param('dptime')
                    ))){
                        $this->response(array('updated' => $this->db->affected_rows()));
                    } else {
                        $this->response(null, 402, 'failed to update');
                    }
                }else {                    
                    if($this->db->insert($this->config_model->table, array(
                        'PostId' => $this->param('postid'),
                        'Type' => $this->param('type'),
                        'DpTime' => $this->param('dptime')
                    ))){
                        $this->response(array('inserted' => $this->db->insert_id()));
                    } else {
                        $this->response(null, 402, 'failed to svae');
                    }
                }
            } else if ($this->param('id')) {
                $config = $this->config_model->findByTypeAndNameAndPageAndOrderNum($this->param('type'),
                    $this->param('name'), $this->param('page'), $this->param('order'))->get();
                if ($config->num_rows() && $config->row()->Id != $this->param('id')) {
                    $this->response(null, 401, '삭제먼저');
                } else if ($this->db->where('Id', $this->param('id'))->update($this->config_model->table, array(
                    'PostId' => $this->param('postid'),
                    'Type' => $this->param('type'),
                    'Name' => $this->param('name'),
                    'Page' => $this->param('page'),
                    'OrderNum' => $this->param('order'),
                    'Headline' => $this->param('headline'),
                    'Title' => $this->param('title'),
                    'Thumbnail' => $this->param('thumbnail'),
                ))) {
                    $this->response(array('inserted' => $this->db->insert_id()));
                } else {
                    $this->response(null, 402, 'failed to svae');
                }
            } else if ($this->config_model->findByTypeAndNameAndPageAndOrderNum($this->param('type'), $this->param('name'),
                $this->param('page'), $this->param('order'))->count_all_results()) {
                $this->response(null, 401, '삭제먼저');
            } else {
                if ($this->db->insert($this->config_model->table, array(
                    'PostId' => $this->param('postid'),
                    'Type' => $this->param('type'),
                    'Name' => $this->param('name'),
                    'Page' => $this->param('page'),
                    'OrderNum' => $this->param('order'),
                    'Headline' => $this->param('headline'),
                    'Title' => $this->param('title'),
                    'Thumbnail' => $this->param('thumbnail'),
                ))) {
                    $this->response(array('inserted' => $this->db->insert_id()));
                } else {
                    $this->response(null, 402, 'failed to svae');
                }
            }
        } else if ($this->input->method() == 'delete') {
            $this->db->where('Id', $this->param('id'))->delete($this->config_model->table);
            if ($this->db->affected_rows()) {
                $this->response(null, 200, '삭제성공');
            } else {
                $this->response(null, 401, '삭제실패');
            }
        } else {
            if ($configQuery = $this->config_model->findByTypeAndNameAndPage($this->param('type'), $this->param('name'), $this->param('page'))->order_by('OrderNum')->get()->result()) {
                $this->response($configQuery);
            } else {
                $this->response(array(), 200);
            }
        }
    }
}
