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

namespace Uapp\Controller;
class CouponController extends BaseController {
    /**
     * 优惠券列表
     */
    public function index(){
        $return=array('status'=>1);
        $param=I('param.');
        $data = D('Shop/CouponsGet')->coupons_list($param);

        if($data && $data['info']){
            $list = D('Wxsmall/Coupons')->format_coupon($data['info']);
            $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(){
        $return=array('status'=>3,'msg'=>'');
        $param=I('param.');
        $id = I('get.id');
        if(!$id){$return['status']=2;$return['msg']="刷新后再试";$this->ajaxReturn($return);}
        $uid=I('uid');
        $data = D('Wxsmall/Coupons')->get_coupon_wx($param);
        if($data['status']==0 || $data['status']==2){
            $data['status']=3;
            $data['msg']=$data['msg'];
        }else{
            // 推送模板消息 领取微信优惠券
            $map['id']=$uid;
            $member_info=M("Member")->where($map)->field('mobile,username,nickname')->find();
            $add_data['to']=$uid;//$member_info['mobile'];
            if($member_info['username']){
                $add_data['user_name']=$member_info['username'];
            }elseif($member_info['nickname']){
                $add_data['user_name']=$member_info['nickname'];
            }else{
                $add_data['user_name']=$member_info['mobile'];
            }
            $add_data['first'] = '领取卡券';
            $where['id']=$id;
            $add_data['coupon_type']='优惠券';
            $add_data['card_sn']=M("Coupons")->where($where)->getField('title');
            // $add_data['goods_name']=M("Coupons")->where($where)->getField('title');
            $add_data['send_time'] = date('Y-m-d H:i:s',NOW_TIME);
            $add_data['remark'] = "尊敬的会员，您获得一张卡券，请及时领取";
            $add_data['type'] = 4;
            $add_data['content'] = "请领取卡券";
            $url=U("Mobile/Index/coupons_give",array('id'=>$id,'uid'=>$uid),true,true);
            $data['list']=sendbytheme($add_data,"MEMBER_CARE_NEW",$url);
            $data['status']=1;
        }
        // if($data['status']==2){$data['msg']="请先登录";}
        $this->ajaxReturn($data);
    }

    /**
     * 更新卡券
     */
    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);
    }
}