<?php
if(!defined('IN_ADMIN')) exit('Access Denied');
if($_SESSION["admin"]!="NewsTeng")showmessage('请登陆', SCRIPT_NAME.'?file=login');
@set_time_limit(0);
require_once TM_ROOT.'include/product.class.php';
require_once TM_ROOT.'include/type.class.php';

$product = new product();
$type = new type();
if(!isset($action)) $action = 'list';
switch ($action){
	case 'add':
            if($dosubmit){
                if(empty($info)) showmessage('操作失败', 'goback');
            	if($_FILES){
            		require_once TM_ROOT.'include/upload.class.php';
        			$upload = new upload('uploadfile', 'jpg|jpeg|gif|bmp|png', 1);   
					$img = $upload->up();
        			$info['img'] = $img;    
        		} 
				$info['time'] = TIME;   
				if(!$product->add($info)) {
					showmessage('添加失败,请检查输入是否正确!','goback');
				}
				showmessage('添加成功！', 'goback');
            }else{   
            	$fl = $type->listinfo('where lx=2');
            	include_once TM_ROOT.'./include/editor.class.php';
				$editor=new editor('info[body]');
				$editor->BasePath='.';
				$editor->Height=500;
				$editor->Width=700;
				$editor->AutoSave=false; 
                include admin_tpl('product');
            }
	break;

    case 'list':
    	$page = max(intval($page), 1);
    	$page_size = 20;
    	$r = $db->get_one("SELECT count(*) as num FROM ".DB_PRE."product");
    	$mpurl =SCRIPT_NAME.'?file=product&action=list&';
    	$dispage =  page($r['num'],$page_size,$page,$mpurl);
      	$n = $product->listinfo('', 'id DESC', $page, $page_size);
        include admin_tpl('product');
	break;

    case 'edit':
        if($dosubmit){
        	if($_FILES){
        		require_once TM_ROOT.'include/upload.class.php';
        		$upload = new upload('uploadfile', 'jpg|jpeg|gif|bmp|png', 1);   
        		$info['img'] = $upload->up();
        	}   
        	if(!$product->updata(intval($id),$info)){
        		showmessage('请确认您的填写是否正确！', 'goback');
        	} 
        	showmessage('保存成功！', 'goback');
        }else{
        	$fl = $type->listinfo('where lx=2');
        	$ns = $product->get(intval($id)); 
            include_once TM_ROOT.'./include/editor.class.php';
			$editor=new editor('info[body]');
			$editor->Value=$ns['body'];
			$editor->BasePath='.';
			$editor->Height=500;
			$editor->Width=700;
			$editor->AutoSave=false; 
            include admin_tpl('product');
        }
	break;
	case 'del':
		$u = $product->del(intval($id));
	    if(!$u){
	    	showmessage('删除失败！', 'goback');
	    }
	    showmessage('删除成功！', 'goback');
	break;
}
?>