<?php
if(!defined('IN_MYMPS')) exit('Forbidden');
require MYMPS_INC."/upfile.fun.php";
if($if_corp != 1) write_msg('您不是商家会员，无此操作权限！');
$ac = in_array($ac,array('list','detail','signin')) ? $ac : 'list';
$id = isset($id) ? intval($id) : '';

if(submit_check('group_submit')){
	
	if(is_array($selectedids)){
		$create_in = create_in($selectedids);
		$query = $db -> query("SELECT * FROM `{$db_mymps}group` $where AND groupid ".$create_in);
		while($row = $db -> fetchRow($query)){
			$delete[$row['id']]['picture'] = $row['picture'];
			$delete[$row['id']]['pre_picture'] = $row['pre_picture'];
		}
		foreach($delete as $k => $v){
			delete_mayi_img($v['picture']);
			delete_mayi_img($v['pre_picture']);
		}
		$db -> query("DELETE FROM `{$db_mymps}group` {$where} AND groupid ".$create_in);
		unset($delete,$row,$query,$create_in);
		write_msg('','?m=group&success=3&status='.$status.'&page='.$page);
	}
	
	$name_file = 'group_image';
	$content = trim($content);
	$des = textarea_post_change(trim($des));
	$cate_id = intval($cate_id);
	$cityid = $cityid ? intval($cityid) : '';
	$areaid = intval($areaid);
	$streetid = intval($streetid);
	$meetdate = intval(strtotime($meetdate));
	$enddate = intval(strtotime($enddate));
	
	if(empty($gname)) write_msg('','?m=group&type=corp&ac=detail&error=37&id='.$id);
	if(empty($content)) write_msg('','?m=group&type=corp&ac=detail&error=35&id='.$id);
	
	if($_FILES[$name_file]['name']){
		check_upimage($name_file);
		$destination = "/group/".date('Ymd')."/";
		$mymps_image = empty($id) ? start_upload($name_file,$destination,0,$mymps_mymps['cfg_group_limit']['width'],$mymps_mymps['cfg_group_limit']['height']) : start_upload($name_file,$destination,0,$mymps_mymps['cfg_group_limit']['width'],$mymps_mymps['cfg_group_limit']['height'],$picture,$pre_picture);
		$picture	 = $mymps_image[0];
		$pre_picture = $mymps_image[1];
		unset($mymps_image,$_FILES);
	}
	
	if(empty($id)){
		$db -> query("INSERT INTO `{$db_mymps}group` (gname,des,content,pre_picture,picture,cate_id,cityid,areaid,gaddress,meetdate,enddate,userid,dateline,glevel) VALUES ('$gname','$des','$content','$pre_picture','$picture','$cate_id','$cityid','$areaid','$gaddress','$meetdate','$enddate','$s_uid','$timestamp','$glevel')");
		/*积分变化*/
		$score_change = get_credit_score();
		$score_changer = $score_change['score']['rank']['group'];
		$db->query("UPDATE `{$db_mymps}member` SET score = score".$score_changer." WHERE userid = '$s_uid'");
		$score_change = $score_changer = NULL;
		$url = '?m=group&type=corp&success=8&ac=detail&id='.$id.'&alert=3';
	} else {
		$db -> query("UPDATE `{$db_mymps}group` SET gname = '$gname',des = '$des',content = '$content',cate_id = '$cate_id',cityid='$cityid',areaid = '$areaid',gaddress='$gaddress',picture='$picture',pre_picture='$pre_picture',meetdate = '$meetdate',dateline = '$timestamp' $where AND groupid = '$id'");
	}
	
	write_msg('',$url ? $url : '?m=group&success=8&ac=detail&id='.$id);
	
} elseif (submit_check('group_signin_submit')){
	
	empty($selectids) && write_msg('','?m=group&type=corp&ac=signin&error=1&page='.$page);
	$db -> query("DELETE FROM `{$db_mymps}group_signin` {$where} AND signid ".create_in($selectedids));
	unset($selectedids);
	write_msg('','?m=group&type=corp&success=3&ac=signin&page='.$page);

} else {
	
	if($ac == 'list') {
		
		require MYMPS_DATA.'/grouplevel.inc.php';
		$rows_num = mymps_count("group",$where);
		$param	  = setParam(array('m','type','ac'));
		$group  = page1("SELECT a.*,b.cate_name FROM `{$db_mymps}group` AS a LEFT JOIN `{$db_mymps}group_category` AS b ON a.cate_id = b.cate_id WHERE a.userid = '$s_uid' ORDER BY a.dateline DESC");
	
	} elseif ($ac == 'detail') {
		
		require MYMPS_DATA.'/grouplevel.inc.php';
		require MYMPS_ROOT.'/plugin/group/include/functions.php';
		if($id){
			$edit = $db -> getRow("SELECT * FROM `{$db_mymps}group` WHERE groupid = '$id'");
			$edit['meetdate'] = date('Y-m-d',$edit['meetdate']);
			$edit['enddate'] = date('Y-m-d',$edit['enddate']);
			$des = de_textarea_post_change($edit['des']);
		}
		$acontent 	= get_editor('content','Member',$edit['content'],'90%','250px');
		
	} elseif ($ac == 'signin') {
		
		require MYMPS_DATA.'/group_signin_status.inc.php';
		
		if(empty($id)) {
			//报名列表
			
			$param = setParam('m','ac','id');
			$rows_num = $db -> getOne("SELECT COUNT(a.signid) FROM `{$db_mymps}group_signin` AS a LEFT JOIN `{$db_mymps}group` AS b ON a.groupid = b.groupid WHERE b.userid = '$s_uid'");
			$signin = page1("SELECT a.*,b.gname FROM `{$db_mymps}group_signin` AS a LEFT JOIN `{$db_mymps}group` AS b ON a.groupid = b.groupid  WHERE b.userid = '$s_uid' ORDER BY dateline DESC");
			
		} else {
			//报名详细
			$signin = $db -> getRow("SELECT a.*,b.gname FROM `{$db_mymps}group_signin` AS a LEFT JOIN `{$db_mymps}group` AS b ON a.groupid = b.groupid WHERE b.userid = '$s_uid' AND a.signid = '$id'");
			$ac = $ac.'_view';
		}
		
	}
	
	$location = location('corp');
	include mayi_tpl('group_'.$ac);
	unset($glevel);
}
?>