<?php
defined ( 'IN_TS' ) or die ( 'Access Denied.' );

$userid = aac('user')->isLogin();
$shopid = intval ( $_POST ['shopid'] );
$much=intval ( $_POST ['much'] );
$strshop = $new ['shop']->find ( 'shop', array (
		'shopid' => $shopid 
) );/*
if ($strshop == '') {
	header ( "HTTP/1.1 404 Not Found" );
	header ( "Status: 404 Not Found" );
	$title = '404';
	include pubTemplate ( "404" );
	exit ();
}*/
//卖家
$arrshopuser = aac ( 'user' )->getSimpleUser ( $strshop ['userid'] );

// 是否审核
if ($strshop ['isaudit'] == 1 && $TS_USER['isadmin']==0 && $TS_USER['userid']!=$strshop['userid']) {
	tsNotice ( '内容审核中...' );
}

switch($_POST ['ts'] )
{
	case "do":
		$address=h($_POST['address']);
		
		if($strshop['type']==1)//注册类
		{
			if($strshop['pay_type']==1)//积分支付
			{
				$preg = '/[^|\s]([\s\S])[$|\s]/';
				$arr = explode("\n", $address);
				$arr = array_filter($arr);  // 删除空元素
				
				$a=count($arr);          //获取码数量
				
				if($a!=$much)
				{
					tsNotice('你的购买数量是：'.$much.'但输入了：'.$a.'个申请');
					
				}
				
				
				if($much>$strshop['stock'])
				{
					tsNotice('库存不足！！');
					
				}
				
				
				
				
				$need_pay=$much*$strshop['pay'];
				if(aac('user')->delScore($userid,"BUY".$strshop['title']."X".$much,$need_pay))
                {
					$reorder='';
					for($b=0;$b<$a;$b++)
					{
						preg_replace('# #','',$arr[$b]);
						
						$reorder=$reorder.'</br>for:'.$arr[$b].'</br>Key:'.md5($arr[$b].$strshop['note']).'</br>';
					
					}
                    $orderid = $new['shop']->create('shop_order',array(
                                                          'userid'=>$userid,
                                                          'selluserid'=>$strshop['userid'],
                                                          'shopid'=>$strshop['shopid'],
														  'title'=>$strshop['title'],
														  'content'=>$strshop['content'],
                                                          'type'=>$strshop['type'],
                                                          'pay' => $strshop['pay'],
                                                          'pay_type' => $strshop['pay_type'],
														  'much'=> $much,
                                                          'address' => $address,
														  'reorder'=>$reorder,
                                                          'state' => 1,
                                                          'addtime'=>date('Y-m-d H:i:s')
                                                          ));
					$new['shop']->update ( 'shop', array (
					'shopid' => $strshop['shopid']
					), array (					
								'stock' => $strshop['stock']-$much
								));									  
                    aac('user')->addScore($strshop['userid'],"订单：".$orderid."收益",$need_pay,1);
                    
                }else
                {
                    tsNotice('积分不足！');
                }
				
				tsNotice('成功提交','查看订单',tsUrl('shop','order',array('id'=>$orderid)));
			}
			
		}
		
		back;
	case "cheack":
		include template ( "buy" );
		back;
	
	
}
