<?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");
include_once('../lib/allchar.php');
//======================================
if(!empty($_POST["submit"]))
{
	$_POST['id']=is_array($_POST['id'])?$_POST['id']:array();
	foreach($_POST['id'] as $key=>$v)
	{
		$prvid=$_POST['province_id'][$key]*1;
		$sql="update ".CITY." set
		city='".$_POST['city'][$key]."',nums='".$_POST['nums'][$key]."',province_id='".$prvid."' where city_id='$v'";
		$db->query($sql);
		if($_POST['delid'][$key])
		{
			$db->query("delete from ".CITY." where city_id='".$_POST['delid'][$key]."'");
		}
	}
	if(!empty($_POST['add_citys']))
	{
		$sql="insert into ".CITY." (city,nums,province_id) 
		values 
		('$_POST[add_citys]','$_POST[add_nums]','$_POST[add_province_id]')";
		$db->query($sql);
	}
	$sql="select * from ".CITY." where city!=''";
	$db->query($sql);
	$re=$db->getRows();
	foreach($re as $v)
	{
		$pycity=c(trim($v['city']));
		$sql="update ".CITY." set py='$pycity' where city_id='$v[city_id]'";
		$db->query($sql);
	}
}
if(!empty($_GET['set']))
{
	@set_time_limit(0);
	$fp=fopen("../install/city.sql","r");
	$sql=fread($fp,filesize("../install/city.sql"));
	fclose($fp);
	$ar=explode(";",$sql);
	foreach($ar as $ve)
	{
		if(!empty($ve))
		{
			$ve=str_replace("b2bbuilder_",$config['table_pre'],$ve);
			$qre=$db->query($ve);
		}
	}
}
if(!empty($_GET['cg']))
{
	$sql="select * from ".CITY;
	$db->query($sql);
	$re=$db->getRows();
	foreach($re as $v)
	{
		$city=c($v['city']);
		$sql="update ".CITY." set city='$city' where city_id='$v[city_id]'";
		$db->query($sql);
	}
}
$sql="select * from ".PROVINCE." order by province asc";
$db->query($sql);
$ct=$db->getRows();
?>
<!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('managecity');?></div>
  <div class="bigboxbody">
    <input onclick="window.location='city.php?set=1'" type="button" class="btn" value="<?php echo lang_show('import_ini_city');?>" />
    <input onclick="window.location='city.php?cg=1'" type="button" class="btn" value="<?php echo lang_show('into_pinyin');?>" />
    <form action="" method="post">
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr class="theader">
          <td width="30"><?php echo lang_show('group_id');?></td>
          <td width="30"><?php echo lang_show('del');?></td>
          <td width="30"><?php echo lang_show('none');?></td>
          <td width="250"><?php echo lang_show('name');?></td>
          <td colspan="2"><select onchange="window.location='city.php?province_id='+this.value;" class="select" name="country" id="country">
              <option value="" selected></option>
              <?php
				foreach($ct as $v)
				{
				?>
              <option <?php if($v['province_id']==$_GET['province_id']){?>selected="selected"<?php } ?> value="<?php echo $v['province_id']; ?>">
              <?php  echo $v['province']; ?>
              </option>
              <?php } ?>
            </select>          </td>
        </tr>
        <?php
		$ksql=NULL;
		if(!empty($_GET["province_id"]))
			$ksql.=" and province_id='$_GET[province_id]' ";
		$sql="select * from ".CITY." where 1 $ksql order by nums 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);
		while($db->next_record())
		{
		?>
        <tr>
          <td><?php  echo $db->f('city_id'); ?></td>
          <td><input type="checkbox" class="checkbox" name="delid[]" value="<?php  echo $db->f('city_id'); ?>" />
            <input name="id[]" type="hidden" value="<?php  echo $db->f('city_id'); ?>" />          </td>
          <td><input style="width:30px;" value="<?php  echo $db->f('nums'); ?>" name="nums[]" type="text" /></td>
          <td><input class="text" value="<?php  echo $db->f('city'); ?>" name="city[]" type="text" /></td>
          <td colspan="2" align="left"><select class="select" name="province_id[]">
              <option value="" selected><?php echo lang_show('select_country'); ?></option>
              <?php
				foreach($ct as $v)
				{
				?>
              <option <?php if($v['province_id']==$db->f('province_id')){?>selected="selected"<?php } ?> value="<?php echo $v['province_id']; ?>">
              <?php  echo $v['province']; ?>
              </option>
              <?php } ?>
            </select>          </td>
        </tr>
        <?php
		}
		?>
        <tr>
          <td>&nbsp;</td>
          <td><?php echo lang_show('add_new');?></td>
          <td><input style="width:30px;" name="add_nums" type="text" id="add_nums" value="0" class="text"></td>
          <td><input name="add_citys" type="text" id="add_citys" class="text"></td>
          <td colspan="2"><select class="select" name="add_province_id" id="add_province_id">
              <?php
			foreach($ct as $v)
			{
			?>
              <option <?php if($v['province_id']==$_GET['province_id']){?>selected="selected"<?php } ?> value="<?php echo $v['province_id']; ?>">
              <?php  echo $v['province']; ?>
              </option>
              <?php }?>
            </select>          </td>
        </tr>
        <tr>
          <td></td>
          <td></td>
          <td colspan="4"><input class="btn" type="submit" name="submit" value="<?php echo lang_show('submit');?>"></td>
        </tr>
        <tr>
          <td colspan="6"><div class="page"><?php echo $pages?></div></td>
        </tr>
      </table>
    </form>
  </div>
</div>
</body>
</html>
