<?php
define('IN_PHPMPS', true);
require_once dirname(__FILE__) . '/include/common.php';
include_once dirname(__FILE__) . '/include/pay.fun.php';

$_REQUEST['act'] = $_REQUEST['act'] ? trim($_REQUEST['act']) : 'list' ;

switch($_REQUEST['act'])
{
    case 'add':
		if($_POST['dosubmit'])
		{
			if($_POST['token']!=$CFG['crypt']) show('提交错误');
			extract($_REQUEST);
            $amount = floatval($amount);
			$time = time();
            $r = $db->getOne("SELECT money FROM {$table}member WHERE userid='$userid'");
			if(count($r)<1)show('不存在此用户');
			//券
			if($stype=='lejuan'){
			   $db->query("INSERT INTO {$table}juan_pay (`userid`,`jid`,`paytime`) VALUES('$userid','$lejuans','$time')");
			   $db->query("UPDATE {$table}juan SET nums=nums-1 where id='$lejuans'");
			   show('操作成功');
			   exit;
			}
			//
			if($stype=='money'){
			  $balance = "money";
			  //$balatxt = "[短信]";
			}elseif($stype=='credit'){
			  $balance = "credit";
			  $balatxt = "[积分]";
			}
            if($operation == '+')
			{
			    $balance += $amount;
			}
			elseif($operation == '-')
			{
			    $balance -= $amount;
			}
			$balance = round($balance, 2);
			
			$year = date('Y', $time);
			$month = date('m', $time);
			$date = date('Y-m-d', $time);

			$note = htmlspecialchars_deep($note);
			$note = $balatxt.cut_str($note, 100);

			if($operation == '+')
			{ 
			  if($stype=='money'){
			     pay_money_add($userid, $amount, $note);
			  }elseif($stype=='credit'){
			     pay_credit_add($userid, $amount, $note);
			  }
			}
			elseif($operation == '-')
			{
			  if($stype=='money'){
			     pay_money_diff($userid, $amount, $note);
			  }elseif($stype=='credit'){
			     pay_credit_diff($userid, $amount, $note);
			  }
			}
			show('操作成功');
		}
		else
		{
			if(!isset($typeid)) $typeid = 0;
			if(!isset($userid)) $userid = '';
			if(!isset($amount)) $amount = '';
			if(!isset($note)) $note = '';
			$userid = $_REQUEST['userid'] ? intval($_REQUEST['userid']) : '';
			$typeid = $_REQUEST['type'] ? trim($_REQUEST['type']) : '';
			if($userid){
			  $usernames = $db->getOne("SELECT username FROM {$table}member WHERE userid='$userid'");
			  if(!$usernames)show('不存在此用户');
			}
			//juan
			$juans = array();
	        $result = $db->query("SELECT id,money,xfmy FROM {$table}juan WHERE nums>0");
	        while($r = $db->fetchrow($result)) {
		      $juans[] = $r;
	        }
			include tpl('add_pay');
		}
	break;

	case 'list':
		extract($_POST);
		$typeid = isset($typeid) ? intval($typeid) : 0;
		$pagesize = '20';
		$page = empty($_REQUEST['page'])? 1 : intval($_REQUEST['page']);

		$sql = '';
		$sql .= $typeid ? " and typeid=$typeid" : "";
		$sql .= isset($paytype) && $paytype ? " and paytype='$paytype'" : "";
		$sql .= isset($date) && $date ? " and date='$date'" : "";
		$sql .= isset($fromdate) && $fromdate ? " and date>='$fromdate'" : "";
		$sql .= isset($todate) && $todate ? " and date<='$todate'" : "";
		$sql .= isset($keywords) && $keywords ? " and note like '%$keywords%'" : "";
		$sql .= isset($userid) && $userid ? " and userid='$userid'" : "";
		$r = $db->getOne("select COUNT(*) from {$table}pay where deleted=0 $sql");
		$pager['search'] = array('act'=>'list');
		$pager = get_pager('pay.php',array('act'=>'list'),$r, $page, $pagesize);

        $pays = $money = array();
		$result = $db->query("select * from {$table}pay where deleted=0 $sql order by payid desc limit $pager[start],$pager[size]");
		while($r = $db->fetchrow($result)) {
			$money[$r['typeid']][] = $r['amount'];
			$pays[] = $r;
		}
		$fromdate = isset($fromdate) ? $fromdate : date('Y-m-d');
		$todate = isset($todate) ? $todate : date('Y-m-d');

		include tpl('list_pay');
	break;

	case 'delete':
		$payid = intval($_REQUEST['id']);
		if(!$payid)show('没有选择记录');
		$db->query("DELETE FROM {$table}pay WHERE payid=$payid ");
		show('操作成功', "pay.php?act=list");
	break;
}
?>