<?php
/**
 * Daytype Management
 * @package     ci-hotel2
 * @author      Teruki Hosokawa <hosotk@gmail.com>
 * @license     New BSD License
 */

require_once (dirname(__FILE__) . "/ADMIN_Controller.php");


/**
 * 日タイプ設定
 * daytypeid = 1 は編集可能だが、削除できない。(基準の日程のため)
 * 基準以外の日程を追加、削除できる。
 */
class Daytypes extends ADMIN_Controller {
    
    function Daytypes()
    {
        parent::ADMIN_Controller();
        $this->load->model('User_model');
        $this->load->model('Hotel_model');
    }
    
    /**
     * 日程タイプ一覧
     */
    public function index($hotelid)
    {
        $this->load->library('session');

        $this->load->helper(array('form', 'url'));
        $this->db->where('status', 1);
        $this->db->where('hotelid', $hotelid);
        $query = $this->db->get('daytypes');
        $data['daytypes'] = $query->result_array();
        $this->load->vars($data);
        $this->load->view('admin/daytypes/index');
    }
    
    public function add($hotelid)
    {
        $this->load->helper(array('form', 'url'));
        
        $data['summary'] = '';
        $this->load->vars($data);
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        $this->form_validation->set_rules('daytype_name', '日タイプ名称', 'required|trim|xss_clean');
        $this->form_validation->set_rules('color', '色', 'required|trim|xss_clean');
        $this->form_validation->set_rules('rate', '基準との比率(%)', 'required|integer|trim|xss_clean');
        
        if (!$this->input->post('submit'))
        {
            $data['daytype_name'] = "";
            $data['color'] = "";
            $data['rate'] = 100;
            $this->load->vars($data);
        }
        if ($this->form_validation->run() == FALSE)
        {
            //echo "Form First Or NG";
            $this->load->view('admin/daytypes/add');
            return;
        }
        else
        {
            $daytypeid = $this->Hotel_model->getDaytypeId($hotelid);
            $data = array(
                           'hotelid' => $hotelid,
                           'daytypeid' => $daytypeid,
                           'daytype_name' => set_value('daytype_name'),
                           'color' => set_value('color'),
                           'rate' => set_value('rate'),
                           'status' => 1
                        );
            $this->db->insert('daytypes', $data);
            
            redirect('admin/daytypes/index/' . $hotelid);
        }
        
    }
    
    public function edit($hotelid, $daytypeid)
    {
        $this->load->helper(array('form', 'url'));
        
        $data['daytypeid'] = $daytypeid;
        $data['summary'] = '';
        $this->load->vars($data);
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        $this->form_validation->set_rules('daytype_name', '日タイプ名称', 'required|trim|xss_clean');
        $this->form_validation->set_rules('color', '色', 'required|trim|xss_clean');
        $this->form_validation->set_rules('rate', '基準との比率(%)', 'required|integer|trim|xss_clean');
        
        if (!$this->input->post('submit'))
        {
            $this->db->where('hotelid', $hotelid);
            $this->db->where('daytypeid', $daytypeid);
            $this->db->where('status', 1);
            $query = $this->db->get('daytypes', 1);
            if ($query->num_rows() > 0)
            {
                $row = $query->row_array();
                $data['daytype_name'] = $row['daytype_name'];
                $data['color'] = $row['color'];
                $data['rate'] = $row['rate'];
                $this->load->vars($data);
                $this->load->view('admin/daytypes/edit');
                return;
            }
            else
            {
                die();
            }
        }
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('admin/daytypes/edit');
        }
        else
        {
            $daytypeid = $this->Hotel_model->getDaytypeId();
            $data = array(
                           'daytype_name' => set_value('daytype_name'),
                           'color' => set_value('color'),
                           'rate' => set_value('rate')
                        );
            $this->db->where('hotelid', $hotelid);
            $this->db->where('daytypeid', $daytypeid);
            $this->db->update('daytypes', $data);
            
            redirect('admin/daytypes/index/' . $hotelid);
        }
    }
    
    /**
     * 日タイプの削除
     */
    public function delete($hotelid, $daytypeid)
    {
        // daytypeid = 1(基準日タイプ)は削除できない。
        if ($daytypeid == 1)
        {
            die();
        }
        
        // 日程に日タイプがあれば削除できない。
        $this->db->where('hotelid', $hotelid);
        $this->db->where('daytypeid', $daytypeid);
        $today = date("Y-m-d");
        $this->db->where('day >=', $today);
        $query = $this->db->get('day_to_daytype', 1);
        if ($query->num_rows() > 0)
        {
            die();
        }
        
        $data = array('status' => -1);
        $this->db->where('hotelid', $hotelid);
        $this->db->where('daytypeid', $daytypeid);
        $this->db->update('daytypes', $data);
        
        redirect('admin/daytypes/index/' . $hotelid);
    }
}
/* End of file daytypes.php */
