<?php
/**
 * 未登録ユーザの予約キャンセル
 *
 * 予約コード,Eメールを入力して予約をキャンセルする。
 *
 * @package     ci-hotel
 * @author      Teruki Hosokawa <hosotk@gmail.com>
 * @licence     New BSD Licence
 *
 */
require_once (dirname(__FILE__) . "/FRONT_Controller.php");

class Cancel extends FRONT_Controller {
    
    function Cancel()
    {
        parent::FRONT_Controller();
        $this->load->model('Common_model');
        $this->load->model('Res_model');
        $this->load->model('Hotel_model');
    }
    
    function index($hotelid, $resid, $rescode)
    {
        $data['hotelid'] = $hotelid;
        $data['resid'] = $resid;
        $data['rescode'] = $rescode;
        $this->load->vars($data);
        
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        $this->form_validation->set_rules('email', 'メールアドレス', 'required|valid_email|trim|xss_clean');
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('cancel/index');
            return;
        }
        else
        {
            $email = set_value('email');
            
            $this->db->where('hotelid', $hotelid);
            $this->db->where('resid', $resid);
            $this->db->where('rescode', $rescode);
            $query = $this->db->get('reservations', 1);
            if ($query->num_rows() <= 0)
            {
                $this->load->view('cancel/index');
                return;
            }
            $reservation = $query->row_array();
            
            $this->db->where('hotelid', $hotelid);
            $this->db->where('resid', $resid);
            $this->db->where('email', $email);
            $query = $this->db->get('reservation_member', 1);
            if ($query->num_rows() <= 0)
            {
                $data['error'] = array();
                $data['error'][] = 'Eメールアドレスが間違っています。';
                $this->load->vars($data);
                $this->load->view('cancel/index');
                return;
            }
            $row = $query->row_array();
            
            // OK
            
            // Eメールアドレスをsessionに保存する。
            $this->phpsession->save('cancel_email', $email);
            
            redirect('cancel/confirm/' . $hotelid . '/' . $resid . '/' . $rescode);
        }
        $this->load->view('cancel/index');
    }
    
    /**
     * キャンセル内容確認
     */
    function confirm($hotelid, $resid, $rescode)
    {
        $data['hotelid'] = $hotelid;
        $data['resid'] = $resid;
        $data['rescode'] = $rescode;
        $this->load->vars($data);
        
        $this->load->helper(array('form', 'url'));
        
        $this->db->where('hotelid', $hotelid);
        $this->db->where('resid', $resid);
        $this->db->where('rescode', $rescode);
        $this->db->where('status >', 0);
        $query = $this->db->get('reservations', 1);
        if ($query->num_rows() <= 0)
        {
            die();
        }
        
        $cancel_email = $this->phpsession->get('cancel_email');
        if (!isset($cancel_email))
        {
            var_dump($cancel_email);
            die('cancel_email is not set.');
        }
        
        if ($this->input->post('submit'))
        {
            $ret = $this->Res_model->cancel($hotelid, $resid, 1, $this->Hotel_model);
            if ($ret < 0)
            {
                log_message('info', 'cancel = ' . $ret);
            }
            $this->phpsession->clear('cancel_email');
            redirect('cancel/accept/' . $hotelid);
        }
        
        $data['contents'] = $this->Res_model->contentHtml($hotelid, $resid);
        $this->load->vars($data);
        
        $this->load->view('cancel/confirm');
    }
    
    /**
     * キャンセル完了
     */
    function accept($hotelid)
    {
        $this->load->view('cancel/accept');
    }
}

