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

namespace Uapp\Controller;
class FightGroupController extends BaseController {
    public function cont(){
        $return=array('status'=>1);
        $param=I('param.');
        $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');
        }

        if(empty($goods_id)){
            $return['status']=3;
            $return['msg']="参数错误";
            $this->ajaxReturn($return);
        }
        //获取商品基本信息
        $goods_info=D("Common/PromotExt")->get_fight($goods_id);
        //评论列表
        $commlist = D('Mobile/Dp')->get_goods('',$goods_id,0);
        //总评论个数
        $dp_count=$commlist['count_dp'];
        if($goods_info['status']){
            //团长列表
            $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['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;
                    
                    $img= format_url(M('Member')->where('id = '.$value['uid'])->getField('header_img'));
                    $img=str_replace('https','http',$img);
                    if(!$img)$img='../../image/header_img.gif';
                    $group_list[$key]['header_img'] =$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);
                    }
                    $group_list[$key]['user_name']=show_user_name($value['uid']);
                }
            }
            $goods_imgs = M("GoodsGallery")->where("goods_id=" . $goods_id)->select();
            foreach($goods_imgs as $key=>$val){
                $img=format_url($val['goods_img']);
                $img=str_replace('https','http',$img);
                $goods_imgs[$key]['goods_img']=$img;
            }
            if (empty($goods_imgs)) $goods_imgs[0]['goods_img'] = $goods_info['data']['img'];
            //获取商品属性和规格
            $data = D("Common/GoodsAttr")->get_shop_attr($goods_id);

            $c_map = array();$c_map['goods_id'] = $goods_id;
            $goods_img = $goods_imgs[0]['goods_img'];
            if($param['uid']!='false' && !empty($param['uid']) && $param['uid'] && $param['uid']!=false){
                $price_info=D('Common/Takeout')->get_min_price($param['shop_id'],$goods_id);
                $goods_info['data']['price_info']=$price_info;
            }
            $goods_info['data']['og_count']=floatval($goods_info['data']['og_count']);
            if($goods_info['data']['remarks']){
                $goods_info['data']['min_price']=$goods_info['data']['remarks'];
                $goods_info['data']['remarks']=format_price($goods_info['data']['remarks']);
            }
            $map=array();
            $map['goods_id']=$goods_id;
            $content=M("GoodsContent")->where($map)->getField('content');
            $str_content=$this->format_content($content);
            $goods_info['data']['content']=$str_content;
            // $content= D('Wxsmall/Takeout')->get_goods_content($param);
            // $goods_info['data']['content']=$content['data'];
            if($commlist['dp_list']){
                foreach($commlist['dp_list'] as $key=>$val){
                    $commlist['dp_list'][$key]['addtime']=time_format($val['addtime']);
                    $img=format_url($val['img']);
                    $img=str_replace('https','http',$img);
                    if(!$img)$img='../../image/user-header.png';
                    $commlist['dp_list'][$key]['img']=$img;
                    if($val['pl_img']){
                        foreach ($val['pl_img'] as $k => $value) {
                            $img=format_url($value);
                            $img=str_replace('https','http',$img);
                            $commlist['dp_list'][$key]['pl_img'][$k]=$img;
                        }
                    }
                    if(empty($val['username'])){
                        $map=array();
                        $map['id']=$val['uid'];
                        $member_info=M("Member")->where($map)->field('nickname,mobile')->find();
                        if($member_info['nickname']){
                            $commlist['dp_list'][$key]['username']=$member_info['nickname'];
                        }else if($member_info['mobile']){
                            $commlist['dp_list'][$key]['username']=$member_info['mobile'];
                        }else{
                            $commlist['dp_list'][$key]['username']='';
                        }
                    }
                }
                if($commlist['dp_group']['preset_list']){
                    foreach($commlist['dp_group']['preset_list'] as $key=>$val){
                        $commlist['dp_group']['preset_list'][$key]=$val."(".get_tag_num($val,1,$goods_id).")";
                    }
                }
            }
            $return['goods_info']=$goods_info['data'];
            $return['goods_info']['goods_imgs']=$goods_imgs;
            $return['commlist']=$commlist;
            $return['dp_count']=$dp_count?$dp_count:0;
            $return['goods_attrs']=$data;
            $return['group_list']=$group_list;
            $return['time']=NOW_TIME;
            if($param['uid']!='false' && !empty($param['uid']) && $param['uid'] && $param['uid']!=false){
                $share_url = geturl('Mobile/FightGroup/cont',array('id'=>$goods_id,'eid'=>$param['uid']),true,true);
            }else{
                $share_url = geturl('Mobile/FightGroup/cont',array('id'=>$goods_id,'eid'=>0),true,true);
            }
            $return['share_url']=$share_url;
            
        }else{
            $return['status']=3;
            $return['msg']="活动已结束";
            $this->ajaxReturn($return);
        }
        $this->ajaxReturn($return);
    }

    public function group_join(){
        $return=array('status'=>1);
        $param=I('param.');
        $order_id=I('get.order_id');
        // $user_info=$this->checkuserauth();
        $uid=$param['uid'];
        // $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'];
        }

        $open_appurl = 'hcb2b2c://?type=appurl&module=Group&orderid='.$order_id;//module:Group是参团页

        //团长头像
        $where = array();
        $where['id'] = $tid;
        $img = M('Member')->where($where)->getField('header_img');
        $return['main_img']=$img;
        //团员头像
        $map_son['pid'] = $orderid;
        $map_son['pay_status'] = 3;
        $team_ids = M('OrderInfo')->field('uid')->where($map_son)->select();
        $have_insert = 0;
        if(!empty($team_ids)){
            foreach ($team_ids as $k => $value) {
                $map=array();
                $map['id']=$value['uid'];
                $img = format_url(M('Member')->where($map)->getField('header_img'));
                $img=str_replace('https','http',$img);
                if(!$img)$img='../../image/header_img.gif';
                $team_ids[$k]['imgs'] =$img;
                if($uid == $value['uid']){
                    $have_insert = 1;
                }
            }
        }
        $act_id = idtoname($order_id,'OrderInfo','pt_id');
        $sum_num = idtoname($act_id,'PromotAct','group_number');
        //差几人
        $where = array();
        $where['pid']=$orderid;
        $where['status']=array('in','2,3');
        $where['pay_status']=3;
        $where['shipping_status']=array('in','1,3,4,5,6');
        $count = M('OrderInfo')->where($where)->count();
        $number = $sum_num-$count-1;
        $map=array();
        $map['order_id']=$order_id;
        $goods_id = M('OrderGoods')->where($map)->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){
            $img=format_url($val['goods_img']);
            $img=str_replace('https','http',$img);
            $goods_imgs[$key]['goods_img']=$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();

        //判断是否过期
        $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);
                            }
                        }
                    }
                    $return['status']=3;
                    $return['msg']='该团已过期';
                    $this->ajaxReturn($return);
                }else{
                    $return['status']=3;
                    $this->ajaxReturn($return);
                }
                // }
            }else{
                $pass = 1; //过期了，但是已成团
            }
        }
        $newarr=0;
        $goods_attr_data = D('Common/GoodsAttr')->getattr($goods_id);
        if($goods_attr_data['newarr']){
            $newarr=1;
        }
        $return['sum_num']=$sum_num;
        $return['newarr']=$newarr;
        $goods_info['data']['min_price']=$goods_info['data']['remarks'];
        $return['goods_attrs']=$data;
        $return['goods_img']=$goods_imgs;
        $return['goods_info']=$goods_info['data'];
        $return['tid']=$tid;
        $return['order_id']=$order_id;
        $return['team_ids']=$team_ids;
        $return['pass']=$pass;
        $return['have_insert']=$have_insert;
        $return['groupprice']=$groupprice;
        $return['center']=$center;
        $return['number']=$number;
        $return['end_time']=$end_time;
        $return['uid']=$uid;
        $return['pt_id']=$pt_id;
        $share_url = geturl('Mobile/FightGroup/group_join',array('order_id'=>$order_id,'eid'=>$uid),true,true);
        $return['share_url']=$share_url;
        $return['cols_xuzhi_c']='<div class="group-title">拼团须知</div><ul><li>•邀请好友开团</li><li>•人满分别发货</li><li>•人不满将退款</li></ul>';
        $return['clos_ptxuzhi_c']='<div class="notice-pop-title">如何参加拼团</div><ul class="notice-pop-list"><li>• 开团或参加别人的团</li><li>• 在规定的时间内，邀请好友参团</li><li>• 达到拼团人数，分别给团长和团员发货</li><li>• 未达到拼团人数，货款将自动返还</li></ul>';
        $this->ajaxReturn($return);
    }
    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);
        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);
    }
}