<?php 
defined('IN_DESTOON') or exit('Access Denied');
login();
require DT_ROOT.'/module/'.$module.'/common.inc.php';
$MG['mall_limit'] > -1 or dalert(lang('message->without_permission_and_upgrade'), 'goback');
$MTYPE = get_type('mall-'.$_userid);
require DT_ROOT.'/include/post.func.php';
include load($module.'.lang');
include load('my.lang');
require MD_ROOT.'/mall.class.php';
$gameid = isset($gameid) ? intval($gameid) : $catid;
$do = new mall($moduleid);
	function pei($username) {
		global $db;
		$row= $db->get_one("SELECT * FROM {$db->pre}member WHERE username='$username'");
		$money = $row['money'];
		$ye = $money-5;
		if($money>5){
			$db->get_one("UPDATE {$db->pre}member SET money='$ye' WHERE username='$username'");
			$num = mysql_affected_rows ();
			$db->query("INSERT INTO {$db->pre}finance_record (username,amount,balance,addtime,reason,bank) VALUES ('$username','-5','$ye','$DT_TIME','无货赔付押金冻结','系统')");
			return 1;
			}else{
				return 0;
				} 
	}
if(in_array($action, array('add', 'edit'))) {
	$FD = cache_read('fields-'.substr($table, strlen($DT_PRE)).'.php');
	if($FD) require DT_ROOT.'/include/fields.func.php';
	isset($post_fields) or $post_fields = array();
	//$CP = $MOD['cat_property'];
    require DT_ROOT.'/include/property.func.php';
    $PPT = property_condition(gameid($gameid), $gtid);
	isset($post_ppt) or $post_ppt = array();
}

$sql = $_userid ? "username='$_username'" : "ip='$DT_IP'";
$limit_used = $limit_free = $need_password = $need_captcha = $need_question = $fee_add = 0;
if(in_array($action, array('', 'add'))) {
	$r = $db->get_one("SELECT COUNT(*) AS num FROM {$table} WHERE $sql AND status>1");
	$limit_used = $r['num'];
	$limit_free = $MG['mall_limit'] > $limit_used ? $MG['mall_limit'] - $limit_used : 0;
}
if(check_group($_groupid, $MOD['group_refresh'])) $MOD['credit_refresh'] = 0;

switch($action) {
	case 'add':
		if($MG['mall_limit'] && $limit_used >= $MG['mall_limit']) dalert(lang($L['info_limit'], array($MG[$MOD['module'].'_limit'], $limit_used)), $_userid ? $MODULE[2]['linkurl'].$DT['file_my'].'?mid='.$mid : $MODULE[2]['linkurl'].$DT['file_my']);
		if($MG['day_limit']) {
			$today = $today_endtime - 86400;
			$r = $db->get_one("SELECT COUNT(*) AS num FROM {$table} WHERE $sql AND addtime>$today");
			if($r && $r['num'] >= $MG['day_limit']) dalert(lang($L['day_limit'], array($MG['day_limit'])), $_userid ? $MODULE[2]['linkurl'].$DT['file_my'].'?mid='.$mid : $MODULE[2]['linkurl'].$DT['file_my']);
		}

		if($MG['mall_free_limit'] >= 0) {
			$fee_add = ($MOD['fee_add'] && (!$MOD['fee_mode'] || !$MG['fee_mode']) && $limit_used >= $MG['mall_free_limit'] && $_userid) ? dround($MOD['fee_add']) : 0;
		} else {
			$fee_add = 0;
		}
		$fee_currency = $MOD['fee_currency'];
		$fee_unit = $fee_currency == 'money' ? $DT['money_unit'] : $DT['credit_unit'];
		$need_password = $fee_add && $fee_currency == 'money';
		$need_captcha = $MOD['captcha_add'] == 2 ? $MG['captcha'] : $MOD['captcha_add'];
		$need_question = $MOD['question_add'] == 2 ? $MG['question'] : $MOD['question_add'];
		$could_elite = check_group($_groupid, $MOD['group_elite']) && $MOD['credit_elite'] && $_userid;
		$could_color = check_group($_groupid, $MOD['group_color']) && $MOD['credit_color'] && $_userid;

		if($submit) {
			if($fee_add && $fee_add > ($fee_currency == 'money' ? $_money : $_credit)) dalert($L['balance_lack']);
			if($need_password && !is_payword($_username, $password)) dalert($L['error_payword']);

			if(!$_userid) {
				if(strlen($post['company']) < 4) dalert($L['type_company']);
				if($AREA) {
					if(!isset($AREA[$post['areaid']])) dalert($L['type_area']);
				} else {
					if(!$post['areaid']) dalert($L['type_area']);
				}
				if(strlen($post['truename']) < 4) dalert($L['type_truename']);
				if(strlen($post['mobile']) < 7) dalert($L['type_mobile']);
			}

			if($MG['add_limit']) {
				$last = $db->get_one("SELECT addtime FROM {$table} WHERE $sql ORDER BY itemid DESC");
				if($last && $DT_TIME - $last['addtime'] < $MG['add_limit']) dalert(lang($L['add_limit'], array($MG['add_limit'])));
			}
			$msg = captcha($captcha, $need_captcha, true);
			if($msg) dalert($msg);
			$msg = question($answer, $need_question, true);
			if($msg) dalert($msg);

			if($do->pass($post)) {
				$CAT = get_cat($post['catid']);
				if(!$CAT || !check_group($_groupid, $CAT['group_add'])) dalert(lang($L['group_add'], array($CAT['catname'])));
				$post['addtime'] = $post['level'] = $post['fee'] = 0;
				$post['style'] = $post['template'] = $post['note'] = $post['filepath'] = '';
				if(!$IMVIP && $MG['uploadpt']) $post['thumb1'] = $post['thumb2'] = '';
				$need_check =  $MOD['check_add'] == 2 ? $MG['check'] : $MOD['check_add'];
				$post['status'] = get_status(3, $need_check);
				$post['hits'] = 0;
				$post['username'] = $_username;

				if($could_elite && isset($elite) && $post['thumb'] && $_credit > $MOD['credit_elite']) {
					$post['level'] = 1;
					credit_add($_username, -$MOD['credit_elite']);
					credit_record($_username, -$MOD['credit_elite'], 'system', lang($L['credit_record_elite'], array($MOD['name'])), $post['title']);
				}

				if($could_color && $color && $_credit > $MOD['credit_color']) {
					$post['style'] = $color;
					credit_add($_username, -$MOD['credit_color']);
					credit_record($_username, -$MOD['credit_color'], 'system', $L['title_color'], '['.$MOD['name'].']'.$post['title']);
				}
                
				 //赔付交易判断
				 if($post_fields['pei']==1){
				  pei($_username); //执行押金暂扣
				  //返还结果判断
				  /***************pei*********/
				   if(pei($_username)!=1){  //结果为假时
				   $msg = "您好，您的账户余客不足，请先充值后再发布赔付商品！";
					$forward = '';
					dmsg($msg);
				 }else{  //押金暂扣成功执行发布
				   if($FD) fields_check($post_fields);
			       property_check($post_ppt);
				   $do->add($post);
				   if($FD) fields_update($post_fields, $table, $do->itemid);
			       property_update($post_ppt, $moduleid, gameid($post['catid']), $do->itemid);
				   if($MOD['show_html'] && $post['status'] > 2) $do->tohtml($do->itemid);
				   $msg = "您好，商品发布成功能，已从您的余额账户扣费冻结5元赔付押金！"; 
					if(gtid_type($post_fields[gtid])==1 || $post_fields['plays']==1){  //商品类型判断，如果为账号商品，执行下一步
					$forward = $MODULE[2]['linkurl'].$DT['file_my'].'?mid='.$mid.'&action=zh&itemid='.$do->itemid;	
					$msg = "商品信息提交成功，请填写账号信息！";	
					}else{ //如果为普通商品，则直接发布
					$forward = $MODULE[2]['linkurl'].$DT['file_my'].'?mid='.$mid.'&status='.$post['status'];
					$msg = $post['status'] == 2 ? $L['success_check'] : $L['success_add'];
					}
					dalert($msg, '', 'parent.window.location="'.$forward.'"');  //弹出提示	   
				   }
				   /*****pen--- end---*/
				 }else{ //非赔付商品
				if($FD) fields_check($post_fields);
			    property_check($post_ppt);
				$do->add($post);
				if($FD) fields_update($post_fields, $table, $do->itemid);
			    property_update($post_ppt, $moduleid, gameid($post['catid']), $do->itemid);
				if($MOD['show_html'] && $post['status'] > 2) $do->tohtml($do->itemid);
				//$msg = $post['status'] == 2 ? $L['success_check'] : $L['success_add'];	
					//set_cookie('dmsg', $msg);
					if(gtid_type($post_fields[gtid])==1 || $post_fields['plays']==1){
					$forward = $MODULE[2]['linkurl'].$DT['file_my'].'?mid='.$mid.'&action=zh&itemid='.$do->itemid;
					$msg = "商品信息提交成功，请填写账号信息！";	
					}else{
					$forward = $MODULE[2]['linkurl'].$DT['file_my'].'?mid='.$mid.'&status='.$post['status'];
					$msg = $post['status'] == 2 ? $L['success_check'] : $L['success_add'];
					}
				    dalert($msg, '', 'parent.window.location="'.$forward.'"');
					 
					 }

			 
					/////////////////////////
					if($fee_add) {
					if($fee_currency == 'money') {
						money_add($_username, -$fee_add);
						money_record($_username, -$fee_add, $L['in_site'], 'system', lang($L['credit_record_add'], array($MOD['name'])), 'ID:'.$do->itemid);
					} else {
						credit_add($_username, -$fee_add);
						credit_record($_username, -$fee_add, 'system', lang($L['credit_record_add'], array($MOD['name'])), 'ID:'.$do->itemid);
					}
				}
                $js = '';
				if(isset($post['sync_sina']) && $post['sync_sina']) $js .= sync_weibo('sina', $moduleid, $do->itemid);
				if(isset($post['sync_qq']) && $post['sync_qq']) $js .= sync_weibo('qq', $moduleid, $do->itemid);
				if(isset($post['sync_qzone']) && $post['sync_qzone']) $js .= sync_weibo('qzone', $moduleid, $do->itemid);
				//$js .= 'window.onload=function(){parent.window.location="'.$forward.'";}';
				
			} else {
				dmsg($do->errmsg, ($need_captcha ? reload_captcha() : '').($need_question ? reload_question() : ''));
			}
		} else {
			if($itemid) {
				$MG['copy'] && $_userid or dalert(lang('message->without_permission_and_upgrade'), 'goback');
				$do->itemid = $itemid;
				$r = $do->get_one();
				if(!$r || $r['username'] != $_username) message();
				extract($r);
				$thumb = $thumb1 = $thumb2 = '';
			} else {
				foreach($do->fields as $v) {
					$$v = '';
				}
				$content = '';
				$catid = 0;
			}
			$item = array();
			$mycatid_select = type_select('mall-'.$_userid, 0, 'post[mycatid]', $L['type_default']);
		}
	break;
	case 'zh':
	if($submit) {
		$itemid = intval($itemid);
		$game_user = strip_tags($game_user);
		$game_pass = strip_tags($game_pass);
		$game_name = strip_tags($game_name);
		$iphone = strip_tags($iphone);
		$email = strip_tags($email);
		$email_pass = strip_tags($email_pass);
		$identity = intval($identity);
		$id_pic = strip_tags($id_pic);
		$security = strip_tags($security);
		$tname = strip_tags($tname);
		//$post = dhtmlspecialchars($post);
		$db->query("INSERT INTO {$db->pre}mall_zhanghao (itemid,game_user,game_pass,game_name,iphone,email,email_pass,identity,tname) VALUES ('$itemid','$game_user','$game_pass','$game_name','$iphone','$email','$email_pass','$identity','$tname')");
		$zh_msg = $db->insert_id();
		if($zh_msg>0){
			$msg = "发布成功，请等待客服人员审核！";
			$forward = $MODULE[2]['linkurl'].$DT['file_my'].'?mid='.$mid.'&status=2';
		}else{
			$msg = "发布失败,请检查内容！";
			$forward = $MODULE[2]['linkurl'].$DT['file_my'].'?mid='.$mid.'&action=zh&itemid='.$itemid;
		}
		dalert($msg, '', 'parent.window.location="'.$forward.'"');
	}
	break;
	case 'edit':
		$itemid or message();
		$do->itemid = $itemid;
		$item = $do->get_one();
		if(!$item || $item['username'] != $_username) message();

		if($MG['edit_limit'] < 0) message($L['edit_refuse']);
		if($MG['edit_limit'] && $DT_TIME - $item['addtime'] > $MG['edit_limit']*86400) message(lang($L['edit_limit'], array($MG['edit_limit'])));

		if($submit) {
			if($do->pass($post)) {
				$CAT = get_cat($post['catid']);
				if(!$CAT || !check_group($_groupid, $CAT['group_add'])) dalert(lang($L['group_add'], array($CAT['catname'])));
				$post['addtime'] = timetodate($item['addtime']);
				$post['level'] = $item['level'];
				$post['fee'] = $item['fee'];
				$post['style'] = $item['style'];
				$post['template'] = $item['template'];
				$post['filepath'] = $item['filepath'];
				$post['note'] = $item['note'];
				if(!$IMVIP && $MG['uploadpt']) {
					$post['thumb1'] = $item['thumb1'];
					$post['thumb2'] = $item['thumb2'];
				}
				$need_check =  $MOD['check_add'] == 2 ? $MG['check'] : $MOD['check_add'];
				$post['status'] = get_status($item['status'], $need_check);
				$post['hits'] = $item['hits'];
				$post['username'] = $_username;
				if($FD) fields_check($post_fields);
				 property_check($post_ppt);
				if($FD) fields_update($post_fields, $table, $do->itemid);
				 property_update($post_ppt, $moduleid, $post['catid'], $do->itemid);
				$do->edit($post);
				set_cookie('dmsg', $L['success_edit']);
				//dalert('', '', 'parent.window.location="'.$forward.'"');
			} else {
				//dalert($do->errmsg);
			}
		} else {
			extract($item);
			$mycatid_select = type_select('mall-'.$_userid, 0, 'post[mycatid]', $L['type_default'], $mycatid);
		}
	break;
	case 'delete':
		$MG['delete'] or message();
		$itemid or message();
		$itemids = is_array($itemid) ? $itemid : array($itemid);
		foreach($itemids as $itemid) {
			$do->itemid = $itemid;
			$item = $do->get_one();
			if(!$item || $item['username'] != $_username) message();
			$do->recycle($itemid);
		}
		dmsg($L['success_delete'], $forward);
	break;
	case 'refresh':
		$MG['refresh_limit'] > -1 or dalert(lang('message->without_permission_and_upgrade'), 'goback');
		$itemid or message($L['select_goods']);
		$itemids = $itemid;
		$s = $f = 0;
		foreach($itemids as $itemid) {
			$do->itemid = $itemid;
			$item = $do->get_one();
			$could_refresh = $item && $item['username'] == $_username;
			if($could_refresh && $MG['refresh_limit'] && $DT_TIME - $item['edittime'] < $MG['refresh_limit']) $could_refresh = false;
			if($could_refresh && $MOD['credit_refresh'] && $MOD['credit_refresh'] > $_credit) $could_refresh = false;
			if($could_refresh) {
				$do->refresh($itemid);
				$s++;
				if($MOD['credit_refresh']) $_credit = $_credit - $MOD['credit_refresh'];
			} else {
				$f++;
			}			
		}
		if($MOD['credit_refresh'] && $s) {
			$credit = $s*$MOD['credit_refresh'];
			credit_add($_username, -$credit);
			credit_record($_username, -$credit, 'system', lang($L['credit_record_refresh'], array($MOD['name'])), lang($L['refresh_total'], array($s)));
		}
		$msg = lang($L['refresh_success'], array($s));
		if($f) $msg = $msg.' '.lang($L['refresh_fail'], array($f));
		dmsg($msg, $forward);
	break;
	case 'onsale':
		$itemid or message($L['select_goods']);
		$itemids = $itemid;
		foreach($itemids as $itemid) {
			$do->itemid = $itemid;
			$item = $do->get_one();
			if($item && $item['username'] == $_username) $do->onsale($itemid);		
		}
		dmsg($L['success_onsale'], $forward);
	break;
	case 'unsale':
		$itemid or message($L['select_goods']);
		$itemids = $itemid;
		foreach($itemids as $itemid) {
			$do->itemid = $itemid;
			$item = $do->get_one();
			if($item && $item['username'] == $_username) $do->unsale($itemid);		
		}
		dmsg($L['success_unsale'], $forward);
	break;
	default:
	case 'relate_del':
		$itemid or message($L['select_goods']);
		$do->itemid = $itemid;
		$M = $do->get_one();
		($M && $M['status'] == 3) or message($L['select_goods']);
		$id = isset($id) ? intval($id) : 0;
		$id or message($L['select_goods']);
		$do->itemid = $id;
		$A = $do->get_one();
		$do->relate_del($M, $A);
		dmsg($L['success_delete'], '?mid='.$mid.'&itemid='.$itemid.'&action=relate');
	break;
	case 'relate_add':
		$relate_name = isset($relate_name) ? htmlspecialchars(trim($relate_name)) : '';
		$relate_name or message($L['mall_relate_name'] );
		$itemid or message($L['select_goods']);
		$do->itemid = $itemid;
		$M = $do->get_one();
		($M && $M['status'] == 3) or message($L['select_goods']);
		$id = isset($id) ? intval($id) : 0;
		$id or message($L['select_goods']);
		$do->itemid = $id;
		$A = $do->get_one();
		($A && $A['status'] == 3 && $A['username'] == $M['username']) or message($L['select_goods']);
		if($itemid != $id) $do->relate_add($M, $A, $relate_name);
		dmsg($L['success_add'], '?mid='.$mid.'&itemid='.$itemid.'&action=relate');
	break;
	case 'relate':
		$itemid or message($L['select_goods']);
		$do->itemid = $itemid;
		$M = $do->get_one();
		($M && $M['status'] == 3) or message($L['select_goods']);
		if($submit) {
			$relate_name = isset($relate_name) ? htmlspecialchars(trim($relate_name)) : '';
			$relate_name or message($L['mall_relate_name'] );
			$do->relate($M, $post, $relate_name);
			dmsg($L['success_update'], '?mid='.$mid.'&itemid='.$itemid.'&action=relate');
		} else {
			$lists = $do->relate_list($M);
		}
	break;
	default:
		$pagesize = 8;
		$offset = ($page-1)*$pagesize;
		$sorder  = $L['mall_orderby'];
		$dorder  = array($MOD['order'], 'edittime DESC', 'edittime ASC', 'addtime DESC', 'addtime ASC', 'price DESC', 'price DESC', 'orders DESC', 'orders ASC', 'sales DESC', 'sales ASC', 'amount DESC', 'amount ASC', 'comments DESC', 'comments ASC', 'hits DESC', 'hits ASC');
		isset($order) && isset($dorder[$order]) or $order = 0;
		$status = isset($status) ? intval($status) : 3;
		in_array($status, array(1, 2, 3, 4)) or $status = 3;
		$mycatid = isset($mycatid) ? ($mycatid === '' ? -1 : intval($mycatid)) : -1;
		$minprice = isset($minprice) ? dround($minprice) : '';
		$minprice or $minprice = '';
		$maxprice = isset($maxprice) ? dround($maxprice) : '';
		$maxprice or $maxprice = '';
		$minorders = isset($minorders) ? intval($minorders) : '';
		$minorders or $minorders = '';
		$maxorders = isset($maxorders) ? intval($maxorders) : '';
		$maxorders or $maxorders = '';
		$minsales = isset($minsales) ? intval($minsales) : '';
		$minsales or $minsales = '';
		$maxsales = isset($maxsales) ? intval($maxsales) : '';
		$maxsales or $maxsales = '';
		$minamount = isset($minamount) ? intval($minamount) : '';
		$minamount or $minamount = '';
		$maxamount = isset($maxamount) ? intval($maxamount) : '';
		$maxamount or $maxamount = '';
		$mincomments = isset($mincomments) ? intval($mincomments) : '';
		$mincomments or $mincomments = '';
		$maxcomments = isset($maxcomments) ? intval($maxcomments) : '';
		$maxcomments or $maxcomments = '';
		$mycat_select = type_select('mall-'.$_userid, 0, 'mycatid', $L['type_default'], $mycatid, '', $L['type_my']);
		$order_select  = dselect($sorder, 'order', '', $order);

		$condition = "username='$_username' AND status=$status";
		if($keyword) $condition .= " AND keyword LIKE '%$keyword%'";
		if($catid) $condition .= $CAT['child'] ? " AND catid IN (".$CAT['arrchildid'].")" : " AND catid=$catid";
		if($mycatid >= 0) $condition .= " AND mycatid=$mycatid";
		if($minprice)  $condition .= " AND price>=$minprice";
		if($maxprice)  $condition .= " AND price<=$maxprice";
		if($minorders)  $condition .= " AND orders>=$minorders";
		if($maxorders)  $condition .= " AND orders<=$maxorders";
		if($minsales)  $condition .= " AND sales>=$minsales";
		if($maxsales)  $condition .= " AND sales<=$maxsales";
		if($minamount)  $condition .= " AND amount>=$minamount";
		if($maxamount)  $condition .= " AND amount<=$maxamount";
		if($mincomments)  $condition .= " AND comments>=$mincomments";
		if($maxcomments)  $condition .= " AND comments<=$maxcomments";

		$timetype = strpos($MOD['order'], 'add') !== false ? 'add' : '';
		$lists = $do->get_list($condition, $dorder[$order]);
		foreach($lists as $k=>$v) {
			$lists[$k]['mycat'] = $v['mycatid'] && isset($MTYPE[$v['mycatid']]) ? set_style($MTYPE[$v['mycatid']]['typename'], $MTYPE[$v['mycatid']]['style']) : $L['type_default'];
		}
	break;
}
$head_title = lang($L['module_manage'], array($MOD['name']));
if($_userid) {
	$nums = array();
	for($i = 1; $i < 5; $i++) {
		$r = $db->get_one("SELECT COUNT(*) AS num FROM {$table} WHERE username='$_username' AND status=$i");
		$nums[$i] = $r['num'];
	}
	$nums[0] = count($MTYPE);
	$r = $db->get_one("SELECT COUNT(*) AS num FROM {$table}_order WHERE seller='$_username'");
	$nums[9] = $r['num'];
}
$EXP = array();
if($_username && in_array($action, array('add', 'edit'))) {
	$result = $db->query("SELECT * FROM {$DT_PRE}mall_express WHERE username='$_username' AND parentid=0 ORDER BY listorder ASC,itemid ASC LIMIT 100");
	while($r = $db->fetch_array($result)) {
		$EXP[] = $r;
	}
}
if($action=='add' || $action=='edit'){
    include template('my_'.$module, 'member');	
}elseif($action=='zh'){
	include template('my_'.$module.'_next', 'member');
}else{
	include template('my_'.$module.'_list', 'member');
}

?>