<?php
if (!defined('ROOT')) exit('Can\'t Access !'); 
class mod_shop extends module_class{
 	function main(){
       $typeid = $_GET['typeid'];
	   $where = "where id>'0' ";
	   $wd = $_POST['wd'];
	   if($typeid) $where .= "and typeid='{$typeid}'";
	   if($wd&&$wd!='请输入关键词') $where .= "and (name like '%{$wd}%' or content like '%{$wd}%')";
	   $pagetotal = $this->pagetotal = $this->mysql->counts("select * from {$this->pre}shop $where");
	   $this->pageclass($pagetotal,10);
	   $this->shop = $this->mysql->getarr("select * from {$this->pre}shop $where order by checked desc,id desc limit ".$this->page->start.",".$this->page->size);  
	   $this->hotshop = $this->mysql->getarr("select * from {$this->pre}shop order by checked desc,id desc limit 0,5");  
	   $this->type = $this->mysql->getarr("select * from {$this->pre}shoptype order by `typeorder` asc");
	}
	
 	function show(){
	   $this->shop = $this->mysql->select_one("select * from {$this->pre}shop where id='{$_GET[id]}' ");  
	   if(!is_array($this->shop))  $this->message('go_back','对不起，该信息不存在');
	   $this->pics = array_filter(explode('|',$this->shop['pics']));
	   $this->hotshop = $this->mysql->getarr("select * from {$this->pre}shop order by checked desc,id desc limit 0,5");  
	   $this->type = $this->mysql->getarr("select * from {$this->pre}shoptype order by `typeorder` asc");
	}
	
 	function buy(){
       $id = $_GET['id'];
	   $this->shop = $this->mysql->select_one("select * from {$this->pre}shop where id='{$id}' ");
	   if(!is_array($this->shop)) $this->message('go_back','对不起，该商品不存在');
	   $this->pics = array_filter(explode('|',$this->shop['pics']));
	   if(submit()){
		  $howmoney = $this->shop['money'];
		  if($this->member['money']<$howmoney) $this->message('go_back','对不起，您的现金不足');
		  $user['money'] = $this->member['money']-$howmoney;
		  $this->mysql->update("{$this->pre}user",$user,"uid='{$this->member[uid]}'"); //扣除现金
		  $order['orderid'] = date('Ymdhis').$this->member['uid'];
		  $order['message'] = "{$howmoney}元现金";
		  $order['shopid'] = $this->shop['id'];
		  $order['shopname'] = $this->shop['name'];
		  $order['checked'] = 0;
		  $order['uid'] = $this->member[uid];
		  $order['addtime'] = time();
		  $order['yourname'] = $_POST['yourname'];
		  $order['dianhua'] = $_POST['dianhua'];
		  $order['shouhuodizhi'] = $_POST['shouhuodizhi'];
          $this->mysql->insert("{$this->pre}order",$order); //写入订单
		  $moneylog['uid'] = $this->member['uid'];
		  $moneylog['addtime'] = time();
		  $moneylog['content'] = "购买商品扣除{$howmoney}元现金，订单号{$order['orderid']}";
		  $moneylog['lognum'] = "-".$howmoney;
          $this->mysql->insert("{$this->pre}log",$moneylog);
		  $this->buyok = true;
	   }
	}
	
 	function shopcart(){
       $id = $_GET['id'];
	   $this->shop = $this->mysql->select_one("select * from {$this->pre}shop where id='{$id}' ");
	   if(!is_array($this->shop)) $this->message('go_back','对不起，该商品不存在');
	   $arr['shopid'] = $_GET['id'];
	   $arr['uid'] = $this->member['uid'];
	   $arr['addtime'] = time();
	   $this->mysql->insert("{$this->pre}shopcart",$arr);
	}
	
}
?>