<?php
/** App登录控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

namespace Wxsmall\Controller;
class CouponController extends BaseController {
    /**
     * 优惠券列表
     */
    public function index(){
        $return=array('status'=>1);
        $param=I('param.');
        $data = D('Shop/CouponsGet')->coupons_list($param);
        //print_r($list);
        if($data && $data['info']){
            $list = D('Coupons')->format_coupon($data['info']); // 1-31 zjn 修改 
            $return['data']=$list['data'];
        }else{
            $return['status']=2;
            $return['msg']='没有更多数据';
        }
        $this->ajaxReturn($return);
    }

    /**
     * 优惠券详情页
     */
    public function countop_count(){
        $id=I('id');
        $info=D("Wxsmall/Coupons")->get_coupon_cont($id);
        $this->ajaxReturn($info);
    }
    /**
     * 优惠券领取
     */
    public function get_coupon_wx(){
        $param=I('post.');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $result=D("Wxsmall/Coupons")->get_coupon_wx($param);
        $this->ajaxReturn($result);
    }

    /**
     * 更新卡券
     */
    public function save_coupon(){
        $return=array('status'=>1);
        $param=I('post.');
        if(empty($param['id']) || empty($param['code'])){
            $return['status']=3;
            $return['msg']='参数错误';
            $this->ajaxReturn($return);
        }
        $user_info=$this->checkuserauth();
        $Weixin = new \Common\Vendor\WeixinCard();
        $result = $Weixin->card_code_decrypt($param['code']);
        if($result['status']==1){
            $map=$data=array();
            $map['id']=$param['id'];
            $map['uid']=$user_info['id'];
            $data['code']=$result['code'];
            $res=M("CouponsGet")->where($map)->save($data);
            if($res){
                $return['msg']='更新成功';
                $this->ajaxReturn($return);
            }else{
                $return['status']=3;
               // $return['msg']=M("CouponsGet")->getLastSql();
                $this->ajaxReturn($return);
            }
        }
        $this->ajaxReturn($result);
    }
}