<?php
$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
include_once($conf_basedir."base.inc.php");
checkAuth('78');	//判断登录和权限


//操作人和城市
$operator = $esfadmin['truename'];
$city = $esfadmin['now_city'];

if($do == 'exchange'){ //礼品兑换明细
	if($id){
		//分页参数
		$pagesize = 20;
		$page_now = $page_now ? $page_now : 1;
		//获得数据当前页面数据
		$sql = "select count(*) from  growth_gift_exchange  where gid = '{$id}'";
		$total_num = $dbback->GetOne($sql);
		 
		$page_total = ceil($total_num/$pagesize);
		if ($page_now != '' && $page_now > $page_total) {
			$page_now = $page_total;
		}elseif($page_now <  0){
			$page_now = 1;
		}

		$start = ($page_now-1)*$pagesize;
		if($total_num <= 0 || $page_total <=0){
			$pages = 0 ;  //计算总页数
		}else{
			$new_pagenavi = new_pagenavi2($page_now,$page_total,$total_num);
		}

		$sql = " select e.*,g.credit_score,g.name,b.truename,b.username,b.master from  growth_gift_exchange as e left join  growth_gift as g on e.gid=g.id left join broker as b  on b.uid=e.bid  where e.gid = '{$id}' limit ".$start.",".$pagesize.";";
		$data_info = $dbback->GetAll($sql);
		$title = '礼品兑换明细';
		include_once(Template("header"));
		include_once(Template("growth_nj/gift_exchange"));
		include_once(Template("footer"));
	}
    
}elseif($do == 'new'){ //新建兑换礼品
    $title = '新建兑换礼品';
    
	//获取经纪人等级
    $GrowthLevel = new GrowthLevel();
	$broker_level_info = $GrowthLevel->findAllLevelData();
	$borker_level = array();
	if(is_array($broker_level_info)){
		foreach($broker_level_info as $key=>$val){
			$borker_level[$val['name']] = $val['name'];
		}
	}

	$checkbox_html = create_htmlable('level', 'radio', $value = array(), $borker_level);

	include_once(Template("header"));
	include_once(Template("growth_nj/gift_new"));
	include_once(Template("footer"));
}elseif($do == 'giftInsert'){ //保存新建兑换礼品   	
    $name = trim($name);
	$num =  intval($num);
	$credit_score = intval($credit_score);
    $pic = trim($p_filename[0]);
	$broker_level = $level;

	if(!$name || !$num || !$credit_score || !$pic || !$etime || !$broker_level){
		js_alert('数据参数错误,保存失败！');die; //数据检查不合格
	}

	$data = array();
	$data['uname'] = $operator;
    $data['name'] = $name;
	$data['num'] = $num;
	$data['credit_score'] = $credit_score;
	$data['broker_level'] =  $broker_level ;
	$data['pic'] = $pic;
	$data['open_flag'] = $open_flag;
	$data['etime'] = $etime;
 
    //插入数据库
	$sql = SqlCombinHelper::insert_sql('growth_gift', $data);
	$db->Execute($sql);
	$id =$db->Insert_ID();
    if($id){
		js_alert('保存成功','./gift_modify.php?do=edit&id='.$id,3);die;
	}else{
		js_alert('保存失败！');die;
	}


}elseif($do == 'giftEdit'){ //保存编辑兑换礼品
	//检测
    $sql = "select * from growth_gift  where id = '{$id}'";
    $data_info = $dbback->GetRow($sql);
	if($data_info['del_flag'] == 1){
		js_alert('该礼品已经删除，不能编辑！');die;
	}
   
	if($type == 'giftEditOpen'){//单独编辑数量和开启状态
		$num =  intval($num);
	    if(!$num && !isset($open_flag)){
			js_alert('数据参数错误,保存失败！');die; //数据检查不合格
		}
		$data = array();
		$data['num'] = $num;
		$data['open_flag'] = $open_flag;
		$strWhere = " id = {$id} ";

		//更新数据库
		$sql = SqlCombinHelper::update_sql('growth_gift', $data, $strWhere);
		$db->Execute($sql);
		$result =$db->Affected_Rows();
		if($result == 1){
			//记录日志
			$data['id'] = $id;
			$description = json_encode($data);
			$operateRecord->insertData(94, '修改礼品数量和状态', $description, $operator);
			js_alert('更新成功！',$back_url,3);die;
		}else{
			js_alert('请勿重复提交！');die;
		}
	}else{
		$name = trim($name);
		$num =  intval($num);
		$credit_score = intval($credit_score);
		$pic = trim($p_filename[0]);
		$broker_level = $level;
		 
		if(!$id || !$name || !$num || !$credit_score || !$pic || !$etime || !$broker_level){
			js_alert('数据参数错误,保存失败！');die; //数据检查不合格
		}

		$data = array();
		$data['name'] = $name;
		$data['num'] = $num;
		$data['credit_score'] = $credit_score;
		$data['pic'] =  $pic ;
		$data['broker_level'] =  $broker_level ;
		$data['etime'] = $etime;
		$data['open_flag'] = $open_flag;
		$strWhere = " id = {$id} ";
		//更新数据库
		$sql = SqlCombinHelper::update_sql('growth_gift', $data, $strWhere);
		$db->Execute($sql);
		$result =$db->Affected_Rows();
		if($result == 1){
			//记录日志
			$data['id'] = $id;
			$description = json_encode($data);
			$operateRecord->insertData(93, '修改礼品信息', $description, $operator);
			js_alert('更新成功！',$back_url,3);die;
		}else{
			js_alert('请勿重复提交！');die;
		}
	
	}

}elseif($do == 'exchangeOk'){ //已经领取礼品确认
	if(!$id){
		echo 201;die;
	}

	$data = array('status'=>1);
	$sql = SqlCombinHelper::update_sql('growth_gift_exchange', $data, "id = '{$id}'");
    $db->Execute($sql);
	$result = $db->Affected_Rows();
	if($result == 1){
		//记录日志
		$description = json_encode(array('id'=>$id,'table'=>'growth_gift_exchange'));
		$operateRecord->insertData(91, '确认礼品兑换', $description, $operator);
		echo 100;die;
	}else{
		echo 200;die;
	}
	
}elseif($do == 'edit'){ //编辑礼品信息
	if(!$id){
		die('参数错误！');
	}
	$title = '编辑兑换礼品';
    
	//检测
	$sql = "select * from growth_gift  where id = '{$id}'";
    $data_info = $dbback->GetRow($sql);
	if($data_info['del_flag'] == 1){
		js_alert('该礼品已经删除，不能编辑！',1);die;
	}

    //获取经纪人等级
    $GrowthLevel = new GrowthLevel();
	$broker_level_info = $GrowthLevel->findAllLevelData();
	$borker_level = array();
	if(is_array($broker_level_info)){
		foreach($broker_level_info as $key=>$val){
			$borker_level[$val['name']] = $val['name'];
		}
	}

	$checkbox_html = create_htmlable('level', 'radio', $data_info['broker_level'], $borker_level);
    
    include_once(Template("header"));
	if($data_info['open_flag'] == 1){
		include_once(Template("growth_nj/gift_edit_open"));
	}else{
		include_once(Template("growth_nj/gift_edit"));
	}
	
	include_once(Template("footer"));
 
}elseif($do == 'del'){ //删除礼品
	if(!$id){
		echo 201;die; //参数有误
	}
    
	$data = array('del_flag'=>1);
	$sql = SqlCombinHelper::update_sql('growth_gift', $data, "id = '{$id}'");
    $db->Execute($sql);
    $result = $db->Affected_Rows();
	if($result == 1){
		//记录日志	
		$description = json_encode(array('id'=>$id,'table'=>'growth_gift'));
		$operateRecord->insertData(92, '删除礼品', $description, $operator);
		echo 100;die;
	}else{
		echo 200;die; 
	}
}elseif($action == 'downdata'){ //下载兑换数据
	if($id){
		$sql = " select e.*,g.credit_score,g.name,b.truename,b.username from  growth_gift_exchange as e left join  growth_gift as g on e.gid=g.id left join broker as b  on b.uid=e.bid  where e.gid = '{$id}';";
		$data_info = $dbback->GetAll($sql);
	}

	$data = "礼品,账号,姓名,兑换时间,兑换房豆,领取状态
	";

	if(is_array($data_info) && !empty($data_info)){
		foreach($data_info as $key=>$val){
            $status = $val['status'] == 1 ? '已领取' : '未领取';
			$data .=$val['name'].','.$val['username'].','.$val['truename'].','.$val['ctime'].','.$val['credit_score'].",".$status."
			";
		}
	}

	$mime_type = 'text/x-csv';
	header('Content-Type: ' . $mime_type);
    header("Content-Disposition: attachment; filename=".$val['name']."礼品兑换报表".$lastym.".csv");
    $actiontype = '';
    echo $data;
    exit();
}













 




?>