<?php
include_once("../includes/global.php");
include_once("../includes/page_utf_class.php");
$script_tmp = explode('/', $_SERVER['SCRIPT_NAME']);
$sctiptName = array_pop($script_tmp);
include_once("auth.php");
//======================================
if(!empty($_POST["submit"]))
{
	foreach($_POST['id'] as $key=>$v)
	{
		$_POST['nums'][$key]=empty($_POST['nums'][$key])?0:$_POST['nums'][$key];
		$sql="update ".COUNTRY." set
	    ename='".$_POST['ename'][$key]."',cname='".$_POST['cname'][$key]."',postcode='".$_POST['postcode'][$key]."',
		flag='".$_POST['flag'][$key]."',nums='".$_POST['nums'][$key]."'
		where id='$v'";
		$db->query($sql);
		if($_POST['delid'][$key])
		{
			$db->query("delete from ".COUNTRY." where id='".$_POST['delid'][$key]."'");
		}
	}
	if(!empty($_POST['add_cname']))
	{
		$_POST['add_nums']*=1;
		$db->query("select id from ".COUNTRY." order by id desc");
		$rid=$db->fetchRow();
		$id=$rid['id']+1;
		$sql="insert into ".COUNTRY." 
		(id,cname,ename,postcode,flag,nums) 
		values 
		('$id','$_POST[add_cname]','$_POST[add_ename]','$_POST[add_postcode]','$_POST[add_flag]','$_POST[add_nums]')";
		$re=$db->query($sql);
	}
	admin_msg('country.php','Successful');
}
?>
<!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>
<TITLE><?php echo lang_show('admin_system');?></TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="main.css" rel="stylesheet" type="text/css" />
</HEAD>
<body>
<div class="bigbox">
  <div class="bigboxhead"><?php echo lang_show('country_m');?></div>
  <div class="bigboxbody">
    <form action="" method="get">
      <table cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td class="searh_left"><?php echo lang_show('key_word');?></td>
          <td><input class="text" name="keyword" type="text" value="<?php echo $_GET['keyword'];?>">
            <input class="btn" type="submit" name="button" id="button" value="<?php echo lang_show('Submit');?>"></td>
        </tr>
      </table>
    </form>
    <form action="" method="post">
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr class="theader">
          <td width="30"><?php echo lang_show('del');?></td>
          <td width="40"><?php echo lang_show('cno');?></td>
          <td width="40"><?php echo lang_show('none');?></td>
          <td width="40"><?php echo lang_show('postcode');?></td>
          <td width="80"><?php echo lang_show('national_flag');?></td>
          <td width="250"><?php echo lang_show('c_e_n');?></td>
          <td><?php echo lang_show('c_c_n');?></td>
        </tr>
        <?php
			if(!empty($_GET['keyword']))
				$tsql=" and ename like '%$_GET[keyword]%' or cname like '%$_GET[keyword]%' or flag like '%$_GET[keyword]%'";
			$sql="select * from ".COUNTRY." where 1 $tsql order by nums asc,ename asc";
			//=============================
				$page = new Page;
				$page->listRows=50;
				if (!$page->__get('totalRows')){
					$db->query($sql);
					$page->totalRows = $db->num_rows();
				}
				$sql .= "  limit ".$page->firstRow.",50";
				$pages = $page->prompt();
			//=====================
			$db->query($sql);
			$re=$db->getRows();
			foreach($re as $v)
			{
			?>
        <tr>
          <td><input type="checkbox" class="checkbox" name="delid[]" value="<?php  echo $v['id']; ?>" />
            <input name="id[]" type="hidden" value="<?php  echo $v['id']; ?>" />
          </td>
          <td><?php  echo $v['id']; ?></td>
          <td><input class="text" style="width:30px;" value="<?php  echo $v['nums']; ?>" name="nums[]" type="text" /></td>
          <td><input class="text" style="width:30px;" value="<?php  echo $v['postcode']; ?>" name="postcode[]" type="text" /></td>
          <td><input style="width:30px;" value="<?php  echo $v['flag']; ?>" name="flag[]" type="text" />
            <img src="../image/cflag/<?php echo $v['flag'];?>.gif" > </td>
          <td><input class="text" value="<?php  echo $v['ename']; ?>" name="ename[]" type="text" /></td>
          <td><input class="text" value="<?php  echo $v['cname']; ?>" name="cname[]" type="text" /></td>
        </tr>
        <?php
		}
		?>
        <tr>
          <td><?php echo lang_show('add_new');?></td>
          <td>&nbsp;</td>
          <td><input style="width:30px;" name="add_nums" type="text" id="nums" class="text"></td>
          <td><input style="width:30px;" name="add_postcode" type="text" id="postcode" class="text"></td>
          <td><input style="width:30px;" name="add_flag" type="text" id="flag" class="text"></td>
          <td><input class="text" name="add_ename" type="text" /></td>
          <td><input class="text" name="add_cname" type="text" /></td>
        </tr>
        <tr>
          <td></td>
          <td colspan="6"><input class="btn" type="submit" name="submit" value="<?php echo lang_show('submit');?>"></td>
        </tr>
        <tr>
          <td colspan="7"><div class="page"><?php echo $pages?></div></td>
        </tr>
      </table>
    </form>
  </div>
</div>
</body>
</html>
