<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Card extends Admin_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Card_model');
		$this->load->model('Price_model');		
	}
	/**
	 * [view 卡片信息显示]
	 * @return [type] [description]
	 */
	public function view(){
		$data = $this->cardInfo();
		$data['title'] = '卡片信息';
		$this->load->view('common/innerheader',$data);
		$this->load->view('admin/cardInfo',$data);

	}


	/**
	 * [edit 修改卡片]
	 * @return [type] [description]
	 */
	public function edit(){
		
		$data = $this->cardInfo();
		$price_default = array(
			array('duration' => 7, 'price' => 7),
			array('duration' => 30, 'price' => 30),
			array('duration' => 90, 'price' => 90),
			array('duration' => 180, 'price' => 180),
			array('duration' => 365, 'price' => 365),
		);
		$data['price_default'] = $price_default;
		$this->load->view('common/innerheader',$data);
		$this->load->view('admin/card_edit',$data);
		$this->load->view('common/footer');
	}

	/**
	 * [cardInfo 获取五折卡信息]
	 * @return [type] [数组]
	 */
	public function cardInfo()
	{
		$data = $this->Card_model->getCardInfo();
		$return_field = 'duration,price';
		$data['now_price'] = $this->Price_model->getPriceInfo($return_field);	
		return $data;
	}

	/**
	 * [store 五折卡修改保存]
	 * @return [type] [json 状态码]
	 */
	public function store(){
		$rules_config = array(
			array(
		        'field' => 'card_name',
		        'label' => '卡片名称',
		        'rules' => 'trim|required|max_length[5]'
		    ),
		    array(
		        'field' => 'card_des',
		        'label' => '卡片简介',
		        'rules' => 'trim|required'
		    ),
		    array(
		        'field' => 'level1_des',
		        'label' => '普通权益简介',
		        'rules' => 'trim|required'
		    ),
		    array(
		        'field' => 'level2_des',
		        'label' => '高级权益简介',
		        'rules' => 'trim|required'
		    )
		);

		$this->load->library('form_validation', $rules_config);

		if($this->form_validation->run() == FALSE)
        {
            print_r($this->form_validation->error_array());
        }


        //卡片进行跟新
        $data = array(
        	'client_name' => $this->input->post('client_name'),
		    'name' => $this->input->post('card_name'),
		    'color'  => $this->input->post('color'),
		    'card_des'  => $this->input->post('card_des'),
		    'level1_des' => $this->input->post('level1_des'),
		    'level2_des' => $this->input->post('level2_des'),
		    'message' => $this->input->post('message'),
		    'status' => 1
		);
        $cid = $this->input->post('id');
        $where = array('id' => $cid);
        

        $this->Card_model->update($data,$where);
        $this->Price_model->deleteBycid($cid);
        $this->Price_model->insertInfo($this->price($cid));
        redirect('admin/card/view');
	}
	/**
	 * [price 接收的价格数组进行重新整理,并判断是否含有空值]
	 * @param  [type] $pre_price [价格数组]
	 * @return [type]            [变量 或者 数组]
	 */
	public function price($cid)
	{
		$price_box = $this->input->post('price_box');
		$price = $this->input->post('price');
		$data = array();
		foreach($price_box as $duration){
			$temp['duration'] = $duration;
			$temp['price'] = $price[$duration];
			$temp['cid'] = $cid;
			$data[] = $temp;
		}
		return $data;
	}
}