<?php
/**
 * Planprice
 * 
 * プラン別料金設定
 * 
 */
require_once (dirname(__FILE__) . "/ADMIN_Controller.php");

class Planprice extends ADMIN_Controller {

    function Planprice()
    {
        parent::ADMIN_Controller();
        $this->load->helper(array('form', 'url'));
        $this->load->model('User_model');
        if ($this->User_model->isAdmin() != true)
        {
            redirect('admin/login/index');
        }
    }
    
    public function index()
    {
        $query = $this->db->get('plans');
        foreach ($query->result() as $row)
        {
            //var_dump($row);
            echo '<a href="http://localhost/ci-hotel/admin/planprice/set/' . $row->planid . '">';
            echo $row->plan_name;
            echo '</a>' . "<br />\n";
        }
    }
    
    public function set($planid)
    {
        $this->load->helper(array('form', 'url'));
        
        $data = array();
        $data['planid'] = $planid;
        
        // submitボタンが押されている場合
        if (isset($_POST['mysubmit']))
        {
            $a_basic_price = intval($_POST['a_basic_price']);
            $b_basic_price = intval($_POST['b_basic_price']);
            $c_basic_price = intval($_POST['c_basic_price']);
            $d_basic_price = intval($_POST['d_basic_price']);
            $a_child1_price = intval($_POST['a_child1_price']);
            $b_child1_price = intval($_POST['b_child1_price']);
            $c_child1_price = intval($_POST['c_child1_price']);
            $d_child1_price = intval($_POST['d_child1_price']);
            $a_child2_price = intval($_POST['a_child2_price']);
            $b_child2_price = intval($_POST['b_child2_price']);
            $c_child2_price = intval($_POST['c_child2_price']);
            $d_child2_price = intval($_POST['d_child2_price']);
            $a_child3_price = intval($_POST['a_child3_price']);
            $b_child3_price = intval($_POST['b_child3_price']);
            $c_child3_price = intval($_POST['c_child3_price']);
            $d_child3_price = intval($_POST['d_child3_price']);
            $a_child4_price = intval($_POST['a_child4_price']);
            $b_child4_price = intval($_POST['b_child4_price']);
            $c_child4_price = intval($_POST['c_child4_price']);
            $d_child4_price = intval($_POST['d_child4_price']);
            
            $query = $this->db->get_where('planprice', array('planid' => $planid));
            if ($query->num_rows() > 0)
            {
                $this->db->where('planid', $planid);
                $this->db->update('planprice', array('a_basic_price'  => $a_basic_price));
                $this->db->update('planprice', array('b_basic_price'  => $b_basic_price));
                $this->db->update('planprice', array('c_basic_price'  => $c_basic_price));
                $this->db->update('planprice', array('d_basic_price'  => $d_basic_price));
                $this->db->update('planprice', array('a_child1_price' => $a_child1_price));
                $this->db->update('planprice', array('b_child1_price' => $b_child1_price));
                $this->db->update('planprice', array('c_child1_price' => $c_child1_price));
                $this->db->update('planprice', array('d_child1_price' => $d_child1_price));
                $this->db->update('planprice', array('a_child2_price' => $a_child2_price));
                $this->db->update('planprice', array('b_child2_price' => $b_child2_price));
                $this->db->update('planprice', array('c_child2_price' => $c_child2_price));
                $this->db->update('planprice', array('d_child2_price' => $d_child2_price));
                $this->db->update('planprice', array('a_child3_price' => $a_child3_price));
                $this->db->update('planprice', array('b_child3_price' => $b_child3_price));
                $this->db->update('planprice', array('c_child3_price' => $c_child3_price));
                $this->db->update('planprice', array('d_child3_price' => $d_child3_price));
                $this->db->update('planprice', array('a_child4_price' => $a_child4_price));
                $this->db->update('planprice', array('b_child4_price' => $b_child4_price));
                $this->db->update('planprice', array('c_child4_price' => $c_child4_price));
                $this->db->update('planprice', array('d_child4_price' => $d_child4_price));
            }
            else
            {
                $data = array(
                    'planid' => intval($planid),
                    'a_basic_price' => $a_basic_price,
                    'b_basic_price' => $b_basic_price,
                    'c_basic_price' => $c_basic_price,
                    'd_basic_price' => $d_basic_price,
                    'a_child1_price' => $a_child1_price,
                    'b_child1_price' => $b_child1_price,
                    'c_child1_price' => $c_child1_price,
                    'd_child1_price' => $d_child1_price,
                    'a_child2_price' => $a_child2_price,
                    'b_child2_price' => $b_child2_price,
                    'c_child2_price' => $c_child2_price,
                    'd_child2_price' => $d_child2_price,
                    'a_child3_price' => $a_child3_price,
                    'b_child3_price' => $b_child3_price,
                    'c_child3_price' => $c_child3_price,
                    'd_child3_price' => $d_child3_price,
                    'a_child4_price' => $a_child4_price,
                    'b_child4_price' => $b_child4_price,
                    'c_child4_price' => $c_child4_price,
                    'd_child4_price' => $d_child4_price,
                    );
                $this->db->insert('planprice', $data); 
            }
        }
        
        $this->db->where('planid', $planid);
        $query = $this->db->get('planprice', 1);
        
        if ($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
                $data['a_basic_price'] = $row->a_basic_price;
                $data['b_basic_price'] = $row->b_basic_price;
                $data['c_basic_price'] = $row->c_basic_price;
                $data['d_basic_price'] = $row->d_basic_price;
                $data['a_child1_price'] = $row->a_child1_price;
                $data['b_child1_price'] = $row->b_child1_price;
                $data['c_child1_price'] = $row->c_child1_price;
                $data['d_child1_price'] = $row->d_child1_price;
                $data['a_child2_price'] = $row->a_child2_price;
                $data['b_child2_price'] = $row->b_child2_price;
                $data['c_child2_price'] = $row->c_child2_price;
                $data['d_child2_price'] = $row->d_child2_price;
                $data['a_child3_price'] = $row->a_child3_price;
                $data['b_child3_price'] = $row->b_child3_price;
                $data['c_child3_price'] = $row->c_child3_price;
                $data['d_child3_price'] = $row->d_child3_price;
                $data['a_child4_price'] = $row->a_child4_price;
                $data['b_child4_price'] = $row->b_child4_price;
                $data['c_child4_price'] = $row->c_child4_price;
                $data['d_child4_price'] = $row->d_child4_price;
                break;
            }
        }
        
        $this->load->vars($data);
        $this->load->view('admin/planprice/set');
        
    }
}
