<?php
if (!defined('IN_HANFOX')) exit('Access Denied');

require(APP_PATH.'module/category.php');
require(APP_PATH.'module/city.php');
require(APP_PATH.'module/huoyuan.php');

$pageurl = '?mod=huoyuan';
$tplfile = 'huoyuan.html';
$table = $DB->table('huoyuan');

$action = isset($_GET['act']) ? $_GET['act'] : 'list';
$smarty->assign('action', $action); 

if (!$smarty->isCached($tplfile)) {
	/** list */
	if ($action == 'list') {
		$pagename = '货源管理';
		$smarty->assign('site_title', $pagename.' - '.$options['site_name']);
		$smarty->assign('site_path', get_sitepath().' &raquo; '.$pagename);
		
		$pagesize = 10;
		$curpage = intval($_GET['page']);
		if ($curpage > 1) {
			$start = ($curpage - 1) * $pagesize;
		} else {
			$start = 0;
			$curpage = 1;
		}
		
		$where = "w.user_id='".$myself['user_id']."'";
	
		$huoyuan = get_huoyuan_list($where, 'ctime', 'DESC', $start, $pagesize);
		$total = $DB->get_count($table.' w', $where);
		$showpage = showpage($pageurl, $total, $curpage, $pagesize);
		
		$smarty->assign('pagename', $pagename);
		$smarty->assign('huoyuan', $huoyuan);
		$smarty->assign('total', $total);
		$smarty->assign('showpage', $showpage);
	}
	
	/** add */
	if ($action == 'add') {
		$pagename = '货源提交';
		
		#统计当日可提交的站点数量
		/*if ($options['submit_limit'] > 0) {
			$time = time() - (3600 * 12);
			$today_count = $DB->get_count($DB->table('huoyuan'), "huoyuan_ctime>='$time'");
			$submit_limit = $options['submit_limit'] - $today_count;
			$smarty->assign('submit_limit', $submit_limit);
		}*/
		
		$smarty->assign('pagename', $pagename);
		$smarty->assign('site_title', $pagename.' - '.$options['site_name']);
		$smarty->assign('site_path', get_sitepath().' &raquo; '.$pagename);
		$smarty->assign('category_option', get_category_option('huoyuan', 0, 0, 0));	
		$smarty->assign('city_option', get_city_option('area', 0, 0, 0));	
		$smarty->assign('do', 'saveadd');
	}
	
	/** edit */
	if ($action == 'edit') {
		$pagename = '货源编辑';
		
		$huoyuan_id = intval($_GET['wid']);
		$where = "w.user_id='$myself[user_id]' AND w.huoyuan_id='$huoyuan_id'";
		$huoyuan = get_one_huoyuan($where);
		if (!$huoyuan) {
			msgbox('您要修改的内容不存在或无权限！');
		}
		
		$huoyuan['jian_pic'] = get_webthumb($huoyuan['web_pic'],$huoyuan['huoyuan_id']);
		$row['huoyuan_content'] = str_replace('[upload_dir]', $options['site_root'].$options['upload_dir'].'/', $row['huoyuan_content']);
		$smarty->assign('pagename', $pagename);
		$smarty->assign('site_title', $pagename.' - '.$options['site_title']);
		$smarty->assign('site_path', get_sitepath().' &raquo; '.$pagename);	
		$smarty->assign('category_option', get_category_option('huoyuan', 0, $huoyuan['cate_id'], 0));
		$smarty->assign('city_option', get_city_option('area', 0, $huoyuan['area_id'], 0));
		$smarty->assign('huoyuan', $huoyuan);	
		$smarty->assign('do', 'saveedit');
	}
	
	/** save */
	if (in_array($_POST['do'], array('saveadd', 'saveedit'))) {
		$cate_id = intval($_POST['cate_id']);
		$area_id = intval($_POST['area_id']);
		$huoyuan_daifa = intval($_POST['huoyuan_daifa']);
		$huoyuan_name = trim($_POST['huoyuan_name']);
			$huoyuan_wxpic = trim($_POST['huoyuan_wxpic']);
		$huoyuan_tags = trim($_POST['huoyuan_tags']);
		$huoyuan_pic = trim($_POST['huoyuan_pic']);
		$huoyuan_intro = trim($_POST['huoyuan_intro']);
		$huoyuan_content = $_POST['huoyuan_content'];
		$huoyuan_weixin = intval($_POST['huoyuan_weixin']);
		$huoyuan_qq = intval($_POST['huoyuan_qq']);
		$huoyuan_tel = trim($_POST['huoyuan_tel']);
		
		$huoyuan_time = time();
		
		
		
	
		if (empty($huoyuan_name)) {
			msgbox('请输入货源名称！');
		} else {
			if (!censor_words($options['filter_words'], $huoyuan_name)) {
				msgbox('货源名称中含有非法关键词！');	
			}
		}
		
		
		
		
		if (!empty($huoyuan_tags)) {
			if (!censor_words($options['filter_words'], $huoyuan_tags)) {
				msgbox('TAG标签中含有非法关键词！');
			}
			
			$huoyuan_tags = str_replace('，', ',', $huoyuan_tags);
			$huoyuan_tags = str_replace(',,', ',', $huoyuan_tags);
			if (substr($huoyuan_tags, -1) == ',') {
				$huoyuan_tags = substr($huoyuan_tags, 0, strlen($huoyuan_tags) - 1);
			}
		}
			
		if (empty($huoyuan_intro)) {
			msgbox('请输入货源简介！');
		} else {
			if (!censor_words($options['filter_words'], $huoyuan_intro)) {
				msgbox('货源简介中含有非法关键词！');	
			}
		}
		if (empty($huoyuan_content)) {
			msgbox('请输入货源详情！');
		} else {
			if (!censor_words($options['filter_words'], $huoyuan_content)) {
				msgbox('货源详情中含有非法关键词！');	
			}
		}
		
		
		
		
		if ($myself['user_type'] == 'VIP会员') {
		$huoyuan_data = array(
			'cate_id' => $cate_id,
			'area_id' => $area_id,
			'user_id' => $myself['user_id'],
			'huoyuan_daifa' => $huoyuan_daifa,
			'huoyuan_pic' => $huoyuan_pic,
			'huoyuan_name' => $huoyuan_name,
			'huoyuan_wxpic' => $huoyuan_wxpic,
			'huoyuan_tags' => $huoyuan_tags,
			'huoyuan_intro' => $huoyuan_intro,
			'huoyuan_content' => $huoyuan_content,
			'huoyuan_weixin' => $huoyuan_weixin,
			'huoyuan_qq' => $huoyuan_qq,
			'huoyuan_tel' => $huoyuan_tel,
			
			'huoyuan_status' => 3,
			'huoyuan_ctime' => $huoyuan_time,
		);}elseif ($myself['user_type']  == '管理员') {
		$huoyuan_data = array(
			'cate_id' => $cate_id,
			'area_id' => $area_id,
			'user_id' => $myself['user_id'],
			'huoyuan_daifa' => $huoyuan_daifa,
			'huoyuan_pic' => $huoyuan_pic,
			'huoyuan_name' => $huoyuan_name,
			'huoyuan_wxpic' => $huoyuan_wxpic,
			'huoyuan_tags' => $huoyuan_tags,
			'huoyuan_intro' => $huoyuan_intro,
			'huoyuan_content' => $huoyuan_content,
			'huoyuan_weixin' => $huoyuan_weixin,
			'huoyuan_qq' => $huoyuan_qq,
			'huoyuan_tel' => $huoyuan_tel,
			'huoyuan_status' => 3,
			'huoyuan_ctime' => $huoyuan_time,
		);
		}elseif ($myself['user_type']  == '快速收录') {
		$huoyuan_data = array(
			'cate_id' => $cate_id,
			'area_id' => $area_id,
			'user_id' => $myself['user_id'],
			'huoyuan_daifa' => $huoyuan_daifa,
			'huoyuan_pic' => $huoyuan_pic,
			'huoyuan_name' => $huoyuan_name,
			'huoyuan_wxpic' => $huoyuan_wxpic,
			'huoyuan_tags' => $huoyuan_tags,
			'huoyuan_intro' => $huoyuan_intro,
			'huoyuan_content' => $huoyuan_content,
			'huoyuan_weixin' => $huoyuan_weixin,
			'huoyuan_qq' => $huoyuan_qq,
			'huoyuan_tel' => $huoyuan_tel,
			'huoyuan_status' => 2,
			'huoyuan_ctime' => $huoyuan_time,
		);
		}else{
		$huoyuan_data = array(
			'cate_id' => $cate_id,
			'area_id' => $area_id,
			'user_id' => $myself['user_id'],
			'huoyuan_daifa' => $huoyuan_daifa,
			'huoyuan_pic' => $huoyuan_pic,
			'huoyuan_name' => $huoyuan_name,
			'huoyuan_wxpic' => $huoyuan_wxpic,
			'huoyuan_tags' => $huoyuan_tags,
			'huoyuan_intro' => $huoyuan_intro,
			'huoyuan_content' => $huoyuan_content,
			'huoyuan_weixin' => $huoyuan_weixin,
			'huoyuan_qq' => $huoyuan_qq,
			'huoyuan_tel' => $huoyuan_tel,
			'huoyuan_status' => 2,
			'huoyuan_ctime' => $huoyuan_time,
		);
		}
		
		if ($_POST['do'] == 'saveadd') {
    		$query = $DB->query("SELECT huoyuan_id FROM $table WHERE huoyuan_name='$huoyuan_name'");
    		if ($DB->num_rows($query)) {
        		msgbox('您所提交的货源已存在！');
    		}
			$DB->insert($table, $huoyuan_data);
			$insert_id = $DB->insert_id();
		    $DB->query("UPDATE ".$DB->table('categories')." SET cate_postcount=cate_postcount+1 WHERE cate_id=$cate_id");
		$DB->query("UPDATE ".$DB->table('areas')." SET area_postcount=area_postcount+1 WHERE area_id=$area_id");
			msgbox('货源提交成功！', '?mod=pay');	
		} elseif ($_POST['do'] == 'saveedit') {
			$huoyuan_id = intval($_POST['huoyuan_id']);
			$where = array('huoyuan_id' => $huoyuan_id);
			$DB->update($table, $huoyuan_data, $where);
			$DB->query("UPDATE ".$DB->table('categories')." SET cate_postcount=cate_postcount+1 WHERE cate_id=$cate_id");
		$DB->query("UPDATE ".$DB->table('areas')." SET area_postcount=area_postcount+1 WHERE area_id=$area_id");
			
			msgbox('货源编辑成功！', '?mod=pay');
		}
	}
}

smarty_output($tplfile);
?>