<?php
/**
 * Guesttype Management
 * @package     ci-hotel
 * @author      Teruki Hosokawa <hosotk@gmail.com>
 * @license     New BSD License
 */
require_once (dirname(__FILE__) . "/ADMIN_Controller.php");

/**
 * ゲストタイプ設定
 * guesttypeid = 1 は編集可能だが、削除できない。(基準のゲストタイプのため)
 * 基準以外のゲストタイプを追加、削除できる。
 */
class Guesttypes extends ADMIN_Controller {
    
    function Guesttypes()
    {
        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('guesttypes');
        $data['guesttypes'] = $query->result_array();
        $this->load->vars($data);
        $this->load->view('admin/guesttypes/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('guesttype_name', '客タイプ名称', 'required|trim|xss_clean');
        //$this->form_validation->set_rules('summary', '概要', 'required|trim|xss_clean');
        $this->form_validation->set_rules('room_count', '部屋の人数としてカウントするか？', 'trim|xss_clean');
        $this->form_validation->set_rules('rate', '料金比率(%)', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('roomtype_rate', '部屋使用率(%)', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('plan_rate', 'プラン使用率(%)', 'required|integer|trim|xss_clean');

        if (!$this->input->post('submit'))
        {
            $data['guesttype_name'] = "";
            $data['summary'] = "";
            $data['rate'] = 100;
            $data['roomtype_rate'] = 100;
            $data['plan_rate'] = 100;
            $this->load->vars($data);
        }
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('admin/guesttypes/add');
            return;
        }
        else
        {
            $guesttypeid = $this->Hotel_model->getGuesttypeId($hotelid);
            $data = array(
                           'hotelid' => $hotelid,
                           'guesttypeid' => $guesttypeid,
                           'guesttype_name' => set_value('guesttype_name'),
                           'rate' => set_value('rate'),
                           'roomtype_rate' => set_value('roomtype_rate'),
                           'plan_rate' => set_value('plan_rate'),
                           'status' => 1
                        );
            $this->db->insert('guesttypes', $data);
            
            redirect('admin/guesttypes/index/' . $hotelid);
        }
    }
    
    public function edit($hotelid, $guesttypeid)
    {
        $this->load->helper(array('form', 'url'));
        
        $data['summary'] = '';
        $data['hotelid'] = $hotelid;
        $data['guesttypeid'] = $guesttypeid;
        $this->load->vars($data);
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        $this->form_validation->set_rules('guesttype_name', '客タイプ名称', 'required|trim|xss_clean');
        //$this->form_validation->set_rules('summary', '概要', 'required|trim|xss_clean');
        $this->form_validation->set_rules('rate', '基準との比率(%)', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('roomtype_rate', '部屋使用率', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('plan_rate', 'プラン使用率', 'required|integer|trim|xss_clean');
        
        if (!$this->input->post('submit'))
        {
            $this->db->where('hotelid', $hotelid);
            $this->db->where('guesttypeid', $guesttypeid);
            $this->db->where('status', 1);
            $query = $this->db->get('guesttypes', 1);
            if ($query->num_rows() <= 0)
            {
                die('oops');
            }
            $row = $query->row_array();
            $data['guesttype_name'] = $row['guesttype_name'];
            $data['summary'] = $row['summary'];
            $data['rate'] = $row['rate'];
            $data['roomtype_rate'] = $row['roomtype_rate'];
            $data['plan_rate'] = $row['plan_rate'];
            $this->load->vars($data);
            
            $this->load->view('admin/guesttypes/edit');
            return;
        }
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('admin/guesttypes/edit');
        }
        else
        {
            $data = array(
                           'guesttype_name' => set_value('guesttype_name'),
                           'summary' => set_value('color'),
                           'rate' => set_value('rate'),
                           'roomtype_rate' => set_value('roomtype_rate'),
                           'plan_rate' => set_value('plan_rate'),
                        );
            $this->db->where('hotelid', $hotelid);
            $this->db->where('guesttypeid', $guesttypeid);
            $this->db->update('guesttypes', $data);
            
            redirect('admin/guesttypes/index/' . $hotelid);
        }
    
    }
    
    public function delete($hotelid, $guesttypeid)
    {
        // daytypeid = 1(基準客タイプ)は削除できない。
        if ($guesttypeid == 1)
        {
            die();
        }
        
        // プランに客タイプがあれば削除できない。
        $this->db->where('hotelid', $hotelid);
        $this->db->where('guesttypeid', $guesttypeid);
        $query = $this->db->get('plan_to_guesttype', 1);
        if ($query->num_rows() > 0)
        {
            die();
        }
        
        $data = array('status' => -1);
        $this->db->where('hotelid', $hotelid);
        $this->db->where('guesttypeid', $guesttypeid);
        $this->db->update('guesttypes', $data);
        
        redirect('admin/guesttypes/index/' . $hotelid);
    }
}
/* End of file guesttypes.php */
