<?php
	!defined('DIRCMS_MEMBER') && exit('Access Denied!');
	if(!$_iscompmayed)showmsg('Bad Request!!!',$DIRCMS['site_url']); //非法请求
	if(!in_array($_gradeid,$usepriv) && $_groupid!=1)
	{
		showmsg('你没有查看该页的权限!');
	}
	$action=empty($action)?'manage':trim($action);
	require_once(DIRCMS_ROOT.'include/form.class.php');	
	require_once(DIRCMS_ROOT.'include/list.class.php');
	$l=new dircmslist();
	switch($action)
	{
		case 'news_manage':
			if(!in_array($_gradeid,$newspriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			$where='userid='.$_userid;
			$k=trim($k);
			$where=!empty($k)?$where." AND title like '%{$k}%'":$where;
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'co_news',$where,$orderby,$page,20);
			$pages=$l->pages;
			include member_tlp('co_news_manage');
			break;
		case 'news_add':
			if(!in_array($_gradeid,$newspriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			if(isset($do_submit))
			{
				if($inputcheckcode!=$_SESSION['checkcode'])showmsg('验证码不正确!');
				if(!trim($info['title']))showmsg('新闻标题不能为空!');
				if(!trim($info['content']))showmsg('新闻内容不能为空!');
				$info['inputtime']=TIME;$info['updatetime']=TIME;
				$info['userid']=$_userid;
				$newscheckpriv=cache_read('newscheckpriv.cache.php',DIRCMS_ROOT.'company/cache/');
				$info['status']=(in_array($_gradeid,$newscheckpriv)||$_groupid==1)?1:0; //////////审核权限可以在后台配置

				unset($info['clicks'],$info['comments']); // 防止作弊 
				$r=$db->fetch_one("SELECT `".DB_PRE."corporatemember`.`companyname` FROM	`".DB_PRE."corporatemember` WHERE `".DB_PRE."corporatemember`.`userid`={$_userid}");
				$info['company']=$r['companyname'];
				$id=$db->insert(DB_PRE."co_news",$info);
				createNews($id);
				createYp();
				showmsg('企业新闻添加成功!',$DIRCMS['site_url'].'member/index.php?file=co_content&action=news_add');
			}
			$f=new form();
			include member_tlp('co_news_add');
			break;
		case 'news_edit':
			if(!in_array($_gradeid,$newspriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			if(isset($do_submit))
			{
				if($inputcheckcode!=$_SESSION['checkcode'])showmsg('验证码不正确!');
				if(!trim($info['title']))showmsg('新闻标题不能为空!');
				if(!trim($info['content']))showmsg('新闻内容不能为空!');
				$r=$db->fetch_one("SELECT userid FROM ".DB_PRE."co_news WHERE id=".intval($id));
				if(!$r || $r['userid']!=$_userid)showmsg_nourl('错误的请求！');
				$info['updatetime']=TIME;
				$newscheckpriv=cache_read('newscheckpriv.cache.php',DIRCMS_ROOT.'company/cache/');
				unset($info['status']); 

				unset($info['userid'],$info['clicks'],$info['comments']); // 防止作弊
				$r=$db->fetch_one("SELECT `".DB_PRE."corporatemember`.`companyname` FROM	`".DB_PRE."corporatemember` WHERE `".DB_PRE."corporatemember`.`userid`={$_userid}");
				$info['company']=$r['companyname'];
				$db->update(DB_PRE."co_news",$info,'id='.intval($id));
				createNews($id);
				createYp();
				showmsg('企业新闻编辑成功!');
			}
			$id=intval($id);
			$c=$db->fetch_one("SELECT * FROM `".DB_PRE."co_news` WHERE `".DB_PRE."co_news`.`id`=".intval($id)." AND `".DB_PRE."co_news`.`userid`={$_userid}");	
			@extract($c);
			$f=new form();
			include member_tlp('co_news_edit');
			break;
		case 'news_delete':
			if(!in_array($_gradeid,$newspriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			$id=intval($id);
			$db->query("DELETE FROM `".DB_PRE."co_news` WHERE `".DB_PRE."co_news`.`id`={$id} AND `".DB_PRE."co_news`.`userid`={$_userid}",true);
			@unlink(DIRCMS_ROOT.'company/shownews_'.$id.'.html');
			createYp();
			showmsg('企业新闻删除成功!',$DIRCMS['site_url'].'member/index.php?file=co_content&action=news_manage');
			break;
		case 'product_manage':
			if(!in_array($_gradeid,$productpriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			$where='userid='.$_userid;
			$k=trim($k);
			$where=!empty($k)?$where." AND product like '%{$k}%'":$where;
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'co_product',$where,$orderby,$page,20);
			$pages=$l->pages;
			$r=cache_read('product_type'.$_userid.'.cache.php',DIRCMS_ROOT.'company/cache/product/');
			$type=$r?$r:array();
			include member_tlp('co_product_manage');
			break;
		case 'product_add':
			if(!in_array($_gradeid,$productpriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			if(isset($do_submit))
			{
				if($inputcheckcode!=$_SESSION['checkcode'])showmsg('验证码不正确!');
				if(!trim($info['product']))showmsg('产品名称不能为空!');
				if(!trim($info['content']))showmsg('产品描述不能为空!');
				$info['inputtime']=TIME;$info['updatetime']=TIME;
				$info['userid']=$_userid;
				$productcheckpriv=cache_read('productcheckpriv.cache.php',DIRCMS_ROOT.'company/cache/');
				$info['status']=(in_array($_gradeid,$productcheckpriv)||$_groupid==1)?1:0; //////////审核权限可以在后台配置

				unset($info['clicks'],$info['comments']); // 防止作弊 
				$r=$db->fetch_one("SELECT `".DB_PRE."corporatemember`.`companyname` FROM	`".DB_PRE."corporatemember` WHERE `".DB_PRE."corporatemember`.`userid`={$_userid}");
				$info['pruductimg']=$info['thumb']?$info['thumb']:'images/bignopic.gif';
				$info['company']=$r['companyname'];
				$id=$db->insert(DB_PRE."co_product",$info);
				$colname=intval($info['typeid']);
				createProcduct($id);
				createYp();
				showmsg('企业产品添加成功!',$DIRCMS['site_url'].'member/index.php?file=co_content&action=product_add');
			}
			$f=new form();
			$r=cache_read('product_type'.$_userid.'.cache.php',DIRCMS_ROOT.'company/cache/product/');
			$type=$r?$r:array();
			include member_tlp('co_product_add');
			break;
		case 'product_edit':
			if(!in_array($_gradeid,$productpriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			if(isset($do_submit))
			{
				if($inputcheckcode!=$_SESSION['checkcode'])showmsg('验证码不正确!');
				if(!trim($info['product']))showmsg('产品名称不能为空!');
				if(!trim($info['content']))showmsg('产品描述不能为空!');
				$colname=intval($info['typeid']);
				$r=$db->fetch_one("SELECT userid FROM ".DB_PRE."co_product WHERE id=".intval($id));
				if(!$r || $r['userid']!=$_userid)showmsg_nourl('错误的请求！');
				$info['updatetime']=TIME;
				$productcheckpriv=cache_read('productcheckpriv.cache.php',DIRCMS_ROOT.'company/cache/');
				unset($info['status']); 

				unset($info['userid'],$info['clicks'],$info['comments']); // 防止作弊
				$r=$db->fetch_one("SELECT `".DB_PRE."corporatemember`.`companyname` FROM	`".DB_PRE."corporatemember` WHERE `".DB_PRE."corporatemember`.`userid`={$_userid}");
				$info['pruductimg']=$info['thumb']?$info['thumb']:'images/bignopic.gif';
				$info['company']=$r['companyname'];
				$db->update(DB_PRE."co_product",$info,'id='.intval($id));
				createProcduct($id);
				createYp();
				showmsg('企业产品编辑成功!');
			}
			$id=intval($id);
			$c=$db->fetch_one("SELECT * FROM `".DB_PRE."co_product` WHERE `".DB_PRE."co_product`.`id`=".intval($id)." AND `".DB_PRE."co_product`.`userid`={$_userid}");	
			@extract($c);
			$f=new form();
			$r=cache_read('product_type'.$_userid.'.cache.php',DIRCMS_ROOT.'company/cache/product/');
			$type=$r?$r:array();
			include member_tlp('co_product_edit');
			break;
		case 'product_type':
			if(!in_array($_gradeid,$productpriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			$r=cache_read('product_type'.$_userid.'.cache.php',DIRCMS_ROOT.'company/cache/product/');
			$type=$r?$r:array();
			if(isset($do_submit))
			{
				$t=array();
				foreach($key as $k=>$v)
				{
					$t[$v]=$val[$k];
				}
				if(trim($newval) && !array_key_exists($newkey,$t))$t[$newkey]=$newval;
				cache_write('product_type'.$_userid.'.cache.php',$t,DIRCMS_ROOT.'company/cache/product/');
				showmsg('产品类型编辑成功!');
			}
			include member_tlp('co_product_type');
			break;
		case 'product_delete':
			if(!in_array($_gradeid,$productpriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			$id=intval($id);
			$db->query("DELETE FROM `".DB_PRE."co_product` WHERE `".DB_PRE."co_product`.`id`={$id} AND `".DB_PRE."co_product`.`userid`={$_userid}",true);
			@unlink(DIRCMS_ROOT.'compant/showproduct_'.$id.'.html');
			createYp();
			showmsg('企业产品删除成功!',$DIRCMS['site_url'].'member/index.php?file=co_content&action=product_manage');
			break;
		case 'coupon_manage':
			if(!in_array($_gradeid,$couponpriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			$where='userid='.$_userid;
			$k=trim($k);
			$where=!empty($k)?$where." AND title like '%{$k}%'":$where;
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'co_coupon',$where,$orderby,$page,20);
			$pages=$l->pages;
			include member_tlp('co_coupon_manage');
			break;
		case 'coupon_add':
			if(!in_array($_gradeid,$couponpriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			if(isset($do_submit))
			{
				if($inputcheckcode!=$_SESSION['checkcode'])showmsg('验证码不正确!');
				if(!trim($info['title']))showmsg('优惠促销标题不能为空!');
				if(!trim($info['content']))showmsg('优惠促销内容不能为空!');
				$info['inputtime']=TIME;$info['updatetime']=TIME;
				$info['userid']=$_userid;
				$couponcheckpriv=cache_read('couponcheckpriv.cache.php',DIRCMS_ROOT.'company/cache/');
				$info['status']=(in_array($_gradeid,$couponcheckpriv)||$_groupid==1)?1:0; //////////审核权限可以在后台配置
				unset($info['clicks'],$info['comments']); // 防止作弊 
				$r=$db->fetch_one("SELECT `".DB_PRE."corporatemember`.`companyname` FROM	`".DB_PRE."corporatemember` WHERE `".DB_PRE."corporatemember`.`userid`={$_userid}");
				$info['company']=$r['companyname'];
				$info['titleimg']=$info['thumb'];
				$id=$db->insert(DB_PRE."co_coupon",$info);
				createCoupon($id);
				createYp();
				showmsg('优惠促销添加成功!',$DIRCMS['site_url'].'member/index.php?file=co_content&action=coupon_add');
			}
			$f=new form();
			include member_tlp('co_coupon_add');
			break;
		case 'coupon_edit':
			if(!in_array($_gradeid,$couponpriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			if(isset($do_submit))
			{
				if($inputcheckcode!=$_SESSION['checkcode'])showmsg('验证码不正确!');
				if(!trim($info['title']))showmsg('优惠促销标题不能为空!');
				if(!trim($info['content']))showmsg('优惠促销内容不能为空!');
				$r=$db->fetch_one("SELECT userid FROM ".DB_PRE."co_coupon WHERE id=".intval($id));
				if(!$r || $r['userid']!=$_userid)showmsg_nourl('错误的请求！');
				$info['updatetime']=TIME;
				$couponcheckpriv=cache_read('couponcheckpriv.cache.php',DIRCMS_ROOT.'company/cache/');
				unset($info['status']); 

				unset($info['userid'],$info['clicks'],$info['comments']); // 防止作弊
				$r=$db->fetch_one("SELECT `".DB_PRE."corporatemember`.`companyname` FROM	`".DB_PRE."corporatemember` WHERE `".DB_PRE."corporatemember`.`userid`={$_userid}");
				$info['company']=$r['companyname'];
				$info['titleimg']=$info['thumb'];
				$db->update(DB_PRE."co_coupon",$info,'id='.intval($id));
				createCoupon($id);
				createYp();
				showmsg('优惠促销编辑成功!');
			}
			$id=intval($id);
			$c=$db->fetch_one("SELECT * FROM `".DB_PRE."co_coupon` WHERE `".DB_PRE."co_coupon`.`id`=".intval($id)." AND `".DB_PRE."co_coupon`.`userid`={$_userid}");	
			@extract($c);
			$f=new form();
			include member_tlp('co_coupon_edit');
			break;
		case 'coupon_delete':
			if(!in_array($_gradeid,$couponpriv) && $_groupid!=1)
			{
				showmsg('你没有查看该页的权限!');
			}
			$id=intval($id);
			$db->query("DELETE FROM `".DB_PRE."co_coupon` WHERE `".DB_PRE."co_coupon`.`id`={$id} AND `".DB_PRE."co_coupon`.`userid`={$_userid}",true);
			@unlink(DIRCMS_ROOT.'compant/showcoupon_'.$id.'.html');
			createYp();
			showmsg('优惠促销删除成功!',$DIRCMS['site_url'].'member/index.php?file=co_content&action=coupon_manage');
			break;
		default:
			showmsg('参数有误,返回网站首页!',$DIRCMS['site_url']); //非法请求
			break;
	}
?>