<?php

/*
'**************************************************************************************************
' 程序名称: CCTVAI开源音乐管理系统
' 官方网站: http://www.cctvai.com
' 联系 Q Q: 1542367792
' QQ交流群：136687984
' 版本：(终结版)
' 备注：未经书面授权，不得向任何第三方提供本软件系统！
' 功能，模板，插件，扩展，定制请联系QQ：1542367792
'**************************************************************************************************
*/
error_reporting(0);
include "../../include/cctvai.conn.php";
include "../function_common.php";
admincheck(4);
/*if(Smallfivesolution()==$_SERVER['SERVER_NAME']) {
} else {
	echo Smallfivesq();
	exit();
}*/
$action=SafeRequest("action", "get");
?>
<!DOCTYPE html>
<html>
 <head> 
  <meta charset="gbk" /> 
  <meta name="renderer" content="webkit" /> 
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 
  <title>后台管理中心</title> 
  <link href="../css/add.css" rel="stylesheet" /> 
  <style type="text/css">
.table2{width: 100%;max-width: 100%;border-collapse: collapse;border-spacing: 0;}
.table2 td{font-size: 12px;border-top: 1px solid #ddd;padding: 8px;vertical-align: top;}
.table2 tr:hover{text-decoration: none;background-color: #e6f2fb;}
.table2 th{border-bottom: 2px solid #ddd;vertical-align: bottom;padding: 2px;text-align: left;}
.table3{width: 100%;max-width: 100%;border-collapse: collapse;border-spacing: 0;}
.table3 tr:hover{text-decoration: none;background-color: #e6f2fb;}
.table3 td{font-size: 12px;line-height:25px;border-top: 1px solid #ddd;padding: 5px;vertical-align: top;border-right: solid 1px #ddd;}
.table3 th{border-bottom: 2px solid #ddd;vertical-align: bottom;padding: 2px;text-align: left;}
</style>
 </head>   
 <body>
<?php
switch($action){
	case 'del':
		del();
		break;
	case 'alldel':
		alldel();
		break;
	case 'edit':
		Edit();
		break;
	case 'saveedit':
		SaveEdit();
		break;
	case 'keyword':
		$key=SafeRequest("key","get");
		main("select * from ".tname('pay')." where cd_name like '%".$key."%' or cd_type like '%".$key."%' or cd_uname like '%".$key."%' or cd_state like '%".$key."%' or cd_key like '%".$key."%' order by cd_addtime desc",20);
		break;
	default:
		main("select * from ".tname('pay')." order by cd_addtime desc",20);
		break;
	}
?>
</body>
</html>
<?php
function EditBoard($Arr, $ActionUrl, $ActionName) {
	global $db, $action;
	$CD_ID=SafeRequest("CD_ID", "get");
	$cd_key=$Arr[0];
	$cd_name=$Arr[1];
	$cd_rmb=$Arr[2];
	$cd_mub=$Arr[3];
	$cd_type=$Arr[4];
	$cd_state=$Arr[5];
	$cd_uid=$Arr[6];
	$cd_uname=$Arr[7];
	$cd_addtime=$Arr[8];
	$cd_extime=$Arr[9];
?>
  <div class="contents"> 
   <div class="panel"> 
    <div class="panel-head"> 
     <strong>编辑订单：<?php echo $cd_key; ?></strong> 
    </div> 
    <form action="<?php echo $ActionUrl; ?>" method="post" name="form2"> 
     <table class="table3"> 
      <tbody> 
       <tr> 
        <td width="70" align="left" class="p10">产品名称</td> 
        <td align="left"> <input type="text" class="input input_hd length_3" value="<?php echo $cd_name; ?>" name="cd_name" id="cd_name" />　 </td> 
       </tr> 
       <tr> 
        <td align="left" class="p10">产品订单号</td> 
        <td align="left"> <input type="text" class="input input_hd length_6" value="<?php echo $cd_key; ?>" name="cd_key" id="cd_key" /></td> 
       </tr> 
       <tr> 
        <td align="left" class="p10">创建人</td> 
        <td align="left"><input type="hidden" class="input input_hd length_2" value="<?php echo $cd_uid; ?>" name="cd_uid" id="cd_uid" /> <input type="text" class="input input_hd length_2" value="<?php echo $cd_uname; ?>" name="cd_uname" id="cd_uname" />　<font color="#d01f3c">&nbsp;&nbsp;必须是站内已存在的用户帐号！不能乱写!</font></td> 
       </tr> 
       <tr> 
        <td width="70" align="left" class="p10">购买数量</td> 
        <td align="left"> <input type="text" class="input input_hd length_1" value="<?php echo $cd_mub; ?>" name="cd_mub" id="cd_mub" />　 </td> 
       </tr> 
       <tr> 
        <td width="70" align="left" class="p10">付款金额</td> 
        <td align="left"> <input type="text" class="input input_hd length_2" value="<?php echo $cd_rmb; ?>" name="cd_rmb" id="cd_rmb" />　 </td> 
       </tr> 
       <tr> 
        <td width="70" align="left" class="p10">付款方式</td> 
        <td align="left"> <input type="text" class="input input_hd length_2" value="<?php echo $cd_type; ?>" name="cd_type" id="cd_type" />　 </td> 
       </tr> 
       <tr> 
        <td width="70" align="left" class="p10">交易状态</td> 
        <td align="left"> 
         <select name="cd_state" id="cd_state"> 
          <option value="">请选择</option> 
          <option value="等待付款" <?php if($cd_state=="等待付款"){echo "selected='selected'";}?>>等待付款</option> 
		  <option value="等待发货" <?php if($cd_state=="等待发货"){echo "selected='selected'";}?>>等待发货</option> 
		  <option value="等待确认收货" <?php if($cd_state=="等待确认收货"){echo "selected='selected'";}?>>等待确认收货</option> 
          <option value="交易完成" <?php if($cd_state=="交易完成"){echo "selected='selected'";}?>>交易完成</option> 
         </select>
		 </td> 
       </tr> 
       <tr> 
        <td width="70" align="left" class="p10">订单时间</td> 
        <td align="left"> <input type="text" class="input input_hd length_3" value="<?php echo $cd_addtime; ?>" name="cd_addtime" id="cd_addtime" />　 </td> 
       </tr> 
       <tr> 
        <td width="70" align="left" class="p10">结束时间</td> 
        <td align="left"> <input type="text" class="input input_hd length_3" value="<?php echo $cd_extime; ?>" name="cd_extime" id="cd_extime" />　 </td> 
       </tr>
       <tr> 
        <td height="35" colspan="2" align="left" bgcolor="#FAFBF7" class="td_border"><button type="submit" name="form2" class="btn btn_submit J_ajax_submit_btn" style="margin-left: 80px;" <?php if($cd_state=="交易完成"){echo 'onclick="return confirm(\'该订单已完成交易,确定要重复交易吗？\');"';}?>>确定提交</button><font color="#d01f3c">&nbsp;&nbsp;重复交易将会重复充值请注意!</font></td>
       </tr> 
      </tbody> 
     </table> 
    </form>
   </div> 
  </div>
<?php
}
function main($sql,$size){
	global $db,$action;
	$Arr=getpagerow($sql,$size);
	$result=$db->query($Arr[2]);
	$videonum=$db->num_rows($result);
?>
<?php
if($action=="") {
	$x5='btn_success';
	$name="会员充值订单";
} elseif($action=="keyword") {
	$key=SafeRequest("key","get");
	$name="搜索：".$key."";
} else {
	$x5='btn_success';
}
?>
<script type="text/javascript">
function CheckAll(form){
	for (var i=0;i<form.elements.length;i++){
		var e = form.elements[i];
		if (e.name != 'chkall')
			e.checked = form.chkall.checked;
	}
}
function MM_jumpMenu(targ, selObj, restore) { //v3.0
	eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
	if (restore)
		selObj.selectedIndex = 0;
}
</script>
<div class="contents"> 
   <div class="panel"> 
    <div class="panel-head">
     <strong><?php echo $name?></strong>
    </div> 
  <form method="get" action="pointsorder.php"> 
   <div class="search_type cc mb10"> 
    <div class="ul_wrap"> 
     <ul class="cc"> 
      <li> <label>关键字：</label> <input type="hidden" name="action" value="keyword"> <input name="key" id="key" value="" type="text" class="input length_3" placeholder="支持订单号搜索"> &nbsp;&nbsp;<button class="btn mr20" type="submit">搜索</button> </li> 
      <li> <label>支付方式：</label>
	  <select class="select_3" onChange="MM_jumpMenu('self',this,0)"> 
        <option value="pointsorder.php">请选择</option> 
        <option value="?action=keyword&key=支付宝" <?php if(SafeRequest("key", "get")=="支付宝") {echo "selected='selected'";}?>>支付宝</option>
        </select>
	  </li>
      <li> <label>交易状态：</label> 
	  <select class="select_3" onChange="MM_jumpMenu('self',this,0)"> 
        <option value="pointsorder.php">请选择</option> 
        <option value="?action=keyword&key=等待付款" <?php if(SafeRequest("key", "get")=="等待付款") {echo "selected='selected'";}?>>等待付款</option>
		<option value="?action=keyword&key=等待发货" <?php if(SafeRequest("key", "get")=="等待发货") {echo "selected='selected'";}?>>等待发货</option>
		<option value="?action=keyword&key=等待确认收货" <?php if(SafeRequest("key", "get")=="等待确认收货") {echo "selected='selected'";}?>>等待确认收货</option>
		<option value="?action=keyword&key=交易完成" <?php if(SafeRequest("key", "get")=="交易完成") {echo "selected='selected'";}?>>交易完成</option>
        </select>
	  </li>
     </ul> 
    </div> 
   </div> 
  </form>
<form name="form" method="post" action="?action=alldel">
    <table class="table2" id="dellist"> 
     <tbody>
      <tr>
       <td width="100" align="left"> 序号</td>
       <td align="left">产品名称</td>
	   <td align="left">产品订单号</td>
	   <td align="left">创建人</td>
	   <td align="left">购买数量</td>
	   <td align="left">付款金额</td>
	   <td align="left">付款方式</td>
	   <td align="left">付款状态</td>
	   <td align="left">订单时间</td>
	   <td align="left">结束时间</td>
	   <td width="120" align="left">操作</td>
      </tr>
<?php
if($videonum==0){
?>
<tr><td height="30" colspan="12" align="center" bgcolor="#FFFFFF" class="td_border"><br><br>没有数据<br><br><br></td></tr> 
<?php
}
if($result){
while ($row = $db ->fetch_array($result)){
?>
      <tr>
       <td width="100" align="left"> <input class="checkbox" type="checkbox" name="cd_id[]" id="cd_id" value="<?php echo $row['cd_id']; ?>"><?php echo $row['cd_id']; ?></td>
       <td align="left"><?php echo ReplaceStr($row['cd_name'],SafeRequest("key","get"),"<font color=\"red\">".SafeRequest("key","get")."</font>"); ?></td>
	   <td align="left"><?php echo $row['cd_key']?></td>
	   <td align="left"><?php echo ReplaceStr($row['cd_uname'],SafeRequest("key","get"),"<font color=\"red\">".SafeRequest("key","get")."</font>"); ?> (<?php echo GetAlias("cctvai_user", "cd_nicheng", "cd_name", $row['cd_uname'])?>)</td>
	   <td align="left"><?php echo $row['cd_mub']?></td>
	   <td align="left"><?php echo $row['cd_rmb']?>.00元</td>
	   <td align="left"><?php echo ReplaceStr($row['cd_type'],SafeRequest("key","get"),"<font color=\"red\">".SafeRequest("key","get")."</font>"); ?></td>
	   <td align="left"><?php echo ReplaceStr($row['cd_state'],SafeRequest("key","get"),"<font color=\"red\">".SafeRequest("key","get")."</font>"); ?></td>
	   <td align="left"><?php echo $row['cd_addtime']?></td>
	   <td align="left"><?php echo $row['cd_extime']?></td>
	   <td width="120" align="left"><a href="?action=edit&CD_ID=<?php echo $row['cd_id']; ?>" class="btn">编辑</a> <a href="?action=del&cd_id=<?php echo $row['cd_id']; ?>" class="btn" onClick="return confirm('确定要删除吗？不可恢复！');">删除</a></td>
      </tr>
<?php
}
}
?>
       <tr> 
        <td height="35" colspan="12" align="left" bgcolor="#FAFBF7" class="td_border"><input type="checkbox" id="chkall" class="checkbox" onClick="CheckAll(this.form)" /><label for="chkall">全选</label> &nbsp;&nbsp; <input type="submit" name="alldel" class="btn btn_submit J_ajax_submit_btn" value="批量删除" onClick="{if(confirm('确定要删除吗？不可恢复！')){this.document.form.submit();return true;}return false;}" /></td> 
       </tr> 
     </tbody>
    </table> 
</form>
   </div> 
   </div>
  <div class="p10" style="margin-bottom: 0px;"> 
   <div class="pages"><?php echo $Arr[0];?></div>
  </div> 
<?php
}
//编辑
function Edit()
{
    global $db;
    $CD_ID=SafeRequest("CD_ID", "get");
    $sql="select * from " . tname('pay') . " where CD_ID=" . $CD_ID;
    if($row=$db->getrow($sql)) {
        $Arr=array(
            $row['cd_key'],
            $row['cd_name'],
            $row['cd_rmb'],
            $row['cd_mub'],
            $row['cd_type'],
            $row['cd_state'],
            $row['cd_uid'],
            $row['cd_uname'],
            $row['cd_addtime'],
            $row['cd_extime']
        );
    }
    EditBoard($Arr, "pointsorder.php?action=saveedit&CD_ID=" . $CD_ID, "编辑");
}
//保存编辑数据
function SaveEdit()
{
    global $db;
    $cd_id=SafeRequest("CD_ID", "get");
    $cd_key=SafeRequest("cd_key", "post");
    $cd_name=SafeRequest("cd_name", "post");
    $cd_rmb=SafeRequest("cd_rmb", "post");
    $cd_mub=SafeRequest("cd_mub", "post");
    $cd_type=SafeRequest("cd_type", "post");
    $cd_state=SafeRequest("cd_state", "post");
    $cd_uid=SafeRequest("cd_uid", "post");
    $cd_uname=SafeRequest("cd_uname", "post");
    $cd_addtime=SafeRequest("cd_addtime", "post");
    $cd_extime=SafeRequest("cd_extime", "post");
    if($cd_state == "") {
        showmessage("出错了，请先选择交易状态！", "pointsorder.php", 1);
    }
    $sql="select * from " . tname('user') . " where cd_name='" . $cd_uname . "'";
    $result=$db->query($sql);
    if($row=$db->fetch_array($result)) {
        if($cd_state == "交易完成") {
            if(strpos($cd_name, "续费" . cd_viptime . "天VIP会员") !== false) {
                $sqls="select cd_grade,cd_vipenddate,cd_vipindate from " . tname('user') . " where cd_name='" . $cd_uname . "'";
                $row3=$db->getrow($sqls);
                $cd_vippoints=cd_vippoints;
                $cd_viptime=cd_viptime;
                $cd_vipenddate=date("Y-m-d", strtotime("+" . $cd_viptime . " day", strtotime($row3['cd_vipenddate'])));
                $db->query("update " . tname('user') . " set cd_vipenddate='$cd_vipenddate' where cd_name='" . $cd_uname . "'");
                //记录账单
                $setarr=array(
                    'cd_type'=>0, //1为加,0为减
                    'cd_uid'=>$cd_uid,
                    'cd_uname'=>$cd_uname,
                    'cd_icon'=>'payvip',
                    'cd_title'=>'支付续费' . cd_viptime . '天VIP会员',
                    'cd_points'=>cd_vippoints,
                    'cd_addtime'=>date('Y-m-d H:i:s')
                );
                inserttable('bill', $setarr, 1);
                //发送通知
                $setarr=array(
                    'cd_uid'=>0,
                    'cd_uname'=>'系统提示',
                    'cd_uids'=>$cd_uid,
                    'cd_unames'=>$cd_uname,
                    'cd_icon'=>'payvip',
                    'cd_data'=>cd_vipxtxt,
                    'cd_dataid'=>0,
                    'cd_state'=>1,
                    'cd_addtime'=>date('Y-m-d H:i:s')
                );
                inserttable('notice', $setarr, 1);

            } elseif(strpos($cd_name, "充值" . cd_viptime . "天VIP会员") !== false) {
                $cd_vippoints=cd_vippoints;
                $cd_viptime=cd_viptime;
                $cd_vipindate=date('Y-m-d H:i:s');
                $tomorrow=mktime(date("H"), date("i"), date("s"), date("m"), date("d")+$cd_viptime, date("Y"));
                $cd_vipenddate=date("Y-m-d H:i:s", $tomorrow);
                $db->query("update " . tname('user') . " set cd_vipgrade=1,cd_grade=1,cd_vipindate='$cd_vipindate',cd_vipenddate='$cd_vipenddate' where cd_name='" . $cd_uname . "'");
                //记录账单
                $setarr=array(
                    'cd_type'=>0, //1为加,0为减
                    'cd_uid'=>$cd_uid,
                    'cd_uname'=>$cd_uname,
                    'cd_icon'=>'payvip',
                    'cd_title'=>'支付充值' . cd_viptime . '天VIP会员',
                    'cd_points'=>cd_vippoints,
                    'cd_addtime'=>date('Y-m-d H:i:s')
                );
                inserttable('bill', $setarr, 1);
                //发送通知
                $setarr=array(
                    'cd_uid'=>0,
                    'cd_uname'=>'系统提示',
                    'cd_uids'=>$cd_uid,
                    'cd_unames'=>$cd_uname,
                    'cd_icon'=>'payvip',
                    'cd_data'=>cd_vipxtxt,
                    'cd_dataid'=>0,
                    'cd_state'=>1,
                    'cd_addtime'=>date('Y-m-d H:i:s')
                );
                inserttable('notice', $setarr, 1);

            } elseif(strpos($cd_name, "金币") !== false) {
                $db->query('update ' . tname('user') . " set cd_points=cd_points+'" . $cd_mub . "' where cd_name='" . $cd_uname . "'");
                //记录账单
                $setarr=array(
                    'cd_type'=>1, //1为加,0为减
                    'cd_uid'=>$cd_uid,
                    'cd_uname'=>$cd_uname,
                    'cd_icon'=>'paypoints',
                    'cd_title'=>'充值' . $cd_mub . '金币',
                    'cd_points'=>$cd_mub,
                    'cd_addtime'=>date('Y-m-d H:i:s')
                );
                inserttable('bill', $setarr, 1);
                //发送通知
                $setarr=array(
                    'cd_uid'=>0,
                    'cd_uname'=>'系统提示',
                    'cd_uids'=>$cd_uid,
                    'cd_unames'=>$cd_uname,
                    'cd_icon'=>'paypoints',
                    'cd_data'=>'您充值的' . $cd_mub . '金币已到账！',
                    'cd_dataid'=>0,
                    'cd_state'=>1,
                    'cd_addtime'=>date('Y-m-d H:i:s')
                );
                inserttable('notice', $setarr, 1);
            }
        }
        $db->query("update " . tname('pay') . " set cd_key='" . $cd_key . "',cd_name='" . $cd_name . "',cd_rmb='" . $cd_rmb . "',cd_mub='" . $cd_mub . "',cd_type='" . $cd_type . "',cd_state='" . $cd_state . "',cd_uname='" . $cd_uname . "',cd_addtime='" . $cd_addtime . "',cd_extime='" . $cd_extime . "' where cd_id=" . $cd_id);
        showmessage("恭喜您，订单编辑成功！", "pointsorder.php", 0);
    } else {
        showmessage("编辑失败，所属会员不存在！", $_SERVER['HTTP_REFERER'], 0);
    }
}
//批量删除
function alldel()
{
    global $db;
    $cd_id=RequestBox("cd_id");
    if($cd_id == 0) {
        showmessage("出错了，请先勾选要删除的订单", "pointsorder.php", 0);
    } else {
        $db->query("delete from " . tname('pay') . " where cd_id in ($cd_id)");
        showmessage("恭喜您，订单删除成功！", "pointsorder.php", 0);
    }
}

//删除
function del()
{
    global $db;
    $cd_id=SafeRequest("cd_id", "get");
    $db->query("delete from " . tname('pay') . " where cd_id=" . $cd_id . "");
    showmessage("恭喜您，订单删除成功！", "pointsorder.php", 0);
}
?>