<?php
/**
 * 营销活动控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <1070309178@qq.com>
 */
namespace Mobile\Controller;
use Think\Controller;
class PromotActController extends BaseController{
	public function _initialize(){
		parent::_initialize ();
        $this->assign('eid',MID);
	}
    //活动列表
    public function index(){
        $shop_id=get_wxsmall_shop_id();
        $map = array();
        $map['start_time'] = array('lt', NOW_TIME);
        $map['end_time'] = array('gt', NOW_TIME);
        $map['act_type'] = array('in', array(1,2,3,4));
        $map['is_pt'] = 0;//排除拼团
        $map['is_limit_time'] = 0;//限时促销
        $map['is_check'] = array("elt",1);//审核过
        $map['_string'] = 'shop_id = '.$shop_id.' or shop_id = 0';
        $list = page_list('PromotAct', $map, 'sort desc', '', '', '', 1);
//        foreach ($list['list'] as $k => $v) {
//            $list['list'][$k] = $v;
//            if ($v['act_type'] == 1) {
//                $list['list'][$k]['title_name'] = '满' . $v['min_amount'] . '减' . $v['act_type_ext'];
//            } elseif ($v['act_type'] == 2) {
//                $list['list'][$k]['title_name'] = '满' . $v['min_amount'] . '享' . ($v['act_type_ext'] / 10) . '折';
//            } else {
//                $list['list'][$k]['title_name'] = $v['act_name'];
//            }
//        }
        $this->assign('tplseo_title','优惠活动'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','优惠活动');
        $title='优惠活动';
        $description='优惠活动'.'_'.C('WEB_SITE_TITLE');
        $share_url=U('Mobile/PromotAct/index',array('eid'=>MID));
        $this->assign("title",$title);
        $this->assign("description",$description);
        $this->assign("share_url",$share_url);
        $this->assign($list);
        $this->display();
    }

    //活动内容
    public function cont($type = 0){
        $getdata = I('param.');
        $id = $getdata['id'];
        $cart_id=$getdata['cart_id'];
        $Actinfo = M('PromotAct')->where('id=' . $id)->field('id,end_time,act_type,min_amount,act_type_ext,big_img,act_name,act_good_ids,is_online,is_offline')->find();
        if ($Actinfo['act_type'] == 1) {
            $Actinfo['title_name'] = '满' . $Actinfo['min_amount'] . '减' . $Actinfo['act_type_ext'];
        } elseif ($Actinfo['act_type'] == 2) {
            $Actinfo['title_name'] = '满' . $Actinfo['min_amount'] . '享' . ($Actinfo['act_type_ext'] / 10) . '折';
        } else {
            $Actinfo['title_name'] = $Actinfo['act_name'];
        }
        //获取赠品
        if($Actinfo['act_type']==3 || $Actinfo['act_type']==4) {
            $list = D("Common/Promot")->get_gift_goods($id);
            if ($list['status'] == 1 && $list['gift_list']) {
                $gift_list = $list['gift_list'];
            }
        }
        //获取购物车的赠品
        if($cart_id){
            $map=array();
            $map['id']=$cart_id;
            $cart_info=M("Cart")->where($map)->field('sum(shop_price*goods_num) as total_price,actgoodsid')->find();
            if($cart_info['actgoodsid']){
                if(stripos($cart_info['actgoodsid'],'-')){
                    $act_id=explode('-',$cart_info['actgoodsid']);
                    $gift_id=$act_id[0];
                }else{
                    $gift_id=$cart_info['actgoodsid'];
                }
            }
        }

        $this->assign('act_info', $Actinfo);
        $this->assign('cart_ids', $cart_id);
        $this->assign('gift_id', $gift_id);
        $this->assign('gift_list', $gift_list);
        $this->assign('tplseo_title',$Actinfo['act_name'].'_'.'促销活动'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('tplseo_keywords',$Actinfo['act_name']);
        $this->assign('tplseo_description',$Actinfo['act_name']);
        $this->assign('page_title',$Actinfo['act_name']);

        //活动下的商品
        $data = D('Common/Promot')->get_active_goods($getdata);
		foreach($data['order'] as $k => $v){
			$data['order'][$k]['url'] = str_replace(".html","/eid/".MID.".html",$v['url']);
		}
		$p = empty($getdata['page']) ? 1 : $getdata['page'];
		if($data['goods_info']['count'] > $p*$data['goods_info']['listRows']){
			$data['goods_info']['show'] = 1;
		}else{
			$data['goods_info']['show'] = 0;
		}
        $title=$Actinfo['act_name'];
        $description=$Actinfo['description'];
        $goods_img=$Actinfo['big_img'];
        $share_url=U('Mobile/PromotAct/cont',array('id'=>$id,'eid'=>MID));
		// echo "<pre>";print_r($data);exit;
        $this->assign("title",$title);
        $this->assign("description",$description);
        $this->assign("goods_img",$goods_img);
        $this->assign("share_url",$share_url);
        if ($type == 1) return $data;

        $this->assign($data['goods_info']);
        $list_html = $this->fetch('goods_list');
        $this->assign('list_html', $list_html);

        $this->assign('order', $data['order']);
        $this->assign($getdata);
        $this->display();
    }
	public function get_cart_list(){
		$data = array();
		$data['status'] = 0;
        $post = I('param.');
		$shop_id = get_wxsmall_shop_id();
        $return=D('Mobile/Takeout')->get_cart_list($shop_id);
		if($return['status'] == 1){
			$data['status'] = 1;
			$cart_info = $return['data'];
		}
		$this->assign($cart_info);
		$this->assign('act_info',$post);
        $html=$this->fetch('footer_cart');
		
		$data['data'] = $html;
		$this->ajaxReturn($data);
	}
	//购物车列表
    public function cart_list($shop_id){
        if(!$shop_id){
            $cart_info=array();
        }else{
            $return=D('Mobile/Takeout')->get_cart_list($shop_id);
            if($return['status'] == 1){
                $cart_info = $return['data'];
            }
        }
        $this->assign($cart_info);
        $html=$this->fetch('footer_cart');
        $this->assign('cart_html',$html);
    }
    public function get_goods_list(){
        $return['status'] = 0;
        $data = $this->cont(1);
        if (empty($data['goods_info']['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data['goods_info']);
        $list_html = $this->fetch('goods_list');
        $return['data'] = $list_html;
		$return['show'] = $data['goods_info']['show'];
        $this->ajaxReturn($return);
    }
}
?>