<?php
$menumark = "package_list";
switch ($act) {
	//#####################@ 会员购买套餐 @#####################//
	case 'add':		
		$package_list = $db->pe_selectall('package');
	
		$package_indate = $package_list[0]['package_indate'];

		$action = "user.php?mod=packagelog&act=addsql&user_id={$_g_user_id}&order_id={$_g_order_id}";
		include(pe_tpl('packagelog_add.html'));
	break;
	//#####################@ 会员购买套餐sql @#####################//
	case 'addsql':
		//获取套餐提成
		$package = $db->pe_select('package', array('package_id'=>$_p_info['package_id']));
		//判断当前用户是否有推荐人
		$info = $db->pe_select('user', array('user_id'=>$_g_user_id));
	
		$_p_info['user_id'] = $_g_user_id;
		$_p_info['user_name'] = $info['user_name'];

		$_p_info['packagelog_etime'] = strtotime($_p_info['packagelog_stime']);
		$_p_info['packagelog_etime'] = strtotime(pe_date($_p_info['packagelog_etime'], 'Y') + $package['package_indate'] .'-'.pe_date($_p_info['packagelog_etime'], 'm').'-'.pe_date($_p_info['packagelog_etime'], 'd'));
		$_p_info['packagelog_stime'] = strtotime($_p_info['packagelog_stime']);

		if ($packagelog_id = $db->pe_insert('packagelog', $_p_info)) {
			if($info['user_level']==2)$db->pe_update('user', array('user_id'=>$_g_user_id),array('user_level'=>5));
			if ($info['user_id_tj']) {
				//判断一级推荐是否是有（一级or二级）推荐级别
				$info_tj1 = $db->pe_select('user', " and `user_id` = '{$info['user_id_tj']}' and `user_tjlevel` != 0");
				if ($info_tj1['user_id']) {
					$brokerage1['brokerage_money'] = $package['package_money'] * (in_array($info_tj1['user_level'], array(3, 4)) ? $package['package_tc1_vip'] : $package['package_tc1_free']);
					$brokerage1['brokerage_atime'] = time();
					$brokerage1['user_id'] = $info_tj1['user_id'];
					$brokerage1['user_name'] = $info_tj1['user_name'];
					$brokerage1['user_tjlevel'] = 1;				
					$brokerage1['user_id_pay'] = $info['user_id'];					
					$brokerage1['user_name_pay'] = $info['user_name'];
					$brokerage1['packagelog_id'] = $packagelog_id;
					//ok 给佣金吧！
					$db->pe_insert('brokerage', $brokerage1);
					$db->pe_update('user', array('user_id'=>$info_tj1['user_id']), "`user_money` = `user_money` + {$brokerage1['brokerage_money']}");
				}
				//判断二级推荐是否是有二级推荐级别
				$info_tj2 = $db->pe_select('user', array('user_id'=>$info_tj1['user_id_tj'], 'user_tjlevel'=>2));
				if ($info_tj2['user_id']) {
					$brokerage2['brokerage_money'] = $brokerage1['brokerage_money'] * (in_array($info_tj2['user_level'], array(3, 4)) ? $package['package_tc2_vip'] : $package['package_tc2_free']);
					$brokerage2['brokerage_atime'] = $brokerage1['brokerage_atime'];
					$brokerage2['user_id'] = $info_tj2['user_id'];
					$brokerage2['user_name'] = $info_tj2['user_name'];
					$brokerage2['user_tjlevel'] = 2;
					$brokerage2['user_id_pay'] = $info['user_id'];			
					$brokerage2['user_name_pay'] = $info['user_name'];
					$brokerage2['packagelog_id'] = $packagelog_id;
					//ok 给佣金吧！
					$db->pe_insert('brokerage', $brokerage2);
					$db->pe_update('user', array('user_id'=>$info_tj2['user_id']), "`user_money` = `user_money` + {$brokerage2['brokerage_money']}");
				}
			}
			if ($_g_order_id) $db->pe_update('order', array('order_id'=>$_g_order_id), array('order_isbuy'=>1));
			pe_success('套餐购买成功!', "user.php?mod=packagelog&act=list&user_id={$_g_user_id}");
		}
		else {
			pe_error('套餐购买失败!');
		}
	break;
	//#####################@ 修改会员套餐 @#####################//
	case 'edit':
		$package_list = $db->pe_selectall('package');
		$cache_package = cache::get('package');

		$info = $db->pe_select('packagelog', array('packagelog_id'=>$_g_id));
		$package_indate = $cache_package[$info['package_id']]['package_indate'];

		$action = "user.php?mod=packagelog&act=editsql&id={$_g_id}&user_id={$info['user_id']}&order_id={$_g_order_id}";
		include(pe_tpl('packagelog_add.html'));
	break;
	//#####################@ 修改会员套餐sql @#####################//
	case 'editsql':
		$package = $db->pe_select('package', array('package_id'=>$_p_info['package_id']));
		$_p_info['packagelog_etime'] = strtotime($_p_info['packagelog_stime']);
		$_p_info['packagelog_etime'] = strtotime(pe_date($_p_info['packagelog_etime'], 'Y') + $package['package_indate'] .'-'.pe_date($_p_info['packagelog_etime'], 'm').'-'.pe_date($_p_info['packagelog_etime'], 'd'));
		$_p_info['packagelog_stime'] = strtotime($_p_info['packagelog_stime']);
		if ($db->pe_update('packagelog', array('packagelog_id'=>$_g_id), $_p_info)) {
			if ($_g_order_id) $db->pe_update('order', array('order_id'=>$_g_order_id), array('order_isbuy'=>1));
			pe_success('套餐修改成功!', "user.php?mod=packagelog&act=list&user_id={$_g_user_id}");
		}
		else {
			pe_error('套餐修改失败!');
		}
	break;
	//#####################@ 套餐日志删除sql @#####################//
	case 'delsql':
		if ($db->pe_delete('packagelog', array('packagelog_id'=>$_g_id))) {
			$db->pe_delete('brokerage', array('packagelog_id'=>$_g_id));
			pe_success('套餐删除成功!');
		}
		else {
			pe_error('套餐删除失败...');
		}
	break;
	//#####################@ 会员套餐列表 @#####################//
	default :
		$cache_package = cache::get('package');
	
		$info_list = $db->pe_selectall('packagelog', array('user_id'=>$_g_user_id));
		include(pe_tpl('packagelog_list.html'));
	break;
}
pe_result();
?>