<?php
// +----------------------------------------------------------------------
// | Yershop 开源网店系统
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: liuwei <1010422715@qq.com>
// +----------------------------------------------------------------------
namespace app\seller\controller;
use think\Controller;
use think\Db;

use think\Session;
use think\Request;
class Shop extends Home{
    public function index(){     
        if(isset($_GET['title'])){
            $map['title']  = array('like', '%'.(string)input('title').'%');
        }
		else{
		    $map="";
		}
		$res=getLists('Shop',$map,10,'id desc',"");;
	    $this->assign('res', $res);
		$this->meta_title="库存管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch(); 
	}
 public function tj(){ 
	    if ( !is_login()) {
			   $this->redirect("User/login");
		}	
	    $uid=is_login();
        if(!Db::name('shop')->where(array("uid"=>$uid,"status"=>4))->find()){
		    $this->redirect("index/shop/step1");
		}    
           $this->meta_title="首页";
		   $this->assign('meta_title', $this->meta_title);
		   $map[]=["shop_id",'=',get_shop_id()];
		  
		   $map[]=["status",'=',0];
           $info["num"]=Db::name('order')->where($map)->count();	
		   $map[]=["status",'=',1];
           $info["num1"]=Db::name('order')->where($map)->count();
		   $map[]=["status",'=',2];
           $info["num2"]=Db::name('order')->where($map)->count();
		   $map[]=["status",'=',3];
           $info["num3"]=Db::name('order')->where($map)->count(); 
		   $info["total"]=Db::name('order')->where($map)->count();
		   $this->assign('info', $info); 
		   unset($map["status"]);
		  $map[]=["status",'gt',0];
		   //统计
		 $starttime=mktime(0,0,0,date('m'),1,date('Y'));
	     $endtime=mktime(23,59,59,date('m'),date('t'),date('Y'));
            $map[]=['update_time','egt',$starttime];
            $map[]=['update_time','elt',$endtime];

			//-----------------本月订单数------------//
            $user["mouthNum"]=Db::name("order")->where($map)->count();


             $total=0;
        $data = db("order")->where($map)->select();
        foreach ($data as $k=>$val) {
			$price=$val['total_money'];
            $total +=  $price;
        }
		//-----------本月销售额------------------//
        $user["monthtotal"]=sprintf("%01.2f", $total);


			//-------------总订单数--------------------//
             unset($map['update_time']);
             $user["orderTotalNum"]=Db::name("order")->where($map)->count();


          $total=0;
        $data = db("order")->where($map)->select();
        foreach ($data as $k=>$val) {
			$price=$val['total_money'];
            $total +=  $price;
        }//-------------------总销售额------------------------//
        $user["total"]=sprintf("%01.2f", $total);
          unset($map);
		   //可提现金额
		  $map[]=["id",'=',$uid];
		   $user["money"]=Db::name('user')->where($map)->value("account");
           $this->assign('user', $user);
		 
		   //访客数 
		 $list["visitor_list"]=0;
		   $list['member_list']=0;
		      $this->assign('list', $list);
		   $date[0] = date('Y-m-d', strtotime('-6 days'));
		   $date[1] = date('Y-m-d', strtotime('-5 days'));
		   $date[2] = date('Y-m-d', strtotime('-4 days'));
		   $date[3] = date('Y-m-d', strtotime('-3 days'));
		   $date[4] = date('Y-m-d', strtotime('-2 days'));
		   $date[5] = date('Y-m-d', strtotime('-1 days'));
		   $date[6] = date('Y-m-d', time());
		   $this->assign('date', $date);
		
	     
			return $this->fetch();	
		  
	}
   public function systems(){   
	    if($_POST){
		   $Shop =new \app\admin\model\Shop;
           $res=$Shop->allowField(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
		      $this->success("更新成功！",'index');
		   }else{
			   $this->error("更新失败！");
		   } 
	  }
	  else{
		     $map["id"]=get_shop_id();
             /* 获取数据 */
             $info=Db::name('Shop')->where($map)->find();       
             $condition["pid"]=0;
             $list=Db::name("category")->where($condition)->select();
		     $this->assign('list',$list);
			 $this->assign('info', $info);
		     $this->meta_title="店铺设置";
		     $this->assign('meta_title', $this->meta_title);
	         return $this->fetch();
	   }
	}
    public function add($id=""){  
	    if($_POST){
		   $Shop = new \app\admin\model\Shop;
           // 过滤post数组中的非数据表字段数据
           $res=$Shop->allowField(true)->save($_POST);
	     if($res){
		      $this->success("新增成功！",'index');
		  }else{
			  $this->error("新增失败！");
		  } 
	   }
	  else{
		    $typelist =Db::name('types')->select();
		    $this->assign('typelist',$typelist);
		    $map['pid']  = 0;
			$list=Db::name('Shop')->where($map)->select();
		    $this->assign('list',$list);
	        return $this->fetch("Shop/edit");
	  }
	}
	 public function del(){   
	    $id=input("id");
	    $map['id']=array("in",$id);
		if(!$map["id"]){
			 $this->error("未选择数据！");
		}
		$res=Db::name("Shop")->where($map)->delete();
		if($res){
		   $this->success("删除成功！");
		}else{
			 $this->success("删除失败！");
		}
	}



}