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

class Code extends Admin_Controller {


    private $expires = array('0'=>'请选择','1'=>'1个月','3'=>'3个月','6'=>'6个月','12'=>'12个月');
    private $duration = array('0'=>'请选择','7'=>'7天','30'=>'30天','90'=>'90天','180'=>'180天','365'=>'365天');

    public function __construct()
    {
        parent::__construct();
        $this->load->model('code_model');
        $this->config->load('otherconf',TRUE);

    }

    public function index($status = '',$expires='',$duration='',$sctime='')
    {
        $config['base_url'] = site_url('admin/code/index');

        $cur_status = empty($status) ? (empty($_POST) ? 0 : $_POST['status']) : $status;
        $cur_expires = empty($expires) ? (empty($_POST) ? 0 : $_POST['expires']) : $expires;
        $cur_duration = empty($duration) ? (empty($_POST) ? 0 : $_POST['duration']) : $duration;
        $cur_sctime = empty($sctime) ? (empty($_POST) ? 0 : (empty($_POST['sctime']) ? 0 : $_POST['sctime'])) : $sctime;
        //使用状态条件
        $where =array();
        if( ! empty($cur_status)){
            $where[] = "status = ".$cur_status;      
        }
        //有效期条件
        if( ! empty($cur_expires)){
            $where[]= "expires = ".$cur_expires; 
        }
        //时长条件
        if( !empty($cur_duration)){
            $where[]= "duration = ".$cur_duration;
        }
        //时间条件
        if( !empty($cur_sctime)){

            $maxtime = strtotime(date("Y-m-d",strtotime($cur_sctime)+86400));
            $mintime = strtotime($cur_sctime);

            $where[] ="create_time >= ".$mintime." and create_time <".$maxtime;        
        }
        $config['base_url'].= '/'.$cur_status.'/'.$cur_expires.'/'.$cur_duration.'/'.$cur_sctime;
        $offset = $this->uri->segment(8);

        #配置分页信息

        $config['total_rows'] = $this->code_model->count($where);

        $config['per_page'] = 50;

        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li id="first">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = '末页';
        $config['last_tag_open'] = '<li id="last">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<li id="down">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<li id="top">';
        $config['prev_tag_close'] = '</li>';
        $config['num_links'] = 5;

        $config['full_tag_open'] = '<div class="wrap"><div class="fenye"><ul>';
        $config['full_tag_close'] = '</ul></div></div>';

        

        #初始化分页类
        $this->pagination->initialize($config);

         #生成分页信息
        $data['pageinfo'] = $this->pagination->create_links();
        $limit = $config["per_page"];
        $fields = "code,duration,create_time,expires,status,username,note";


        $data['codeinfo'] = $this->code_model->listcode($where,$fields,$limit,$offset);

        //配置状态匹配
        $status = $this->config->item('codestatus','otherconf');
        $currentime = time();
        foreach ($data['codeinfo'] as $k => $v) {
            $codexpires = strtotime("+{$v['expires']} months",$v['create_time']);
            if($v['status'] == 1 && $currentime>$codexpires)
            {
                $data['codeinfo'][$k]['status'] = $status[3];
                $this->code_model->expireCode($v['code']);
            }
            else
            {
                $data['codeinfo'][$k]['status'] = $status[$v['status']];
            }
            
            $data['codeinfo'][$k]['create_time'] = date('Y-m-d',$v['create_time']);
        }

        $data['status'] = array('0'=>'全部','1'=>'未使用','2'=>'已使用','3'=>'已过期');//使用状态
        $data['cur_status'] = $cur_status;
        $data['expires'] = $this->expires;//有效期
        $data['cur_expires'] = $cur_expires;
        $data['duration'] = $this->duration;//时长
        $data['cur_duration'] = $cur_duration;
        $data['cur_sctime'] = ($cur_sctime==0)?'':$cur_sctime;

		$this->load->view('common/innerheader',$data);
		$this->load->view('admin/exchange_code',$data);
		$this->load->view('common/footer');	
    }

    public function export()
    {
        echo $this->export_service->exportcode();
    } 



    public function generate(){

        $duration = $this->input->post('duration');
        $expires = $this->input->post('expires');
        $note = $this->input->post('note', true);
        $number = intval($this->input->post('number'));

        if($duration == 0 || !isset($this->duration[$duration])){
            exit('duration error');
        }

        if($expires == 0 || !isset($this->expires[$expires])){
            exit('expires error');
        }

        if($number < 1){
            exit('number error');
        }




        

        for($i = 1; $i <= $number; $i++){
            $data['code'] = $this->code_model->generate();
            $data['duration'] = $duration;
            $data['expires'] = $expires;
            $data['note'] = $note;
            $this->code_model->insert($data);
        }

        redirect('admin/code/index');

    }
}