<?php
include ('admin_global.php');

$cmd = $_GET["cmd"];
if($cmd=="list"){
    $args=$_POST;
    $t_sql="select id from card where";
    $sql="select c.*,gc.name as gameclassname from card as c,gameclass as gc where gc.id=c.gameclassid and ";
    $search=iconv("utf-8", "gbk", $args[keywords]);
    if($search==null || $search==""){
        $sql.=" 1=1 ";
        $t_sql.=" 1=1 ";
    }else{
        $sql.=" title like '%$search%' ";
        $t_sql.=" title like '%$search%' ";
    }
//echo $sql;
    $total=mysql_num_rows($db->query($t_sql));  //计算结果集总条数用于分页
    if($args[limit]!=""){
        if($args[start]!="" || $args[start]!=0){
         $start=$args[start];
        }else{
            $start=0;
        }
         $sql.="order by c.id desc limit ".$start.",$args[limit]";
    }
    $query=$db->query($sql);
    while ($row=$db->fetch_array($query)) {
        $i=0;
        $getTime=$row[getTime];
        if($getTime==null || $getTime==""){
            $getTime="无";
        }else{
            $getTime=date("m-d", $row[getTime]);
        }
        $data.="{id:".$row[id].",number:'$row[number]',addTime:'".  date("m-d", $row[addTime])."',getTime:'$getTime',gameclass:\"".iconv("GBK", "utf-8", $row[gameclassname])."\",userId:$row[userid],gameclassid:'$row[gameclassid]'}";
        if($i<  (count($row)-1)){
           $data.=",";
        }
        $i++;
    }
    $str="{rowCount:".$total.",result:[".$data."]}";
    echo $str;
}

if($cmd=="save"){
    $args=$_POST;
    $sql="insert into card values(null,'$args[number]',".mktime().",null,0,'$args[gameclassid]')";
    $query=$db->query($sql);
    if($query){
        echo "{success:true,info:'新增成功'}";
    }else{
        echo "{failure:true,info:'新增失败'}";
    }
}

if($cmd=="update"){
    $args=$_POST;
    $sql="update card set number=$args[number],addTime='".mktime()."'  where id=$args[id]";
    $query=$db->query($sql);
    if($query){
        echo "{success:true,info:'修改成功'}";
    }else{
        echo "{failure:true,info:'修改失败'}";
    }
}

if($cmd=="remove"){
    $sql="delete from card where id=$_POST[id]";
    $query=$db->query($sql);
    if($query){
        echo "{success:true,info:'删除成功'}";
    }else{
        echo "{failure:true,info:'删除失败'}";
    }
}

if($cmd=="import"){
    $numbers=$_POST[numbers];
    $arr=explode("\n",$numbers);
    $gameId=$_POST[gameId];
    if(count($arr)>0){
    foreach ($arr as $key => $value) {
        $sql="insert into card values(null,'$value',".mktime().",null,0,'$gameId')";
        $db->query($sql);
    }
        echo "{success:true}";
    }else{
        echo "{failure:true}";
    }
}
?>
