<?php  
	include_once("./define.php");
	include_once("./config/config.php");
	include_once('./includes/card_cls.php');
	include_once('./includes/rand_cls.php');
	include_once('./includes/upload_cls.php');
	$do	 = _R('do');
	$act = _R('act','list');
	$shop = $common->getShop($_SESSION['shopcode']);
	$card = new Card($shop['shop_regkey']);
	$msmrand = new msmrand();
	if($_SESSION['shopcode']==$_SESSION['villagecode']){
		$villages = $shop;
	}else{
		if($_SESSION['villagecode']){
			$villages = $common->getShop($_SESSION['villagecode']);
		}else{
			$villages = $shop;
		}		
	}
	//如果自己是代理，强制转换代理是自己
	if($_SESSION['mem_id']){
		$agency = $ass->one('agency','','agency_audit="Y" and agency_mem='.$_SESSION['mem_id']);
		if($agency){
			$_SESSION['agencycode'] = $agency['agency_code'];
		}
	}
	
	if($do){
		switch($do){
			case 'saveFeedback':
				//feedback_name feedback_hand feedback_man feedback_contents
				$feedback_title		= _R('feedback_title');
				$feedback_hand		= _R('feedback_hand');
				$feedback_man		= _R('feedback_man');
				$feedback_contents	= _R('feedback_contents');
				$mem = $ass->one('member','','mem_id="'.$_SESSION['mem_id'].'"');
				$data=array(
					'feedback_title'	=> $feedback_title,
					'feedback_hand'		=> $mem['mem_hand'],
					'feedback_man'		=> $feedback_man?$feedback_man:$mem['mem_name'],
					'feedback_contents' => $feedback_contents,
					'feedback_name'		=> $mem['mem_name'],
					'feedback_mem'		=> $_SESSION['mem_id'],
					'feedback_shop'		=> $shop['shop_id'],
					'feedback_comp'		=> $shop['comp_id'],
					'feedback_time'		=> date('Y-m-d H:i:s'),
				);

				$ret = saveExposeImage($_SESSION['mem_id']);
				if($ret['img1']['ret'])$data['feedback_image1'] = $ret['img1']['path'];
				if($ret['img2']['ret'])$data['feedback_image2'] = $ret['img2']['path'];
				if($ret['img3']['ret'])$data['feedback_image3'] = $ret['img3']['path'];

				if($ass->insert('feedback',$data)){
					$feedback_id = $db->insert_id();
					$messArr = array(
						'first'	=>'您好，您有反馈订单。',
						'keyword1'	=>$feedback_title,//商品明细
						'keyword2'	=>date("Y-m-d H:i:s"),//下单时间
						'keyword3'	=>'反馈信息:'.$feedback_contents,//配送地址
						'keyword4'	=>'联系人:'.$feedback_man,//联系人
						'keyword5'	=>'联系电话:'.$feedback_hand,//付款状态
						'remark'=>'',
					);
					$urls = 'http://'.$_SERVER['SERVER_NAME'];
					$selfArr = explode('/',$_SERVER['PHP_SELF']);
					for($i=1;$i<count($selfArr)-1;$i++){
						$urls.='/'.$selfArr[$i];
					}
					$url = $urls.'/gift.php?act=feedback_info&feedback_id='.$feedback_id.'&shopcode='.$shop['shop_code'];
					//$send = $common->wxNoticeGroup($shop['shop_id'],$shop['bill_comp'],'E',$messArr,$url,'new_bill');
					//if($send['ret']){
					//	$common->sendMessageNotice($shop['shop_id'],'E');
					//}
					echo "<script language='javascript'>";
					echo "window.location.href = 'gift.php?act=index';";				
					echo "</script>";
				}
				break;
			case 'saveAudit':
				$feedback_id	= _R('feedback_id');
				$feedback_opinion	= _R('feedback_opinion');
				$feedback = $ass->one('feedback','','feedback_id='.$feedback_id.' and feedback_shop='.$shop['shop_id']);
				if($feedback){
					if($feedback['feedback_audit']=='N'){
						if($ass->update('feedback',array('feedback_audit'=>'Y','feedback_opinion'=>$feedback_opinion,'feedback_time1'=>date("Y-m-d H:i:s")),'feedback_shop='.$shop['shop_id'].' and feedback_id='.$feedback_id)){
							exit($json->encode(array('ret'=>true,'msg'=>'操作成功！')));
						}else{
							exit($json->encode(array('ret'=>false,'msg'=>'操作失败！')));
						}
					}else{
						exit($json->encode(array('ret'=>false,'msg'=>'已经确定！')));
					}
				}else{
					exit($json->encode(array('ret'=>false,'msg'=>'反馈信息不存在！')));
				}
				break;
			//真伪查询
			case 'searchCard':
				$card_code = _R('card_code');
				if($card_code){
					$card = $ass->one('card_register','','card_code="'.$card_code.'" and card_shop='.$shop['shop_id']);
					if($card){
						//A未激活，B已领卡，C已激活，D已销售
						$state = array(
							'A'=>'未领卡',
							'B'=>'卡未付款',
							'C'=>'卡已激活',
							'D'=>'卡未激活',
						);
						exit($json->encode(array('ret'=>true,'msg'=>$state[$card['card_state']])));
					}else{
						exit($json->encode(array('ret'=>false,'msg'=>'卡号不存在！')));
					}
				}else{
					exit($json->encode(array('ret'=>false,'msg'=>'卡号不能为空！')));
				}
				break;
		}
	}
	switch($act){
		case 'index':
			$mem = $ass->one('member','','mem_id="'.$_SESSION['mem_id'].'"');
			if($mem['mem_hand']){
				$_tpl->assign('shop', $shop);
				$_tpl->display('gift/','gift_index');
			}else{
				$_tpl->assign('villages', $villages);
				$_tpl->assign('parnetphone', $parnetphone);
				$_tpl->assign('shopindex', true);
				$_tpl->assign('mem', $mem);
				$_tpl->display('gift/','bind_phone');
			}
			break;
		case 'gift_help':
			$sql = 'select * from '.$ass->table('shop_ad');
			$sql.= ' where ad_shop='.$shop['shop_id'].' and ad_mode="B" limit 0,5';
			$rst = $db->query($sql);
			$ImageList=array();
			while($row=$db->fetch_array($rst)){
				$ImageList[]= array(
					'img'=>$row['ad_image'],
					'url'=>$row['ad_url']
				);
			}
			$sql = 'select * from '.$ass->table('help');
			$sql.= ' where help_shop='.$shop['shop_id'].' and help_show="Y"';
			$rst = $db->query($sql);
			$list=array();
			while($row=$db->fetch_array($rst)){
				$list[]= array(
					'help_id'	=> $row['help_id'],
					'help_name'	=> $row['help_name'],
					'help_icon'	=> $row['help_icon'],
					'help_text'	=> $row['help_text']
				);
			}
			$_tpl->assign('ImageList', $ImageList);
			$_tpl->assign('list', $list);
			$_tpl->assign('shop', $shop);
			$_tpl->display('gift/','gift_help');
			break;
		case 'help_info':
			$help_id = _R('help_id');
			$sql = 'select * from '.$ass->table('shop_ad');
			$sql.= ' where ad_shop='.$shop['shop_id'].' and ad_mode="B" limit 0,5';
			$rst = $db->query($sql);
			$ImageList=array();
			while($row=$db->fetch_array($rst)){
				$ImageList[]= array(
					'img'=>$row['ad_image'],
					'url'=>$row['ad_url']
				);
			}
			$help = $ass->one('help','','help_id='.$help_id.' and help_shop='.$shop['shop_id']);
			$_tpl->assign('ImageList', $ImageList);
			$_tpl->assign('help', $help);
			$_tpl->assign('shop', $shop);
			$_tpl->display('gift/','help_info');
			break;
		case 'gift_feedback':
			$mem = $ass->one('member','','mem_id="'.$_SESSION['mem_id'].'"');
			if($mem['mem_hand']){
				$sql = 'select * from '.$ass->table('shop_ad');
				$sql.= ' where ad_shop='.$shop['shop_id'].' and ad_mode="B" limit 0,5';
				$rst = $db->query($sql);
				$ImageList=array();
				while($row=$db->fetch_array($rst)){
					$ImageList[]= array(
						'img'=>$row['ad_image'],
						'url'=>$row['ad_url']
					);
				}
				$_tpl->assign('ImageList', $ImageList);
				$_tpl->assign('shop', $shop);
				$_tpl->display('gift/','gift_feedback');
			}else{
				$_tpl->assign('villages', $villages);
				$_tpl->assign('parnetphone', $parnetphone);
				$_tpl->assign('shopindex', true);
				$_tpl->assign('mem', $mem);
				$_tpl->display('gift/','bind_phone');
			}
			break;
		case 'feedback_list':
			$sql = 'select * from '.$ass->table('feedback');
			$sql.= ' where feedback_shop='.$shop['shop_id'].' and feedback_mem='.$_SESSION['mem_id'];
			$rst = $db->query($sql);
			$LIST=array();
			while($row=$db->fetch_array($rst)){
				$LIST[]= array(
					'feedback_id'	=>$row['feedback_id'],
					'feedback_title'=>$row['feedback_title'],
					'feedback_hand'	=>$row['feedback_hand'],
					'feedback_man'=>$row['feedback_man'],
					'feedback_name'=>$row['feedback_name'],
					'feedback_contents'=>$row['feedback_contents'],
					'feedback_time'=>$row['feedback_time'],
					'feedback_audit'=>$row['feedback_audit'],
					'feedback_image1'=>$row['feedback_image1']
				);
			}
			$_tpl->assign('LIST', $LIST);
			$_tpl->display('gift/','feedback_list');
			break;
		case 'feedback_info':
			$feedback_id = _R('feedback_id');
			$sql = 'select * from '.$ass->table('shop_ad');
			$sql.= ' where ad_shop='.$shop['shop_id'].' and ad_mode="B" limit 0,5';
			$rst = $db->query($sql);
			$ImageList=array();
			while($row=$db->fetch_array($rst)){
				$ImageList[]= array(
					'img'=>$row['ad_image'],
					'url'=>$row['ad_url']
				);
			}
			$feedback = $ass->one('feedback','','feedback_id='.$feedback_id.' and feedback_shop='.$shop['shop_id']);
			//只有管理员才能审核
			if($ass->number('member_manage','manage_mem='.$_SESSION['mem_id'].' and manage_shop='.$shop['shop_id'])>0){
				$ismanage = true;
			}else{
				$ismanage = false;
			}
			$_tpl->assign('ismanage', $ismanage);
			$_tpl->assign('ImageList', $ImageList);
			$_tpl->assign('feedback', $feedback);
			$_tpl->assign('shop', $shop);
			$_tpl->display('gift/','feedback_info');
			break;
		case 'board_info':
			$sql = 'select * from '.$ass->table('shop_ad');
			$sql.= ' where ad_shop='.$shop['shop_id'].' and ad_mode="B" limit 0,5';
			$rst = $db->query($sql);
			$ImageList=array();
			while($row=$db->fetch_array($rst)){
				$ImageList[]= array(
					'img'=>$row['ad_image'],
					'url'=>$row['ad_url']
				);
			}
			$_tpl->assign('ImageList', $ImageList);
			$_tpl->assign('shop', $shop);
			$_tpl->display('gift/','board_info');
			break;
		//真伪查询
		case 'true_search':
			$sql = 'select * from '.$ass->table('shop_ad');
			$sql.= ' where ad_shop='.$shop['shop_id'].' and ad_mode="B" limit 0,5';
			$rst = $db->query($sql);
			$ImageList=array();
			while($row=$db->fetch_array($rst)){
				$ImageList[]= array(
					'img'=>$row['ad_image'],
					'url'=>$row['ad_url']
				);
			}
			$_tpl->assign('ImageList', $ImageList);
			$_tpl->assign('shop', $shop);
			$_tpl->display('gift/','true_search');
			break;
		//提货兑换
		case 'activate_member':
			$addline = trim(_R('addline'));
			if($addline){
				$_ADDRESS = $ass->one('member_address','','mem_id='.$_SESSION['mem_id'].' and address_line='.$addline);
			}else{
				$_ADDRESS = $ass->one('member_address','','mem_id='.$_SESSION['mem_id'].' and address_default="Y"');
			}
			$_ADDRESS['address_string'] = $_ADDRESS['province_name'].$_ADDRESS['city_name'].$_ADDRESS['region_name'].$_ADDRESS['address_info'];
			
			$sql = 'select * from '.$ass->table('shop_ad');
			$sql.= ' where ad_shop='.$shop['shop_id'].' and ad_mode="B" limit 0,5';
			$rst = $db->query($sql);
			$ImageList=array();
			while($row=$db->fetch_array($rst)){
				$ImageList[]= array(
					'img'=>$row['ad_image'],
					'url'=>$row['ad_url']
				);
			}
			$_tpl->assign('ImageList', $ImageList);
			$_tpl->assign('addline', $_ADDRESS['address_line']);
			$_tpl->assign('_ADDRESS', $_ADDRESS);
			$_tpl->display('gift/','activate_member');
			break;
	}

	function saveExposeImage($mem_id)
	{
		global $ass,$_CONFIG,$_INI;
		$upload = "../ueditor/";
		if(!is_dir($path)){
			@mkdir($path);
			@chmod($upload,0777);
		}
		$upload = "../ueditor/mem/";
		if(!file_exists($upload)){
			@mkdir($upload);
			@chmod($upload,0777);
		}
		$upload = "../ueditor/mem/feedback/";
		if(!file_exists($upload)){
			@mkdir($upload);
			@chmod($upload,0777);
		}
		$upload = "../ueditor/mem/feedback/".$_SESSION['mem_id'];
		if(!file_exists($upload)){
			@mkdir($upload);
			@chmod($upload,0777);
		}

		$uploadClass = new upfile("jpg,jpeg,gif,png,bmp",$upload);
		$ret = array('logo'=>array('ret'=>false,'msg'=>''),'image1'=>array('ret'=>false,'msg'=>''),'image2'=>array('ret'=>false,'msg'=>''));
		$allow = array('image/gif','image/jpeg','image/pjpeg','image/png');

		if(isset($_FILES['feedback_image1']) && $_FILES['feedback_image1'] && $_FILES['feedback_image1']['tmp_name'])
		{
			$ext = get_extension($_FILES['feedback_image1']['name']);
			if(in_array($_FILES['feedback_image1']['type'],$allow)){
				$fname = $upload.'/'.'feedback_image1'.time().'.'.$ext;				
				//移动文件
				if(@copy($_FILES['feedback_image1']['tmp_name'],$fname)){
					$ret['img1'] = array('ret'=>true,'path'=>$fname);
				}else{
					$ret['img1'] = array('ret'=>false,'msg'=>'文件上传失败！');
				}
			}else{
				$ret['img1'] = array('ret'=>false,'msg'=>'格式不正确！');
			}
		}
		if(isset($_FILES['feedback_image2']) && $_FILES['feedback_image2'] && $_FILES['feedback_image2']['tmp_name'])
		{
			$ext = get_extension($_FILES['feedback_image2']['name']);
			if(in_array($_FILES['feedback_image2']['type'],$allow)){
				$fname = $upload.'/'.'feedback_image2'.time().'.'.$ext;				
				//移动文件
				if(@copy($_FILES['feedback_image2']['tmp_name'],$fname)){
					$ret['img2'] = array('ret'=>true,'path'=>$fname);
				}else{
					$ret['img2'] = array('ret'=>false,'msg'=>'文件上传失败！');
				}
			}else{
				$ret['img2'] = array('ret'=>false,'msg'=>'格式不正确！');
			}
		}
		if(isset($_FILES['feedback_image3']) && $_FILES['feedback_image3'] && $_FILES['feedback_image3']['tmp_name'])
		{
			$ext = get_extension($_FILES['feedback_image3']['name']);
			if(in_array($_FILES['feedback_image3']['type'],$allow)){
				$fname = $upload.'/'.'feedback_image3'.time().'.'.$ext;				
				//移动文件
				if(@copy($_FILES['feedback_image3']['tmp_name'],$fname)){
					$ret['img3'] = array('ret'=>true,'path'=>$fname);
				}else{
					$ret['img3'] = array('ret'=>false,'msg'=>'文件上传失败！');
				}
			}else{
				$ret['img3'] = array('ret'=>false,'msg'=>'格式不正确！');
			}
		}
		return $ret;
	}
	function get_extension($file)
	{
		return strtolower(substr($file, strrpos($file, '.')+1));
	}
?>  