<?php
/**
 * 收藏中心
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */
namespace Mobile\Controller;
use Think\Controller;
class CollectionController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        if(!MID){
              cookie('forward',U('Mobile/'.CONTROLLER_NAME.'/'.ACTION_NAME));
              $this->redirect('Mobile/Login/index');
        }
        if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
                  session('format_url',U('Mobile/'.CONTROLLER_NAME.'/'.ACTION_NAME));
                  $this->redirect('Mobile/Login/perfect');
        }elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
                  $this->error('您已经领取过会员卡了！');exit;
        }
    }

    /**
     *商品收藏
     */
    public function favorites($type = 0){
        $page=I('param.page');
        $page=$page?$page:1;
        $collectgoods = D('Common/Member')->get_collect_goods('','','',$page,20);
        if ($type == 1) return $collectgoods;
        $this->assign($collectgoods);
        $this->assign('tplseo_title', '我的收藏' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '我的收藏');
        $this->display();
    }
    public function get_favorites_list(){
        $return['status'] = 0;
        $data = $this->favorites(1);
        if (empty($data['list'])) $this->ajaxReturn($return);
        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('favorites_list');
        $return['html'] = $list_html;
        $return['sum_page'] = $data['totalPages'];
        $this->ajaxReturn($return);
    }
    /**
     *店铺收藏
     */
    public function favorites_store($type = 0){
		$page=I('param.page');
		$page=$page?$page:1;
        $collectshops = D('Common/Member')->get_collect_shops('','','',$page);
		if ($type == 1) return $collectshops;
        $this->assign($collectshops);
        $this->assign('tplseo_title', '店铺收藏' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '店铺收藏');
        $this->display();
    }
    public function get_store_list(){
        $return['status'] = 0;
        $data = $this->favorites_store(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('store_list');
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
    /**
     *删除收藏的商品或店铺
     * @param $id 收藏的商品或店铺ID
     * @param $type 删除的是商品还是店铺
     */
    public function delete_collect($id, $type)
    {
        $data = D('Common/Member')->delete_collect($id, $type);
        $this->ajaxReturn($data);
    }
    // 微信卡券
    public function get_coupons($type=0,$use_types=1,$page=1){
        $use_type=I('use_type');
        if(!$use_type)$use_type = $use_types;
        if($use_type == 1){
            $param['status'] = 0;
        }else if($use_type == 2){
            $param['status'] = 1;
        }else if($use_type == 3){
            $param['status'] = 2;
        }
        $param['page'] = $page;
        $list = D('Mobile/Tuoke')->get_coupons($param);
        $member_info=checkuserauthtype();
        if($list['data']){
              foreach($list['data'] as $k=>$v){
                        $list['data'][$k]['reduce_cost'] = Intval($v['reduce_cost']);
                        $list['data'][$k]['least_cost'] = Intval($v['least_cost']);
                        if($v['rank_id'] == 'DISCOUNT'){
                              $list['data'][$k]['discount'] = (100-$v['discount'])/10;
                        }
                        if($v['shop_id']!='0'){
                              $list['data'][$k]['is_show'] = array_filter(explode(',',$v['shop_id']));
                        }else{
                            $list['data'][$k]['is_show'] = 0;
                        }
                $list['data'][$k]['uid'] = $member_info['id'];
              }
	}
        $list['use_type'] = $use_type;
        if($type==1)return $list; 
        $this->assign('list',$list['data']);
        $this->assign('show',$list['show']);
        $this->assign($list['num']);
        $this->assign('use_type',$use_type);
        $get_coupons_list=$this->fetch('get_coupons_list');
        $this->assign('get_coupons_list',$get_coupons_list);
        
        $this->display();
    }
    public function get_coupons_list(){
        $return['status']=0;
        $use_types = I('use_type');
        $page = I('page');
        $data=$this->get_coupons(1,$use_types,$page);
        if(empty($data))$this->ajaxReturn($return);
        $return['status']=1;
        $this->assign('list',$data['data']);
        $this->assign('use_type',$data['use_type']);
        $get_coupons_list=$this->fetch('get_coupons_list');
        $return['data']=$get_coupons_list;
        $return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    function employ(){
        $return['status'] = 1;
        $id = I('get.id');
        $memberinfo = checkuserauthtype();
        $uid = $memberinfo['id'];
        $where['uid'] = $uid;
        $where['id'] = $id;
        $get_coupons = M('CouponsGet')->where($where)->find();
        $coupons = M('Coupons')->where('id='.$get_coupons['coupons_id'])->field('description,notice,title,brand_name,use_limit')->find();
        $where_c['status'] = 1;
        $where_c['uid'] = $uid;
        $count = M('CouponsGet')->where($where_c)->count();
        if(time() < $get_coupons['begin_timestamp']){
            $return['msg'] = '还未到达有效期';
            $return['status'] = 0;
            $this->ajaxReturn($return);exit();
        }elseif (time() > $get_coupons['end_timestamp']) {
            $return['msg'] = '优惠券已过期';
            $return['status'] = 0;
            $this->ajaxReturn($return);exit();
        }
        if($count >= $coupons['use_limit']){
            $return['msg'] = '使用次数达到上限';
            $return['status'] = 0;
            $this->ajaxReturn($return);exit();
        }
        $get_coupons['begin_timestamp'] = date('Y-m-d',$get_coupons['begin_timestamp']);
        $get_coupons['end_timestamp'] = date('Y-m-d',$get_coupons['end_timestamp']);
        $get_coupons['description'] = $coupons['description'];
        $get_coupons['notice'] = $coupons['notice'];
        $get_coupons['title'] = $coupons['title'];
        $get_coupons['brand_name'] = $coupons['brand_name'];
        $get_coupons['url'] = U('Home/qrcode/index',array('data' => $get_coupons['code'],'size' => '4'));
        $return['status'] = 1;
        $return['data'] = $get_coupons;
        $this->ajaxReturn($return);
    }

    public function weixin(){
        $Weixin = new \Common\Vendor\WeixinCoupon();
        $media = $Weixin->media();
        dump($media);
    }
    public function kaquan_cont(){
        $id = I('param.id');
        if(!$id){
              $this->error('数据错误');
        }
        $map = array();$map['id'] = $id;
        $get_info = M('CouponsGet')->where($map)->field(true)->find();
        if(!$get_info){
              $this->error('数据错误');
        }
        $map = array();$map['id'] = $get_info['coupons_id'];
        $kaquan_info = M('Coupons')->where($map)->field(true)->find();
        $get_info['reduce_cost'] = Intval($get_info['reduce_cost']);
        $get_info['least_cost'] = Intval($get_info['least_cost']);
        if($get_info['rank_id'] == 'DISCOUNT'){
              $get_info['discount'] = (100-$get_info['discount'])/10;
        }
        if(!empty($kaquan_info['is_show'])){
            $get_info['is_show'] = array_filter(explode(',',$kaquan_info['is_show']));
        }else{
            $get_info['is_show'] = 0;
        }
        $now_t = NOW_TIME;
        if($get_info['status'] == 1){
            $get_info['use_type'] = 2;
        }else{
            if($get_info['end_timestamp'] > $now_t){
                $get_info['use_type'] = 1;
            }else{
                $get_info['use_type'] = 3;
            }
        }
        $this->assign('info',$get_info);
        $this->display();
    }
}