<?php
/**
 * 积分商品列表
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author shixiaoli <1094186793@qq.com>
 */

namespace Mobile\Controller;
use Think\Controller;
class FightGroupController extends BaseController {
	public function _initialize(){
        parent::_initialize();
		//微信分享相关配置
		$wx = new \Common\Vendor\Jssdk();
		$signPackage = $wx->getSignPackage();
		$this->assign('signPackage',$signPackage);
	}
    public function index(){
        $map=array();
        $join='';
        $order='';
        $param=I('param.');
        if(empty($param['cat_id']))$param['cat_id']=0;
        if(empty($param['brand_id']))$param['brand_id']=0;
        $p=$param['p'];
        $catid=$param['cat_id'];
        /*排序开始*/
        $orders=D('Common/Goods')->cate_order($param);
        /*排序结束*/
        //价格分级开始
        $price_list = get_price_map($catid,$param['price']);
        if(!empty($price_list['shop_price']))$map['a.shop_price']=$price_list['shop_price'];
        //价格分级结束
        /*属性筛选开始*/
        $type_id=get_cate_type($catid,5);//类型ID
        $attr_list=D('Common/GoodsAttr')->get_attr($param,$type_id);
        if(!empty($attr_list)){
            $map['_string']=$attr_list['_string'];
        }
        //属性筛选结束
        //关键词筛选
        if(!empty($param['keyword']))$map['a.goods_name']=array('like','%'.$param['keyword'].'%');
        //分类筛选
        if(!empty($param['cat_id']))$map['a.cat_id']=$param['cat_id'];
        //品牌筛选
        if(!empty($param['brand_id']))$map['a.brand_id']=$param['brand_id'];
        //排序
        if($param[$param['order_name']]==1){$order_type='desc';
		}elseif($param[$param['order_name']]==2){$order_type='asc';}
        if(!empty($param['order_name']) && $order_type)$order=$param['order_name'].' '.$order_type;
        
        
        $goodslist=D('Common/PromotExt')->get_fight_list($map,$join,$order,$p,'',5);
        $seodata=D('Common/Goods')->format_listseotile($param['brand_id'],$catid,1,'','','',5);
		
		// echo "<pre>";print_r($goodslist);exit;
        //分类
		$where = array();
        $where['pid']=0;
        $where['status']=1;
        $cat_list=M('PurchaseCategory')->where($where)->field('id,name')->order('sort,id desc')->select();
        $this->assign('cat_list',$cat_list);
        $this->assign('catpos_title','拼团');
        $this->assign('tplseo_title',C('WEB_SITE_TITLE'));
        $this->assign('pricelist', empty($price_list['pricelist'])?array():$price_list['pricelist']);
        $this->assign($goodslist);
        $this->assign('orders',$orders);
        $this->assign('type_id',$type_id);
        $this->assign($seodata);
        // $this->assign('param',$param);
        $this->assign('catid',$catid);
        $this->assign($param);
        $this->display();
    }
    public function cont(){
        $goods_id=intval(I('get.id'));
        $order_id=intval(I('get.order_id'));
        if(!empty($order_id)){
            $goods_id = M('OrderGoods')->where('order_id='.$order_id)->getField('goods_id');
        }
        //获取商品基本信息
        $goods_info=D("Common/PromotExt")->get_fight($goods_id,4);
		$commlist = D('Dp')->get_goods('',$goods_id,0);
		//总评论个数
		$dp_count=$commlist['count_dp'];
		// print_r($goods_info);exit;
        if($goods_info['status']){
            cookie('forward',U('Mobile/FightGroup/cont',array('id'=>$goods_id,'eid'=>MID))); 
            //团长列表
            $group_id = $goods_info['data']['cid'];
			$map = array();
            $map['pt_id'] = $group_id;   //活动id
            $map['pid'] = 0;             //团长
            $map['status'] = 2;          //已确认
            $map['shipping_status'] = 6; //待成团
            $map['pay_status'] = 3;      //已付款
            $group_list = M('OrderInfo')->where($map)->order('id desc')->select();
            if(!empty($group_list)){
                foreach ($group_list as $key => $value) {
                    $mes = M('PromotAct')->where('id='.$value['pt_id'])->find();
                    //人数
                    $where = array();$where['pid']=$value['id'];
                    $where['status']=2;$where['pay_status']=3;$where['shipping_status']=6;
                    $num = M('OrderInfo')->where($where)->count();
                    $group_list[$key]['number'] = $mes['group_number']-$num-1;
                    $group_list[$key]['header_img'] = format_url(M('Member')->where('id = '.$value['uid'])->getField('header_img'));
                    //结束时间
                    $group_time_limit = $mes['group_time_limit'];
                    $begin_time=$value['confirm_time'];
                    $p_end = $begin_time + ($group_time_limit * 3600);
                    if($p_end > $mes['end_time']){
                        $group_list[$key]['end_time'] = $mes['end_time'];
                    }else{
                        $group_list[$key]['end_time'] = $begin_time + ($group_time_limit * 3600);
                    }
                    $this->assign('now_time',NOW_TIME);
                }
            }
            $data=D("Goods")->getmobile_goodscontent($goods_id);
            $goods_imgs = M("GoodsGallery")->where("goods_id=" . $goods_id)->select();
            foreach($goods_imgs as $key=>$val){
                $goods_imgs[$key]['goods_img']=$val['goods_img'];
            }
            if (empty($goods_imgs)) $goods_imgs[0]['goods_img'] = $goods_info['data']['img'];
            // //获取商品属性和规格
            // $data = D("Common/GoodsAttr")->getattr($goods_id);

			$c_map = array();$c_map['goods_id'] = $goods_id;
            $content = M('GoodsContent')->where($c_map)->field('content,seotitle')->find();
			
			$share_url = U('Mobile/FightGroup/cont',array('id'=>$goods_id,'eid'=>MID));
			$title = $goods_info['data']['goods_name'];
			$description = '拼团分享';
			$goods_img = $goods_imgs[0]['goods_img'];
			$this->assign('title',$title);
			$this->assign('description',$description);
			$this->assign('goods_img',$goods_img);
			$this->assign('share_url',$share_url);
			
			
            $price_info=D("Common/Cart")->getminprice($goods_id);
            //print_r($price_info);
            $this->assign('group_list',$group_list);
            $this->assign($data);
            $this->assign('price_info',$price_info);
            $this->assign('group_id',$group_id);
            $this->assign('order_id',$order_id);
            $this->assign('end_time',$end_time);
            // $this->assign('attr_list', $data['newarr']);//属性
            // $this->assign('attr', $data);//属性
            $this->assign('content', $content);//商品详情
            $shop_id=get_wxsmall_shop_id();
            $stock = D("Common/Cart")->shopgoodsstock($goods_id,$shop_id,$attr_key=0);
            if($stock['goods_number'] > 0){
                $goods_info['data']['is_add'] = 1;
            }else{
                $goods_info['data']['is_add'] = 0;
                $goods_info['data']['add_msg'] = $stock['info'];//3-29 zjn
            }
            $goods_info['data']['attr']=D("Common/GoodsAttr")->get_shop_attr($goods_id);
            if($goods_info['data']['attr']['combinationprice']){
                $combinationprice=json_decode($goods_info['data']['attr']['combinationprice'],true);
                $attr_price=array();
                foreach ($combinationprice as $key => $value) {
                    $attr_price[$key]=$value;
                    $attr_price[$key]['attr_price']=0.00;
                }
                $goods_info['data']['attr']['combinationprice']=json_encode($attr_price);
            }
            $this->assign('goods_info', $goods_info['data']);//商品信息
            $this->assign('attr', $goods_info['data']['attr']);
            $this->assign('commlist', $commlist);//商品评论
            $this->assign('c_count', $dp_count);//总评论数
            $this->assign('goods_imgs', $goods_imgs);//商品相册
        }else{
            $url = U('Goods/goods_cont',array('id'=>$goods_id,'eid'=>MID));
            $this->error('活动已结束',$url);
            // $this->error($goods_info['msg'],U($goods_info['url']));
        }
        $this->display();
    }
   
    public function group_join(){
        $order_id=I('get.order_id');
        // $tid = idtoname($order_id,'OrderInfo','uid'); //团长
		$where = array();
		$where['id'] = $order_id;
		$order_info = M('OrderInfo')->where($where)->field('uid,pid')->find();
		if($order_info['pid'] == 0){
			$tid = $order_info['uid']; //团长
			$orderid = $order_id;
		}else{
			$tid = idtoname($order_info['pid'],'OrderInfo','uid','id',false); //团长
			$orderid = $order_info['pid'];
		}
        cookie('forward',U('Mobile/FightGroup/group_join',array('order_id'=>$order_id,'eid'=>MID))); 
        $member_info = session('member_info');
        $uid = $member_info['id'];

        $open_appurl = 'hcb2b2c://?type=appurl&module=Group&orderid='.$order_id;//module:Group是参团页
        $this->assign('open_appurl',$open_appurl);
        //团长头像
		$where = array();
		$where['id'] = $tid;
        $img = M('Member')->where($where)->getField('header_img');
        //团员头像
        $map_son['pid'] = $orderid;
        $map_son['pay_status'] = 3;
        $team_ids = M('OrderInfo')->field('uid')->where($map_son)->select();
        if(!empty($team_ids)){
            foreach ($team_ids as $k => $value) {
                $team_ids[$k]['imgs'] = format_url(M('Member')->where('id = '.$value['uid'])->getField('header_img'));
                if(MID == $value['uid']){
                    $have_insert = 1;
                }
            }
        }
// echo "<pre>";print_r($team_ids);exit;
        $act_id = idtoname($order_id,'OrderInfo','pt_id');
        $sum_num = idtoname($act_id,'PromotAct','group_number');
        //差几人
		$where = array();
        $where['pid']=$orderid;
        $where['status']=2;
        $where['pay_status']=3;
        $where['shipping_status']=array('not in','2,7');
        $count = M('OrderInfo')->where($where)->count();
        $number = $sum_num-$count-1;
        $goods_id = M('OrderGoods')->where('order_id='.$order_id)->getField('goods_id');
        $pt_id = idtoname($order_id,'OrderInfo','pt_id');

        //价格
		$pt_map = array();$pt_map['act_id'] = $pt_id;$pt_map['style'] = 4;
        $group_price = M('PromotExt')->where($pt_map)->getField('remarks');
        $groupprice = unserialize($group_price);

        $price = idtoname($goods_id,'Goods','shop_price');
        $center = $price - $groupprice;
        $groupprice = format_decimal($groupprice);
        //相册
        $goods_imgs = M("GoodsGallery")->where("goods_id=" . $goods_id)->select();
        foreach($goods_imgs as $key=>$val){
            $goods_imgs[$key]['goods_img']=$val['goods_img'];
        }
        $goods_info=D("Common/PromotExt")->get_fight($goods_id,4);
        if (empty($goods_imgs)) $goods_imgs[0]['goods_img'] = $goods_info['data']['img'];
		$this->assign('buy_limit', $goods_info['data']['buy_limit']);
        $shop_id=get_wxsmall_shop_id();
		/*if($shop_id){
            $map=array();
            $map['id']=$shop_id;
            $shop_name=M("Shops")->where($map)->getField('name');
            $this->assign('shop_name',$shop_name);
			$this->assign('shop_id',$shop_id);
        }*/
        //属性
        // $data = D("Common/GoodsAttr")->getattr($goods_id);
        // $data=D("Goods")->getmobile_goodscontent($goods_id);
        $data=D("Goods")->getmobile_shops_goodscontent($goods_id,$shop_id);
		
		$share_url = U('Mobile/FightGroup/group_join',array('order_id'=>$orderid,'eid'=>$tid));
		$title = $data['goods_info']['goods_name'];
		$description = '拼团分享';
		$goods_img = $goods_imgs[0]['goods_img'];
		$this->assign('title',$title);
		$this->assign('description',$description);
		$this->assign('goods_img',$goods_img);
		$this->assign('share_url',$share_url);
		
        //判断是否过期
        $begin_time = idtoname($order_id,'OrderInfo','confirm_time');
        $act_map['id']=$act_id;
        $group_time_limit =  M('PromotAct')->where($act_map)->getField('group_time_limit');
        $p_end = M('PromotAct')->where($act_map)->getField('end_time');
        $end_time = $begin_time + ($group_time_limit * 3600);
        if($end_time > $p_end){
            $end_time = $p_end;
        }
        if($end_time < NOW_TIME){
            $map['id'] = $order_id;
            $ship = M('OrderInfo')->where($map)->getField('shipping_status');
            if($ship == 6){
                // $save['status'] = 10;
                // $update = M('OrderInfo')->where($map)->save($save);
                // if($update){
                    $postdata['order_id']=$order_id;
                    $postdata['admin_id']=MID;//操作日志用
                    $postdata['type']='Member';//操作日志用
                    $return=D('Common/OrderInfo')->cancel_order($postdata);
                    $goodsid = M('PromotExt')->where('act_id='.$act_id)->getField('style_ext_id');
                    if($return['status'] == 1){
                        $save['status'] = 10;
                        $update = M('OrderInfo')->where($map)->save($save);
                        $son_orders = M('OrderInfo')->where('pid='.$order_id)->field('id')->select();
                        if(!empty($son_orders)){
                            $map_order['admin_id']=MID;//操作日志用
                            $map_order['type']='Member';//操作日志用
                            foreach ($son_orders as $key => $value) {
                                $map_order['order_id']=$value['id'];
                                $updates = D('Common/OrderInfo')->cancel_order($map_order);
                                if($updates['status'] == 1){
                                    $maps['id'] = $value['id'];
                                    $saves['status'] = 10;
                                    $updates = M('OrderInfo')->where($maps)->save($saves);
                                }
                            }
                        }
                        $this->error('该团已过期',U('Mobile/FightGroup/cont',array('id'=>$goodsid,'eid'=>MID)));
                    }else{
                        $this->error('该团已过期',U('Mobile/FightGroup/cont',array('id'=>$goodsid,'eid'=>MID)));
                    }
                // }
            }else{
                $pass = 1; //过期了，但是已成团
            }
        }else{
            $this->assign('endtime',$end_time);
        }
        $this->assign('order_id',$orderid);
        $this->assign('pass',$pass);
        $this->assign('goods_id',$goods_id);
        $this->assign('team_ids',$team_ids );
        $this->assign('pt_id',$pt_id);
        $this->assign('have_insert',$have_insert);
        $this->assign('goods_imgs',$goods_imgs);
        $this->assign('groupprice',$groupprice);
        $this->assign('center',$center);
        $this->assign($data);
        $this->assign('img',$img);
        $this->assign('number',$number);
        $this->assign('tid',$tid);
        $this->assign('uid',$uid);
        $this->display();
    }
    public function get_catgoods_list(){
        $return['status']=0;
        $data = I('post.');
        $paramm['is_pt'] = 1;
        $paramm['cat_id'] = $data['cat_id'];
        $data=D("Goods")->get_goods_index($paramm);
        // var_dump($data);die;
        if(empty($data['goods_list']['list']))$this->ajaxReturn($return);
        if(!empty($data['goods_list']['list'])){
            foreach($data['goods_list']['list'] as $k=>$v){
                // $data['goods_list']['list'][$k]['img']=format_url($v['img']);
                $data['goods_list']['list'][$k]['img']=thumb($v['img'],'500','500','noimg_500_500.png');
            }
        }
        $return['status']=1;
        $return['data']=$data['goods_list']['list'];
        $this->ajaxReturn($return);
    }
}