<?php
include_once("$config[webroot]/module/buy/includes/buy_class.php");
//===================================================
//报价提交
if(!empty($_POST['ajax']))
{
	$id=$_POST['id']*1;
	if(empty($buid))
		exit('uid');//未登录
	if($id)
	{
		$db->query('select valid_time from '.BUY.' where id='.$id.' limit 1');
		$valid_time=$db->fetchField('valid_time');
		$num=(strtotime(date($valid_time))+24*60*60)-time();
		if($num<0)
			exit('timeout'); //报价结束
		$db->query('select count(*) as num from '.BUY.' where id='.$id.' limit 1');
		$num=$db->fetchField('num');
		if(empty($num))
			exit('nobuy'); //求购已经删除
		$db->query('select count(*) as num from '.BUYQPRO.' where buy_id='.$id.' and seller_id='.$buid.' limit 1');
		$num1=$db->fetchField('num');
		if($num1)
			exit('es'); //已经报价了
		//----------------------
		foreach($_POST['pname'] as $key=>$v)
		{
			$con[]=array($v,$_POST['number'][$key],$_POST['unit'][$key],$_POST['price'][$key],$_POST['bprice'][$key]);
		}
		$con=serialize($con);
		$flag=$db->query("insert into  ".BUYQPRO."(buy_id,seller_id,con)  values('$id','$buid','$con')");
		//--------------------插入消息提示
		if($flag)
		{
			$db->query('select userid,title from '.BUY.' where id='.$id.' limit 1');
			$re=$db->fetchRow();
			$userid=$re['userid'];
			$title=$re['title'];//求购名称
			$fromInfo=NULL;
			$date=date("Y-m-d H:i");
			$sql="insert into ".FEEDBACK."
				  (touserid,sub,con,date,msgtype)
				  VALUES
				  ('$userid','您求购的[$title]有新报价','<a href=\"main.php?m=buy&s=admin_buy_list&id=$id\">点此查看</a>','$date','3')";

			$res=$db->query($sql);
		}
		if($flag)
		exit('ok');	//报价成功
	}
	exit('error');	//未知错误
}
//===========================================
if(!empty($_GET["id"])&&is_numeric($_GET["id"]))
	$id=$_GET["id"];
else
	msg($config['weburl'].'/404.php');

$buy=new buy();
$de=$buy->space_buy_detail($id);
//-----------------------------
$ifpay=isset($_SESSION["IFPAY"])?$_SESSION['IFPAY']:0;
if(!empty($config['infotype_'.$ifpay]))
	$de['view_right']=true;
else
	$de['view_right']=false;
$tpl->assign("de",$de);

$tpl->assign("relbuy",$buy->company_other_buy($de['userid'],8));

//======================================================
include_once("module/company/includes/company_class.php");
$user=new user();
$tpl->assign("cominfo",$user->get_cominfo($de['userid']));

//================导航，keyword，description=================
$parcat=substr($de['catid'],0,4);
$sql="select catid,cat,ext_table from ".OCAT." where catid='$parcat'";
$db->query($sql);
$ocats=$db->fetchRow();
$tpl->assign("pcat",$ocats);
$ext_table=$ocats['ext_table'];

if(!empty($ocats['cat']))
{
	$ks.=$ocats['cat'];
	$guide.="<a href='$config[weburl]/?m=buy&s=buy&id=$parcat'>".$ocats['cat']."</a>&raquo;";
}
if(strlen($de['catid'])>=6)
{	
	$parcat=substr($de['catid'],0,6);
	$sql="select catid,cat,ext_table from ".OCAT." where catid='$parcat'";
	$db->query($sql);
	$ocats=$db->fetchRow();
	$ext_table=$ocats['ext_table'];
	if(!empty($ocats['cat']))
	{
		$ks.=','.$ocats['cat'];
		$guide.="<a href='$config[weburl]/?m=buy&s=buy&id=$parcat'>".$ocats['cat']."</a>&raquo;";
	}
}
if(strlen($de['catid'])>=8)
{	
	$parcat=substr($de['catid'],0,8);
	$sql="select catid,cat,ext_table from ".OCAT." where catid='$parcat'";
	$db->query($sql);
	$ocats=$db->fetchRow();
	$ext_table=$ocats['ext_table'];
	if(!empty($ocats['cat']))
	{
		$ks.=','.$ocats['cat'];
		$guide.="<a href='$config[weburl]/?m=buy&s=buy&id=$de[catid]'>".$ocats['cat']."</a>&raquo;";
	}
}
$tpl->assign("guide",$guide.$de['title']);
$tpl->assign("province",get_province());
//-------------------------------------Seo config
$ar1=array('[catname]','[title]','[keyword]','[des]');
$ar2=array($ocats['cat'],$de['title'],$de['keywords'],strip_tags($de['con']));
$config['title']=str_replace($ar1,$ar2,$config['title3']);
$config['keyword']=str_replace($ar1,$ar2,$config['keyword3']);
$config['description']=str_replace($ar1,$ar2,$config['description3']);
//==================================================
$tpl->assign("current","buy");
include_once("footer.php");
$out=tplfetch("buy_detail.htm");
?>