<?php
/**
 * @version        Somao_Search X1.0 9:23 2012-1-1 Somao $
 * @package        Somao_Search.Site
 * @copyright      Copyright (c) 2009 - 2012, Somao, Inc.
 * @license        http://www.somao123.com/
 * @link           http://www.somao.cc/
 */
require "global.php";
CheckPurview('tg_keysys',$authoritys,$rank);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<meta http-equiv="x-ua-compatible" content="ie=7" />
<link href="css/admincp.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
  <div class="floattop">
    <div class="itemtitle"><h3>竞价关键词设置</h3>
      <ul class="tab1">
        <li class="current"><a href="keywords_set.php"><span>关键词列表</span></a></li>
        <li><a href="?action=addform"><span>添加关键词</span></a></li>
      </ul>
     </div>
   </div>
 <div class="floattopempty"></div>
<table class="tb tb2 " id="tips">
<tr><th  class="partition">提示</th></tr>
<tr><td class="tipsblock" s="1"><ul id="tipslis"><li>广告管理。</li></ul></td></tr>
</table>
<?php
$action=$_GET["action"];
switch ($action)
{
    case "saveform":
    saveform();
    break;

    case "addform":
	addform($action);
	break;
	
	case "modify":
	addform($action);
	break;
	
	case "del":
	$db->query("delete from ve123_keyword_price where key_id='".intval($_GET["key_id"])."'");
	break;
}	
if($action==""){?>
<table class="tb tb2 ">
  <tr class="header">
    
    <th style="width:50px; text-align:center">ID</th>
    <th style="width:300px; text-align:left">关键词</th>
    <th>最低价格</th>
    <th>操作</th>
  </tr>
  <?php
  $sql="select * from ve123_keyword_price";
  $result=$db->query($sql);
  $total=$db->num_rows($result);//记录总数
  $pagesize=20;//每页显示数
  $totalpage=ceil($total/$pagesize);
  $page=intval($_GET["page"]);
  if($page<=0){$page=1;}
  $offset=($page-1)*$pagesize;
  $result=$db->query($sql." order by key_id desc limit $offset,$pagesize");
  while ($rs=$db->fetch_array($result))
  {
  ?>
  <tr>
    <td width="50" height="28" align="center"><?php echo $rs["key_id"]?></td>
    <td><?php echo $rs["keywords"];?></td>
    <td><?php echo $rs["price"];?></td>
    <td><a href="?action=modify&key_id=<?php echo $rs["key_id"];?>">修改</a>&nbsp;&nbsp;<a href="?action=del&key_id=<?php echo $rs["key_id"];?>" onClick="if(!confirm('确定删除吗?')) return false;">删除</a></td>
  </tr>
  <?php
  }
  ?>
 <tr><td colspan="15"><?php echo pageshow($page,$totalpage,$total,"?");?></td></tr>
</table>


<?php
}

function addform($do_action)
{
global $db,$p_cid;
  if ($do_action=="modify")
  {
      $key_id=intval($_GET["key_id"]);
	  $sql="select * from ve123_keyword_price where key_id='$key_id'";
	  $rs=$db->get_one($sql);
	  $keywords=$rs["keywords"];
	  $price=$rs["price"];
	  $sort_id=$rs["sort_id"];
	  $btn_txt="确定修改";
  }
  else
  {
     $btn_txt="确定添加";
  }
?>
<table class="tb tb2 " id="tips">
<form id="form1" name="form1" method="post" action="?action=saveform">
  <tr>
    <td width="100" align="right">关键词:</td>
    <td>
      <input name="keywords" type="text" value="<?php echo $keywords?>" size="50" /></td>
  </tr>
  <tr>
    <td align="right">最低价格:</td>
    <td><input name="price" type="text" size="50"  value="<?php echo $price?>"/></td>
  </tr>
  
  <tr>
    <td>&nbsp;</td>
    <td>
	<input type="hidden" name="key_id" value="<?php echo $key_id?>">
	<input type="hidden" name="do_action" value="<?php echo $do_action?>">
	<input type="submit" name="Submit" value="<?php echo $btn_txt?>" />	</td>
  </tr>
  </form>
</table>

<?php
}
?>

<?php
function saveform()
{
global $db;
   $keywords=trim($_POST["keywords"]);
   $price=trim($_POST["price"]);
   $key_id=intval($_POST["key_id"]);
   $do_action=$_POST["do_action"];
   if ($do_action=="modify")
   {
	 $array=array('keywords'=>$keywords,'price'=>$price);
	 $db->update("ve123_keyword_price",$array,"key_id='$key_id'");
	 jsalert("修改成功","keywords_set.php");
   }
   else
   {     
             $array=array('keywords'=>$keywords,'price'=>$price);
	         $db->insert("ve123_keyword_price",$array);  
			 jsalert("提交成功","keywords_set.php");
   }
}
?>
</div>
</body>
</html>