<?php
/**
 * 账户中心
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */

namespace Member\Controller;
use Think\Controller;
class AccountLogController extends BaseController {
	public function _initialize(){
		parent::_initialize ();
		$this->account_type=strtoarray(C('ACCOUNT_TYPE'));$this->assign('account_type',$this->account_type);
		$this->account_status=strtoarray(C('ACCOUNT_STATUS'));$this->assign('account_status',$this->account_status);
   	}
	/**
	   *获取未使用代金券列表
	   */
	public function redpacket(){
		$bonu_list=D('Common/Member')->redPacket(1);
		$this->assign($bonu_list);
        $this->assign('tplseo_title','未使用代金券_会员中心_'.C('WEB_SITE_TITLE'));
		$this->display();
	}
	/**
	   *已使用代金券
	   */
	public function used_redpacket(){
		$bonu_list=D('Common/Member')->redPacket(2);
		$this->assign($bonu_list);
        $this->assign('tplseo_title','已使用代金券_会员中心_'.C('WEB_SITE_TITLE'));
		$this->display();
	}
	/**
	  *已过期代金券
	  */
	public function overdue_redpacket(){
        $bonu_list=D('Common/Member')->redPacket(3);
		$this->assign($bonu_list);
        $this->assign('tplseo_title','已过期代金券_会员中心_'.C('WEB_SITE_TITLE'));
		$this->display();
	}
	/**
	  *删除我的代金券
	  */
	public function del_redpacket($id){
		$data=D('Common/Member')->delete_redPacket($id);
        $this->ajaxReturn($data);
	}
	/**
	   *获取积分列表
	   */
	public function integral(){
		$postdata=I('post.');
		$integral_list=D('Common/Member')->integral($postdata);
		$this->assign($integral_list);$this->assign($postdata);
        		$this->assign('tplseo_title','消费积分列表_会员中心_'.C('WEB_SITE_TITLE'));
		$this->display();
	}
	/**
	   *获取积分列表
	   */
	public function grade_integral(){
		$postdata=I('post.');
		$integral_list=D('Common/Member')->integral($postdata,3);
		$this->assign($integral_list);$this->assign($postdata);
        		$this->assign('tplseo_title','等级积分列表_会员中心_'.C('WEB_SITE_TITLE'));
		$this->display();
	}
	/**
	  *领取线下代金券
	  */
	public function draw_redpacket(){
		if(IS_POST){
			$return['status']=0;
			$postdata=I('param.');
			if(empty($postdata['bonus_sn'])){
				$return['msg']='该线下代金券序列号不能为空';
				$this->ajaxReturn($return);
			}
			$map['bonus_type_id']=4;
			$map['bonus_sn']=$postdata['bonus_sn'];
			$count=M('MemberBonus')->where($map)->count('id');
			if($count<=0){
				$return['msg']='该线下代金券不存在';
				$this->ajaxReturn($return);
			}
			$user_id=M('MemberBonus')->where($map)->getField('user_id');
			if($user_id){
				$return['msg']='该线下代金券已被领取';
				$this->ajaxReturn($return);
			}
			//判断是否过期
			$is_out_date=M('MemberBonus')->where($map)->getField('end_time');
			if($is_out_date<NOW_TIME){
				$return['msg']='该线下代金券已过期';
				$this->ajaxReturn($return);
			}
			$data['user_id']=$this->member_info['id'];
			$data['bonus_type_id']=5;
			$result=M('MemberBonus')->where($map)->save($data);
			if($result){
				$return['msg']='领取线下代金券成功';
				$return['status']=1;
				$return['url']=U('Member/AccountLog/redpacket');
			}else{
				$return['msg']='领取线下代金券失败';
			}
			$this->ajaxReturn($return);
		}else{
            $this->assign('tplseo_title','领取代金券_会员中心_'.C('WEB_SITE_TITLE'));
			$this->display();	
		}
	}
}
?>