<?php
define('CURSCRIPT','corp');

require(dirname(__FILE__)."/global.php");
require(MYMPS_INC."/db.class.php");

(!defined('IN_ADMIN') || !defined('IN_MYMPS')) && exit('Access Denied');

if($admin_cityid) write_msg('您没有权限访问该页！');

$part = $part ? $part : 'list' ;

if(!submit_check(CURSCRIPT.'_submit')){
	
	if($part == 'list'){
		
		!chk_adm_purview("purview_商家分类")&& write_msg("很抱歉，您所在会员组没有当前菜单的操作权限！");
		
		$corp = cat_list('corp',0,0,false);
		$here = "商家分类";
		include(mayi_tpl("corp_list"));
	} elseif($part == 'add'){
		
		!chk_adm_purview("purview_增加分类")&& write_msg("很抱歉，您所在会员组没有当前菜单的操作权限！");
		
		$maxorder = $db->getOne("SELECT MAX(corporder) FROM {$db_mymps}corp");
		$maxorder = $maxorder + 1;
		$here = "增加分类";
		include(mayi_tpl("corp_add"));
	} elseif($part == 'edit'){
		$corp = $db->getRow("SELECT * FROM {$db_mymps}corp WHERE corpid = '$corpid'");
		$here = "编辑商家分类";
		include(mayi_tpl("corp_edit"));
	} elseif($part == 'del'){
		if(empty($corpid)) write_msg('没有选择记录');
		
		mymps_delete("corp","WHERE corpid = '$corpid'");
		mymps_delete("corp","WHERE parentid = '$corpid'");
		
		clear_cache_files('corp_option_static');
		clear_cache_files('corp_pid_releate');
		clear_cache_files('corp_tree');
		write_msg("删除商家分类 $corpid 成功","?part=list","Mymps_record");
	}
	
} else {
	
	if($part == 'add'){
		
		if(empty($corpname))write_msg("请填写商家分类名称");
		
		$corpname  = explode('|',trim($corpname));
		
		if(empty($corporder)){
			$maxorder = $db->getOne("SELECT MAX(corporder) FROM {$db_mymps}corp");
			$corporder = $maxorder + 1;
		}
		
		if(is_array($corpname)){
			foreach ($corpname as $key => $value){
				$value = trim($value);
				$corporder ++;
				$len = strlen($value);
				if($len < 2 || $len > 30){
					write_msg("分类名必须在2个至30个字符之间");
					exit();
				}
				$db->query("INSERT INTO {$db_mymps}corp (corpname,parentid,corporder) VALUES ('$value','$parentid','$corporder')");
			}

		}
		clear_cache_files('corp_option_static');
		clear_cache_files('corp_pid_releate');
		clear_cache_files('corp_tree');
		
		write_msg('成功更新商家分类！','corp.php?part=list','write_record');
		
	} elseif($part == 'edit') {
		
		if(empty($corpname))write_msg("请填写商家分类名称");
		$len = strlen($corpname);
		$corpid == $parentid && write_msg("隶属分类不能为自己！");
		if($len < 2 || $len > 30)write_msg("类别名称必须在2个至30个字符之间");
		
		$sql = "UPDATE {$db_mymps}corp SET corpname='$corpname',
		parentid='$parentid',
		corporder='$corporder'
		WHERE corpid = '$corpid'";
		$res = $db->query($sql);
		
		clear_cache_files('corp_option_static');
		clear_cache_files('corp_pid_releate');
		clear_cache_files('corp_tree');
		
		write_msg('成功更新商家分类！','corp.php?part=list','write_record');
		
	} elseif($part == 'list' && is_array($corporder)) {
	
		foreach($corporder as $key => $value){
			$db->query("UPDATE `{$db_mymps}corp` SET corporder = '$value' WHERE corpid = ".$key);		
		}
		
		clear_cache_files('corp_option_static');
		clear_cache_files('corp_pid_releate');
		clear_cache_files('corp_tree');
		
		write_msg('成功更新商家分类！','corp.php?part=list','write_record');
	
	}
	
}

is_object($db) && $db->Close();
$db = $mymps_global = $part = $action = $here = NULL;
?>