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

class Coupon extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->permission->is_admin();
        //$this->output->enable_profiler(TRUE);
		$this->load->model('Coupon_model');
		$this->load->library('pagination');
    }
	
	function index()
	{
		echo 'hello';
		exit;
	}
	
	//生肖促销订单列表
	function zodiac($page=1)
	{
		$sumOrders = $this->Coupon_model->get_count('zodiac');
		$config['base_url'] = site_url('coupon/zodiac');
		$config['total_rows'] = $sumOrders;
		$config['per_page'] = 20;
		$config['num_links'] = 4;
		$config['first_link'] = '&lsaquo; First';
		$config['prev_link'] = '&laquo; Prev';
		$config['next_link'] = 'Next &raquo;';
		$config['last_link'] = 'Last &rsaquo;';
		$config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($config);
		
		$data['menu'] = $this->menu();
		$data['coupon'] = $this->Coupon_model->getlist('zodiac',$page,20);
		$data['sumno'] = ($page-1) * 20;	//页序号
		$data['sumOrders'] = $sumOrders;	//订单总数
		//$data['used'] = $this->Coupon_model->get_count('used');
		//$data['invalid'] = $this->Coupon_model->get_count('invalid');
		$this->load->view('bootstrap/header', $data);
        $this->load->view('bootstrap/coupon');
        $this->load->view('bootstrap/footer');
	}
	
	function lottery($page=1)
	{
		
		$sumOrders = $this->Coupon_model->get_count('lottery');
		$config['base_url'] = site_url('coupon/lottery');
		$config['total_rows'] = $sumOrders;
		$config['per_page'] = 20;
		$config['num_links'] = 4;
		$config['first_link'] = '&lsaquo; First';
		$config['prev_link'] = '&laquo; Prev';
		$config['next_link'] = 'Next &raquo;';
		$config['last_link'] = 'Last &rsaquo;';
		$config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($config);
		
		$data['menu'] = $this->menu();
		$data['lottery'] = $this->Coupon_model->getlist('lottery',$page,20);
		$data['sumno'] = ($page-1) * 20;	//页序号
		$data['sumOrders'] = $sumOrders;	//订单总数
		//$data['used'] = $this->Coupon_model->get_count('used');
		//$data['invalid'] = $this->Coupon_model->get_count('invalid');
		$this->load->view('bootstrap/header', $data);
        $this->load->view('bootstrap/lottery');
        $this->load->view('bootstrap/footer');	
	}
	
	//更新
	function doupdate()
	{
		$aid = $this->input->post('aid');
		$doact = $this->input->post('doact');
		$statu = $this->input->post('status');
		switch ($doact)
		{
			case 'isuse':
				$field='is_use';
				$this->Coupon_model->zodiac_updatestatu($aid,$statu,$field);
				echo 'ok';
			break;
			
			case 'isvalid':
				$field='is_valid';
				$this->Coupon_model->zodiac_updatestatu($aid,$statu,$field);
				echo 'ok';
			break;
			
			case 'delete':
				$this->Coupon_model->zodiac_dodel($aid);
				echo 'ok';	
			break;
			
			default:
				echo 'error';
			break;
		}
	}
	
	//搜索
	function search()
	{
		$kwd=$this->input->post('kwds');
		$data['kwds']=$kwd;
		$searchlist = $this->Coupon_model->get_zodiac_search($kwd);
		$data['coupon'] = $searchlist;
		$data['sumno'] = 0;		//页序号
		$data['sumOrders'] = count($searchlist);	//订单总数
		$this->load->view('bootstrap/header', $data);
        $this->load->view('bootstrap/coupon');
        $this->load->view('bootstrap/footer');
	}
	
	public function menu()
	{
		return array(
			array(
			'url' =>'coupon/zodiac',
			'name' =>'生肖促销'
			),
			array(
			'url' =>'coupon/lottery',
			'name' =>'转盘抽奖'
			),
		);
	}

	//导出邮件列表
	function load_email_list($page=1)
	{
		$sumOrders = $this->Coupon_model->get_count('zodiac');
		$config['base_url'] = site_url('coupon/zodiac');
		$config['total_rows'] = $sumOrders;
		$config['per_page'] = 300;
		$config['num_links'] = 4;
		$config['first_link'] = '&lsaquo; First';
		$config['prev_link'] = '&laquo; Prev';
		$config['next_link'] = 'Next &raquo;';
		$config['last_link'] = 'Last &rsaquo;';
		$config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($config);
		
		$data = $this->Coupon_model->getlist('zodiac',$page,300);
		$this->_excel($data);
	}

	//导出数据到excel
    //$output要填充到excel的数据
    public function _excel($output=array()){
        header("Content-Type: application/vnd.ms-excel;");
        header("Content-Disposition:attachment;filename=".time().".cvs");
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public');
        $excel_str="";
        foreach ($output as $item) {
            $excel_str.=$item->useremail.",";//设置某一单元格的值
            $excel_str.="\n";//添加行结束符
        }
        //if($filetype==2) $excel_str=iconv('UTF-8','GB2312',$excel_str);//excel简体中文版使用gb2312编码
        echo "$excel_str";
    }
}