<?php
/**
 * Admin Res
 * @package     hotel reservation system
 * @author      Teruki Hosokawa <hosotk@gmail.com>
 * @license     New BSD Licence
 */
require_once (dirname(__FILE__) . "/ADMIN_Controller.php");

class Res extends ADMIN_Controller {

    function Res()
    {
        parent::ADMIN_Controller();
        $this->load->helper(array('form', 'url'));
        $this->load->model('User_model');
        if ($this->User_model->isAdmin() != true)
        {
            redirect('admin/login/index');
        }
        $this->load->model('Res_model');
        $this->load->model('Hotel_model');
    }
    
    /**
     * 予約一覧
     */
    public function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->db->select('reservation.resid, reservation.created, resuser.name, reservation.cin, reservation.cout, reservation.plan_num, reservation.room_num, reservation.total_price, reservation.status, reservation.basic_num, plans.plan_name');
        $this->db->from('reservation');
        $this->db->order_by('reservation.created DESC');
        $this->db->join('resuser', 'resuser.resid = reservation.resid');
        $this->db->join('plans', 'plans.planid = reservation.planid');
        $query = $this->db->get();
        
        $data['ress'] = $query->result_array();
        //var_dump($data);
        //$data['title'] = "Plans";
        //$data['headline'] = "";
        $this->load->vars($data);
        $this->load->view('admin/res/index');
    }
    
    /**
     * 予約詳細
     */
    public function detail($resid)
    {
        $this->load->helper(array('form', 'url'));
        
        $data['title'] = "Res";
        $data['headline'] = "";
        
        $this->db->select('reservation.resid, reservation.rescode, reservation.created, resuser.name, reservation.cin, reservation.cout, reservation.cinhour, reservation.memo, reservation.plan_num, reservation.room_num, reservation.total_price, reservation.status, reservation.basic_num, reservation.child1_num, reservation.child2_num, reservation.child3_num, reservation.child4_num, plans.plan_name, roomtypes.roomtype_name');
        $this->db->from('reservation');
        $this->db->order_by('reservation.created DESC');
        $this->db->join('resuser', 'resuser.resid = reservation.resid');
        $this->db->join('plans', 'plans.planid = reservation.planid');
        $this->db->join('roomtypes', 'roomtypes.roomtypeid = reservation.roomtypeid');
        $this->db->where('reservation.resid', $resid);
        $query = $this->db->get();
        if ($query->num_rows() > 0)
        {
            $row = $query->row_array();
            $data = $row;
        }
        $data['resid'] = $resid;
        $this->load->vars($data);
        
        $this->db->where('resid', $resid);
        $query = $this->db->get('resuser', 1);
        if ($query->num_rows() > 0)
        {
            $row = $query->row_array();
            $data = $row;
        }
        $this->load->vars($data);
        
        $this->load->view('admin/res/detail');
    }
    
    /**
     * 予約ステータスを変更する。
     * 変更後、予約詳細画面に遷移する。
     */
    public function changeStatus($resid, $new_status)
    {
        $this->load->helper(array('form', 'url'));
        
        $this->db->where('resid', $resid);
        $query = $this->db->get('reservation', 1);
        if ($query->num_rows() <= 0)
        {
            return;
        }
        
        $row = $query->row_array();
        $status = $row['status'];
        
        if ($new_status == 2)
        {
            /**
             * 仮予約->本予約
             */
            $this->Res_model->confirmRes($resid, $this->Hotel_model);
        }
        else if ($new_status == 0)
        {
            if ($status == 1)
            {
                // 仮予約 -> キャンセル
                $this->Res_model->cancel($resid, 2, $this->Hotel_model);
            }
            else if ($status == 2)
            {
                // 本予約 -> キャンセル
                $this->Res_model->cancel($resid, 3, $this->Hotel_model);
            }
        }
        
        redirect('admin/res/detail/' . $resid);
    }
}
/* End of file res.php */
