<!--#include file=connection.asp-->
<!--#include file="inc/const.asp" -->
<!-- #include file="inc/DvADChar.asp" -->
<!--#include file="z_home_const.asp" -->
<%
dim CurItem,ItemName,rshome,CurID
dim Salefee,SalePrice,CurDate,SucFlag
dim MinPrice,CurType

founderr=false
If Dvbbs.Userid=0 Or Dvbbs.Userid="" Then 

Dvbbs.AddErrCode(6):Dvbbs.Showerr()
	founderr=true
end if

CurID=CheckStr(request("itemid"))
if isnull(curid) then
	errmsg=errmsg+"<br>"+"<li>没有指定家园物品编号！"
	founderr=true
elseif not isnumeric(curid) then
	errmsg=errmsg+"<br>"+"<li>提交的数据有错误！"
	founderr=true
else
	set rsHome=server.createobject("ADODB.Recordset")
	sql="select itemid from Home_myitems where id="&CurID
	rsHome.open sql,conn,1,1
	CurItem=rsHome(0)
	rsHome.Close
	set rshome=nothing
end if

set rs=server.createobject("adodb.recordset")

if request("StopSale")<>1 then
dim HasCnt1,HasCnt2,NeedCnt1,NeedCnt2
sql="select count(*) from home_myitems where itemid="&CurItem&" and Type>0 and Type<5"
rs.open sql,conn,1,1
if rs.eof then
  HasCnt1=0
else
  HasCnt1=rs(0)
end if

rs.close
sql="select count(*) from home_myitems where itemid="&CurItem&" and Type=0"
rs.open sql,conn,1,1
if rs.eof then
  HasCnt2=0
else
  HasCnt2=rs(0)
end if
rs.close
sql="SELECT  count(itemid) from home_scendetail a inner join home_scen b on a.scenid=b.id where b.username='"&dvbbs.membername&"' and b.isopen and a.itemid="&CurItem&" group by a.scenid,a.itemid order by count(a.itemid) desc"
rs.open sql,conn,1,1
if rs.eof then
  NeedCnt1=0
else
  NeedCnt1=rs(0)
end if
rs.close
sql="SELECT count(itemid) from home_scendetail a inner join home_scen b on a.scenid=b.id where b.username='"&dvbbs.membername&"' and not b.isopen and a.itemid="&CurItem&" group by a.scenid,a.itemid order by count(a.itemid) desc"
rs.open sql,conn,1,1
if rs.eof then
  NeedCnt2=0
else
  NeedCnt2=rs(0)
end if
rs.close
if not (HasCnt1>NeedCnt1 and HasCnt1+HasCnt2>NeedCnt2) then
  errmsg=errmsg+"<br>"+"<li>正在使用的物品不能出售！"
  founderr=true
end if
end if



sql="select  * from home_items where id="&CurItem
rs.open sql,conn,1,1
ItemName=rs("name")
CurType=(rs("type") mod 100)-1
if CurType>=5 then CurType=4
salefee=conn.execute("select top 1 sale from home_config")(0)
minprice=int(rs("price1")*conn.execute("select top 1 salemin from home_config")(0)/100+0.99)
rs.close
if request("action")="sale" then
	if isnull(Request("SalePrice")) or Request("SalePrice")="" then
		errmsg=errmsg+"<br>"+"<li>出售价格不可为空！"
		founderr=true
	else
		SalePrice=CheckStr(Request("SalePrice"))
		if not isInteger(SalePrice) then
			errmsg=errmsg+"<br>"+"<li>出售价格必须是整数！"
			founderr=true
		end if
		if clng(SalePrice)<MinPrice then
			errmsg=errmsg+"<br>"+"<li>出售价格必须大于"&MinPrice&"！"
			founderr=true
		end if
		SalePrice=clng(SalePrice)
	end if
end if
if founderr then
	call sale_head()
	call sale_Msg()
else
	CurDate=year(now())&"-"&month(now())&"-"&day(now())
	set rs=server.createobject("adodb.recordset")
	sql="select * from home_myitems where id="&CurID&" and username='"&dvbbs.MemberName&"'"
	rs.open sql,conn,1,1
	if rs.bof or rs.eof then
		if request("StopSale")=1 then
			errmsg=errmsg+"<br>"+"<li>您没有这个商品或该商品已被人购买，不能停售！"
		else
			errmsg=errmsg+"<br>"+"<li>您没有这个商品，不能出售！"
		end if
		founderr=true
	else
		if request("StopSale")=1 then
			if rs("type")<>5 then
				errmsg=errmsg+"<br>"+"<li>您尚未出售这个商品，不能停售！"
				founderr=true
			end if
		else
			if rs("type")=5 then
				errmsg=errmsg+"<br>"+"<li>您已经出售了这个商品，无须再次出售！"
				founderr=true
			end if
		end if
	end if
	rs.close
	set rs=nothing
	if request("action")="sale" then
		if int(saleprice*salefee/100+0.99)>mymoney then
			errmsg=errmsg+"<br>"+"<li>您的现金("&mymoney&" 元)不足，无法支付二手市场的手续费 "&int(saleprice*salefee/100+0.99)&" 元，出售失败！"
			founderr=true
		end if
	end if
	if founderr then
		call sale_head()
		call sale_Msg()
	else
		if request("stopsale")=1 then
			set rs=server.createobject("adodb.recordset")
			rs.open "select  * from home_myitems where username='"&dvbbs.MemberName&"' and id="&curID,conn,1,3
			salefee=int(rs("salePrice")*salefee/100+0.99)
			rs("salePrice")=0
			rs("type")=CurType
			rs.update
			rs.close
			set rs=nothing
			conn.execute("update Jauser set Jamonery=Jamonery+"&salefee&" where Jauname='"&dvbbs.MemberName&"'")
			errmsg="<li>已经取消了《<font color=blue>"&itemName&"</font>》的出售！"
			call sale_head()
			response.write "<script>"
			response.write "window.opener.document.execCommand('Refresh');"
			response.write "</script>"
			call sale_Msg()
		else
			if request("action")="sale" then
				set rs=server.createobject("adodb.recordset")
				rs.open "select  * from home_myitems where username='"&dvbbs.MemberName&"' and id="&curID,conn,1,3
				rs("salePrice")=saleprice
				rs("type")=5
				rs.update
				rs.close
				set rs=nothing
				salefee=int(saleprice*salefee/100+0.99)
				conn.execute("update JaUser set Jamonery=Jamonery-"&salefee&" where Jauname='"&dvbbs.MemberName&"'")
				errmsg="<li>已经成功地出售了《<font color=blue>"&itemName&"</font>》，等待他人的购买！"
				call sale_head()
				response.write "<script>"
				response.write "window.opener.document.execCommand('Refresh');"
				response.write "</script>"
				call sale_Msg()
			else
				call sale_head()%>
				<form action="?" method=post name=fix>
				<input type=hidden name="action" value="sale">
				<table cellpadding=3 cellspacing=1 align=center bgcolor=blue width=97%>
					<tr> 
						<th colspan=3>出售家园物品 -- <%=ItemName%></th>
					</tr>
					<tr> 
						<td class=tablebody1 valign=middle><b>出售价格：</b></td>
						<td class=tablebody1 valign=middle><input type=text name="saleprice" size=10> 元</td>
	         	<td rowspan=2 class=tablebody1 valign=middle width="84" height="84" border="0"><%
					  	if localpic=1 then
					  		response.write "<img src="""&PicPath&"0/"&right("0000"&CurItem,4)&".GIF"" border=1>"
					  	else
					  		response.write "<img src="""&PicPath&CurItem&"/00/00/"" border=1>"
					  	end if
	         	%></td>
					</tr>
					<tr> 
						<td class=tablebody1 colspan=2><b>说明</b>：<br>① 出售商品的最低价格为 <%=minPrice%> 元<br><font color=red>② 出售商品每件收取成交价格的 <%=SaleFee%> %</font></td>
					</tr>
					<tr> 
						<td class=tablebody2 valign=middle colspan=3 align=center><input type=hidden name=itemid value=<%=CurID%>><input type=Submit value="确定" name=Submit>&nbsp;<input type="button" name="close" value="关闭" onclick="window.close()"></td>
					</tr>
				</table>
				</form>
			<%end if
		end if
	end if
end if
'dvbbs.footer()

sub sale_head()%>
	<html><head>
	<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
	<title>翻新家园物品</title>
	<!--#include file=inc/forum_css.asp-->
	</head>
	<body><br>
<%end sub

sub sale_msg()%>
	<br>
	<table cellpadding=3 cellspacing=1 align=center bgcolor=blue width=97%>
		<tr align=center>
			<th width="100%">翻新家园物品信息</th>
		</tr>
		<tr>
			<td width="100%" class=tablebody1><b>操作结果：</b><br><%
				if founderr then
					response.write "<font color=red>"
					response.write errmsg
					response.write "</font>"
				else
					response.write errmsg
				end if
			%></td>
		</tr>
		<tr align=center>
			<td width="100%" class=tablebody2><a href="javascript:window.close()">[关闭窗口]</a><%if request("action")="sale" and founderr then%>&nbsp;&nbsp;<a href="javascript:history.go(-1)">[返回上页]</a><%end if%></td>
		</tr>
	</table>
<%end sub%>

