<?php
/**
 * 会员信息管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */
namespace Mobile\Controller;
use Think\Controller;
class LittleShopController extends BaseController
{
    public function _initialize(){
        parent::_initialize();
        // if (!MID) {
        //     $this->redirect('Mobile/Login/index');
        // }
        // $map['id']=MID;
        // $userinfo=M('Member')->where('id='.MID)->field(true)->find();
        // $this->assign($userinfo);

    }
    public function editinfo(){
        $postdata = I('param.');
        $id=$postdata['id']; //参数
        $this->assign('id',$id);
        $info=M("LittleShop")->field(true)->find($id);
        //微信分享相关配置
        $wx = new \Common\Vendor\Jssdk();
        $signPackage = $wx->getSignPackage();
        $this->assign('signPackage',$signPackage);
        $this->assign($info);
        $this->assign('tplseo_title', $info['name'].'_'.'修改资料' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '修改资料');
        $this->display();
    }
    /**
     *小店首页
     */
    public function index(){
        $postdata = I('param.');
        if($postdata['lshop_id']){
            $shop_id=$postdata['lshop_id'];
            if($postdata['eid']){
                if(MID)$duid=MID;else $duid=0;
                if($duid==$postdata['eid']){
                    $is_show=1;
                }else{
                    $is_show=2;
                }
                $this->assign('is_show',$is_show);
            }
        }else{
            $shop_id=intval(idtoname(MID,'LittleShop','id','uid',false));
        }
        if(!$shop_id){
            //还没有开通小店 跳转 指定区域商品页面
            $this->redirect('Mobile/ProLevel/index');
        }
        $info=M('LittleShop')->field(true)->find($shop_id);
        // if(!empty($info['banner'])){
        //     $banner_list=explode(',',$info['banner']);
        // }
        $map['shop_id']=$shop_id;
        $map['is_putaway']=0;
        $map['is_show']=1;
        $goods_ids=M('LittleShopGoods')->where($map)->limit(0,8)->getField('goods_id',true);
        $this->assign('goods_ids',$goods_ids);
        $this->assign('shop_id',$shop_id);
        $this->assign('banner_list',$banner_list);
        $this->assign($info);
        $before_title=$title?$title:'我的小店';
        $this->assign('page_title',$before_title);
        $Jssdk = new \Common\Vendor\Jssdk;
        $getSignPackage = $Jssdk->getSignPackage();
        $this->assign('getSignPackage',$getSignPackage);
        $this->assign('tplseo_title',$info['name']. '_' . C('WEB_SITE_TITLE'));
        $this->display();
    }
    /*
    * 小店精选
    */
    public function choice(){
        $postdata = I('param.');
        $eid = $postdata['eid'];
        // $lshop_id = $postdata['lshop_id'];
        $lshop_id = cookie('lshop_id');
        $user_shop_id=intval(idtoname(MID,'LittleShop','id','uid',false));
        if($lshop_id){
            $shop_id=$lshop_id;
        }
        if($lshop_id == $user_shop_id){
            $this->assign('is_lshop',1);
        }else{
            $this->assign('is_lshop',0);
        }
        if(!$shop_id){
            //还没有开通小店 跳转 指定区域商品页面
            $this->redirect('Mobile/ProLevel/index');
        }
        $info=M('LittleShop')->field(true)->find($shop_id);
        $map['lg.shop_id']=$shop_id;
        $map['lg.is_putaway']=0;
        $map['lg.is_show']=1;
        // $goods_ids_show=M('LittleShopGoods')->where($map)->getField('goods_id',true);
        $goods_ids=M('LittleShopGoods')->alias('lg')->where($map)->join('LEFT JOIN __GOODS__ g ON g.id=lg.goods_id')->field('lg.goods_id')->select();
        foreach ($goods_ids as $key => $value) {
            $goods_ids_show[]=$value['goods_id'];
        }
        // echo M('LittleShopGoods')->getLastSql();echo '123';die;
        $map['lg.is_show']=0;
        $goods_ids=M('LittleShopGoods')->alias('lg')->where($map)->join('LEFT JOIN __GOODS__ g ON g.id=lg.goods_id')->field('lg.goods_id')->select();
        foreach ($goods_ids as $key => $value) {
            $goods_ids_none[]=$value['goods_id'];
        }
        // print_r($goods_ids);die;
        $this->assign('info',$info);
        $this->assign('shop_id',$shop_id);
        $this->assign('goods_ids_show',$goods_ids_show);
        $this->assign('goods_ids_none',$goods_ids_none);
        $before_title=$title?$title:'店铺精选';
        $this->assign('page_title',$before_title);
        $this->assign('tplseo_title',$info['name'].'_' . C('WEB_SITE_TITLE'));
        $this->display();
    }
    /**
     *小店列表
     */
    public function shop_goods_list(){
        $postdata = I('param.');
        $root=array();
        $map=array();
        $join='';
        if(empty($param['page']))$param['page']=1;
        $p=$param['page'];
        $map['shop_id']=$shop_id;
        $map['is_putaway']=0;
        $map['is_show']=1;
        $goods_ids=M('LittleShopGoods')->where($map)->getField('goods_id',true);
        if(!empty($goods_ids)){
            $strids=implode(',',$goods_ids);
            $where['id']=array('in',$strids);
        }
        $goods_list=D('Common/Goods')->get_goods_list(0,$where,$join,'',$p,1);
        // print_r($goods_list);exit;
        $this->assign('goods_list',$goods_list);
        $this->assign($info);
        $this->display();
    }
    /**
     *修改小店信息
     */
    public function updateinfo(){
        if(IS_POST){
            $postdata = I('param.');
            $data = D('LittleShop')->lshop_infor($postdata);
            $this->ajaxReturn($data);
        }
    }
    /**
     *图库列表
     */
    public function shop_banner_all(){
        //图库列表
        $list=M('Images')->field(true)->select();
        $this->assign('list',$list);
        $before_title=$title?$title:'轮播图中心';
        $this->assign('page_title',$before_title);
        $this->display();
    }
    /**
     *可供选商品列表
     */
    public function add_goods($type=0){
        $param=I('param.');
        $paramm=array();
        if(empty($param['json_param'])) {
            $paramm=$param;
            $this->assign('json_param', json_encode($param));
        }else {
            $json_param=json_decode(htmlspecialchars_decode($param['json_param']),true);
            $paramm=array_merge($json_param,$param);
            $this->assign('json_param', $param['json_param']);
        }
        $data=D("Goods")->get_goods_index($paramm);
        if($type==1)return $data;
        if($param['cat_id'])$title=$data['cate_name'];
        $map['shop_id']=intval(idtoname(MID,'LittleShop','id','uid',false));
        $goods_ids=M('LittleShopGoods')->where($map)->getField('goods_id',true);
        $this->assign('goods_ids',$goods_ids);
        $this->assign($param);
        $this->assign($data);
        $list_html=$this->fetch('goods_list');
        $this->assign('list_html',$list_html);
        $before_title=$title?$title:'商品列表';
        $this->assign('page_title',$before_title);
        if($param['is_lshop']){
            $this->assign('is_lshop',$param['is_lshop']);
        }else{
            $this->assign('is_lshop',1);
        }
        $this->display();
    }
    public function get_goods_list(){
        $return['status']=0;
        $data=$this->add_goods(1);
        if(empty($data['goods_list']['list']))$this->ajaxReturn($return);
        $return['status']=1;
        $this->assign($data);
        $map['shop_id']=intval(idtoname(MID,'LittleShop','id','uid',false));
        $goods_ids=M('LittleShopGoods')->where($map)->getField('goods_id',true);
        $list_html=$this->fetch('goods_list');
        $return['data']=$list_html;
        $this->ajaxReturn($return);
    }

    /**
     *（商品、轮播图）加入我的小店
     */
    public function add_myshop(){
        $postdata = I('param.');
        $shop_id=intval(idtoname(MID,'LittleShop','id','uid',false));
        $data=$postdata;
        $data['id']=$shop_id; //小店id 参数
        $data['uid']=MID;//参数
        $result = D('LittleShop')->add_sthing($data);
        $this->ajaxReturn($result);
    }

    /**
     *我的轮播图
     */
    public function my_shop_banner(){
        $postdata = I('param.');
        if($postdata['id']){
            $map['id']=$postdata['id'];//小店id
        }else{
            $map['id']=intval(idtoname(MID,'LittleShop','id','uid',false));
        }
        $strlist=M('LittleShop')->where($map)->getField('banner');
        if(!empty($strlist)){
            $list=explode(',',$strlist);
        }
        $this->assign('list',$list);
        $before_title=$title?$title:'小店轮播图';
        $this->assign('page_title',$before_title);
        $this->display();
    }

    /**
     *我的商品在售
     */
    public function my_goods_up(){
        $postdata = I('param.');$this->assign($postdata);
        if($postdata){
            $map['shop_id']=$postdata['lshop_id'];//小店id
            $order=$postdata['name'].' '.$postdata['sort'];
        }else{
            $map['shop_id']=intval(idtoname(MID,'LittleShop','id','uid',false));
        }
        if(empty($postdata['name']) || empty($postdata['sort']))$order="id desc";
        // 小店名称
        $shop_name=idtoname(MID,'LittleShop','name','uid',false);
        $map['is_putaway']=0; //上架
        $upgoods_ids=M('LittleShopGoods')->where($map)->getField('goods_id',true);
        if(!empty($upgoods_ids)){
            $upstrids=implode(',',$upgoods_ids);
            $where['id']=array('in',$upstrids);
            $upgoods_list=M('Goods')->where($where)->field('id,img,goods_name,sell_count,addtime,shop_price')->order($order)->select();
        }

        $map['is_putaway']=1; //下架
        $down_num=M('LittleShopGoods')->where($map)->count();
        unset($map['is_putaway']);
        $map['is_putaway']=0; //上架
        $up_num=M('LittleShopGoods')->where($map)->count();
        $this->assign('down_num',intval($down_num));
        $this->assign('up_num',intval($up_num));
        $this->assign('upgoods_list',$upgoods_list);
        $this->assign('shop_id',$map['shop_id']);
        $before_title=$title?$title:'我的商品';
        $this->assign('page_title',$before_title);
        $this->assign('tplseo_title',$shop_name. '_' . C('WEB_SITE_TITLE'));
        $this->display();
    }

    /**
     *我的商品下架
     */
    public function my_goods_down(){
        $postdata = I('param.');$this->assign($postdata);
        if($postdata){
            $map['shop_id']=$postdata['lshop_id'];//小店id
            $order=$postdata['name'].' '.$postdata['sort'];
        }else{
            $map['shop_id']=intval(idtoname(MID,'LittleShop','id','uid',false));
        }
        if(empty($order))$order="id desc";
        // 小店名称
        $shop_name=idtoname(MID,'LittleShop','name','uid',false);
        $map['is_putaway']=1; //下架
        $downgoods_ids=M('LittleShopGoods')->where($map)->getField('goods_id',true);
        if(!empty($downgoods_ids)){
            $downstrids=implode(',',$downgoods_ids);
            $where['id']=array('in',$downstrids);
            $downgoods_list=M('Goods')->where($where)->field('id,img,goods_name,sell_count,addtime,shop_price')->order($order)->select();
        }
        $map['is_putaway']=1; //下架
        $down_num=M('LittleShopGoods')->where($map)->count();
        unset($map['is_putaway']);
        $map['is_putaway']=0; //上架
        $up_num=M('LittleShopGoods')->where($map)->count();
        $this->assign('down_num',intval($down_num));
        $this->assign('up_num',intval($up_num));
        $this->assign('downgoods_list',$downgoods_list);
        $this->assign('shop_id',$map['shop_id']);
        $before_title=$title?$title:'我的商品';
        $this->assign('page_title',$before_title);
        $this->assign('tplseo_title',$shop_name. '_' . C('WEB_SITE_TITLE'));
        $this->display();
    }


    /**
     *从我的小店中删除轮播图
     */
    public function delete_myshop(){
        $postdata = I('param.');
        $shop_id=intval(idtoname(MID,'LittleShop','id','uid',false));
        $postdata['shop_id']=$shop_id; //小店id 参数
        $postdata['id']=$postdata['id']; //轮播图id 参数
        $data = D('LittleShop')->del_banner($postdata);
        $this->ajaxReturn($data);
    }

    /**
     *从我的小店中修改商品
     * @param $type 1：商品下架 2：是否首页推荐
     */
    public function update_myshop(){
        $postdata = I('param.');
        $data = D('LittleShop')->update_goods($postdata);
        $this->ajaxReturn($data);
    }

    /**
     *小店中心
     */
    public function my_gerenzx(){
        $shop_id=intval(idtoname(MID,'LittleShop','id','uid',false));
        if(!$shop_id)$this->redirect('Mobile/ProLevel/index');
        $info = $map = $where = array();
        $info=M('LittleShop')->field(true)->find($shop_id);
        //获取我的下级中的所有注册会员
        $where['rank_id']=1;
        $uids=get_all_child_id(MID,$where);
        if($uids)array_push($uids,MID);if(empty($uids))$uids=array(-1);
        //总营业额
        $map['pay_status']=3;
        $map['is_delete']=0;
        $map['status']=3;
        $map['type']=14;//外卖订单
        $map['uid']=array('in',$uids);
        $order_info = M('OrderInfo')->where($map)->field('sum(goods_amount) as sellmoney,count(id) as order_num')->select();
        $info['sellmoney']=$order_info[0]['sellmoney'];
        //总订单数
        $info['order_num']=$order_info[0]['order_num'];
        //商品数
        $g_map['lg.shop_id']=$shop_id;
        $g_map['lg.is_putaway']=0;
        $g_map['g.status']=1;
        $g_map['g.publish_wait']=1;
        $info['goods_num']=intval(M('LittleShopGoods')->alias('lg')->where($g_map)->join('LEFT JOIN __GOODS__ g ON g.id=lg.goods_id')->count());
        $info['header_img']=idtoname(MID,'Member','header_img','id',false);
        $this->assign('lshop_id',$shop_id);
        $this->assign('info',$info);
        $this->assign('page_title','我的小店');
        $this->assign('tplseo_title',$info['name']. '_' . C('WEB_SITE_TITLE'));
        $this->display();
    }

    //全部订单
    public function order_info($is_ajax = 0){
        $postdata=I('param.');
        //获取我的下级中的所有注册会员
        $where['rank_id']=1;
        $uids=get_all_child_id(MID,$where);
        array_push($uids,MID);
        $map['pay_status']=3;
        $map['is_delete']=0;
        $map['status']=3;
        $map['type']=14;// 外卖订单
        $map['uid']=array('in',$uids);
        $this->assign('tplseo_title','订单管理'.'_'.C('WEB_SITE_TITLE'));
        $this->assign('page_title','订单管理');
        $page_num=15;
        $page=empty($postdata['page'])?1:$postdata['page'];
        $all_order = page_list('OrderInfo',$map,'id desc',true,$page_num,'',$page);
        //格式化订单数据
        if($all_order){
            foreach($all_order['list'] as $k=>$v){
                $goods_info['all_num']=$goods_info['num']=0;
                $all_order['list'][$k]=$v;
                if($v['type']==3){
                    $all_order['list'][$k]['goods_amount']=$v['goods_amount']-$v['used_score_money'];
                }else{
                    $all_order['list'][$k]['goods_amount']=$v['goods_amount'];
                }
                $goods_map['order_id']=$v['id'];
                $goods_info['num']=M('OrderGoods')->where($goods_map)->count();
                if($goods_info['num']) {
                    $goods_info['list']=M('OrderGoods')->where($goods_map)->getField('id,goods_id,goods_name,goods_attr,goods_price,goods_number,status,goods_integral',true);
                    foreach ($goods_info['list'] as $ke => $vo) {
                        $attr = unserialize($vo['goods_attr']);
                        $goods_info['list'][$ke] = $vo;
                        $goods_info['all_num'] += $vo['goods_number'];
                        $goods_info['list'][$ke]['goods_price'] = $vo['goods_price'];
                        $goods_info['list'][$ke]['img'] = idtoname($vo['goods_id'], 'Goods', 'img');
                        $goods_info['list'][$ke]['attr_name'] = $attr['name'];
                        $goods_info['list'][$ke]['status'] = $vo['status'];
                        $goods_info['list'][$ke]['goods_number']=intval($vo['goods_number']);
                    }
                    $all_order['list'][$k]['goods']=$goods_info;
                }
            }

            if($all_order['count'] > $page_num*$page){
                $all_order['show'] = 1;
            }else{
                $all_order['show'] = 0;
            }
        }
        if ($is_ajax == 1) return $all_order;
        $this->assign($all_order);
        $this->assign($postdata);
        $list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->display();
    }
    public function get_index_list(){
        $return['status'] = 0;
        $data = $this->order_info(1);
        if (empty($data['list'])) $this->ajaxReturn($return);
        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
        $return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }

}