<?php
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . '/includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);
$exc = new exchange($ecs->table("facility"), $db, 'id', 'name');

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'list';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/*------------------------------------------------------ */
//-- 商品分类列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    /* 获取分类列表 */
    $fac_list = fac_list();
    /* 模板赋值 */
    $smarty->assign('ur_here',      '設施清單');
    $smarty->assign('action_link',  array('href' => 'facility.php?act=add', 'text' => '新增設施'));
    $smarty->assign('full_page',    1);

    $smarty->assign('fac_list',     $fac_list['fac_list']);

    /* 列表页面 */
    assign_query_info();
    $smarty->display('facility_list.htm');
}

/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $fac_list = fac_list();
    $smarty->assign('fac_list',     $fac_list['fac_list']);

    make_json_result($smarty->fetch('facility_list.htm'));
}
/*------------------------------------------------------ */
//-- 添加商品分类
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
    /* 权限检查 */
//    admin_priv('cat_manage');



    /* 模板赋值 */
    $smarty->assign('ur_here',      '新增設施');
    $smarty->assign('action_link',  array('href' => 'facility.php?act=list', 'text' => '設施清單'));

//    $smarty->assign('cat_select',   cat_list(0, 0, true));
    $smarty->assign('form_act',     'insert');
    $smarty->assign('fac',     array('is_show' => 1,'sort_order'=>50,'id'=>0));



    /* 显示页面 */
    assign_query_info();
    $smarty->display('facility_info.htm');
}

/*------------------------------------------------------ */
//-- 商品分类添加时的处理
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'insert')
{
    /* 权限检查 */
//    admin_priv('cat_manage');

    /* 初始化变量 */
    $fac['id']       = !empty($_POST['id'])       ? intval($_POST['id']): 0;
    $fac['sort_order']   = !empty($_POST['sort_order'])   ? intval($_POST['sort_order']) : 0;
    $fac['name']     = !empty($_POST['name'])     ? trim($_POST['name'])     : '';
    $fac['is_show']      = !empty($_POST['is_show'])      ? intval($_POST['is_show'])    : 0;

	if(isset($_FILES['image']) && $_FILES['image']['tmp_name'] != '')
	{
		$img = $image->upload_image($_FILES['image']);
		if($img === false)
		{
			sys_msg($image->error_msg(), 1, array(), false);
		}
	}

	if(@$img != '')
	{
		$fac['image']=$img;
	}

    /* 入库的操作 */
    if ($db->autoExecute($ecs->table('facility'), $fac) !== false)
    {
        $cat_id = $db->insert_id();

        admin_log($_POST['name'], 'add', 'facility');   // 记录管理员操作
        clear_cache_files();    // 清除缓存

        /*添加链接*/
        $link[0]['text'] = '繼續新增';
        $link[0]['href'] = 'facility.php?act=add';

        $link[1]['text'] = '設施清單';
        $link[1]['href'] = 'facility.php?act=list';

        sys_msg('新增成功', 0, $link);
    }
 }

/*------------------------------------------------------ */
//-- 编辑商品分类信息
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
//    admin_priv('cat_manage');   // 权限检查
    $id = intval($_REQUEST['id']);
    $fac_info = get_fac_info($id);  // 查询分类信息数据

    /* 模板赋值 */
    $smarty->assign('ur_here',     '編輯設施');
    $smarty->assign('action_link', array('text' => '設施清單', 'href' => 'facility.php?act=list'));

    //分类是否存在首页推荐

    $smarty->assign('fac',    $fac_info);
    $smarty->assign('form_act',    'update');

    /* 显示页面 */
    assign_query_info();
    $smarty->display('facility_info.htm');
}

/*------------------------------------------------------ */
//-- 编辑商品分类信息
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'update')
{
    /* 权限检查 */
//    admin_priv('cat_manage');

    /* 初始化变量 */
    $id              = !empty($_POST['id'])       ? intval($_POST['id'])     : 0;
    $fac['sort_order']   = !empty($_POST['sort_order'])   ? intval($_POST['sort_order']) : 0;
    $fac['name']     = !empty($_POST['name'])     ? trim($_POST['name'])     : '';
    $fac['is_show']      = !empty($_POST['is_show'])      ? intval($_POST['is_show'])    : 0;

	if(isset($_FILES['image']) && $_FILES['image']['tmp_name'] != '')
	{
		$img = $image->upload_image($_FILES['image']);
		if($img === false)
		{
			sys_msg($image->error_msg(), 1, array(), false);
		}
	}

	if(@$img != '')
	{
		$fac['image']=$img;
	}


    if ($db->autoExecute($ecs->table('facility'), $fac, 'UPDATE', "id='$id'"))
    {
        /* 更新分类信息成功 */
        clear_cache_files(); // 清除缓存
        admin_log($_POST['name'], 'edit', 'facility'); // 记录管理员操作

        /* 提示信息 */
        $link[] = array('text' => '設施清單', 'href' => 'facility.php?act=list');
        sys_msg('修改成功', 0, $link);
    }
}


/*------------------------------------------------------ */
//-- 编辑排序序号
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'edit_sort_order')
{
//    check_authz_json('cat_manage');

    $id = intval($_POST['id']);
    $val = intval($_POST['val']);

    if (fac_update($id, array('sort_order' => $val)))
    {
        clear_cache_files(); // 清除缓存
        make_json_result($val);
    }
    else
    {
        make_json_error($db->error());
    }
}

/*------------------------------------------------------ */
//-- 切换是否显示
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'toggle_is_show')
{
//    check_authz_json('cat_manage');

    $id = intval($_POST['id']);
    $val = intval($_POST['val']);

    if (fac_update($id, array('is_show' => $val)) != false)
    {
        clear_cache_files();
        make_json_result($val);
    }
    else
    {
        make_json_error($db->error());
    }
}

/*------------------------------------------------------ */
//-- 删除商品分类
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'remove')
{
//    check_authz_json('cat_manage');

    /* 初始化分类ID并取得分类名称 */
    $id   = intval($_GET['id']);
    $cat_name = $db->getOne('SELECT name FROM ' .$ecs->table('facility'). " WHERE id='$id'");


    /* 如果不存在下级子分类和商品，则删除之 */
        /* 删除分类 */
    $sql = 'DELETE FROM ' .$ecs->table('facility'). " WHERE id = '$id'";
        if ($db->query($sql))
        {
            clear_cache_files();
            admin_log($name, 'remove', 'facility');
        }

    $url = 'facility.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    ecs_header("Location: $url\n");
    exit;
}

function get_fac_info($id)
{
    $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('facility'). " WHERE id='$id' LIMIT 1";
    return $GLOBALS['db']->getRow($sql);
}

function fac_list()
{
    $result = get_filter();
    
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keywords'] = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
        {
            $filter['keywords'] = json_str_iconv($filter['keywords']);
        }
        $filter['sort_by']    = empty($_REQUEST['sort_by'])    ? 'id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC'     : trim($_REQUEST['sort_order']);

        $ex_where = ' WHERE 1 ';

        if ($filter['keywords'])
        {
        	/* keyword判斷行動電話或帳號 */
            $ex_where .= " AND (user_name LIKE '%" . mysql_like_quote($filter['keywords']) . "%' or name LIKE '%" . mysql_like_quote($filter['keywords']) . "%' or tel like  '%" . mysql_like_quote($filter['keywords']) . "%' )";
        }

		
//		$filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('facility') . $ex_where);

        /* 分页大小 */
//        $filter = page_and_size($filter);

        $sql = "SELECT * ".
                " FROM " . $GLOBALS['ecs']->table('facility') .
				$ex_where .
                " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] ;
//                " LIMIT " . $filter['start'] . ',' . $filter['page_size'];
//互亿无线代码

        $filter['keywords'] = stripslashes($filter['keywords']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $fac_list = $GLOBALS['db']->getAll($sql);
//    $count = count($fac_list);
//    for ($i=0; $i<$count; $i++)
//    {
//	}

    $arr = array('fac_list' => $fac_list);

    return $arr;
}

function fac_update($id, $args)
{
    if (empty($args) || empty($id))
    {
        return false;
    }

    return $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('facility'), $args, 'update', "id='$id'");
}

?>