<?php
// +----------------------------------------------------------------------
// | 天方夜谭 [ 世界上最好用的QQ秒赞系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2019 http://www.eirds.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 天方夜谭 <17404785@qq.com>
// +----------------------------------------------------------------------

//页面标题
define('Title', '网站商品管理');

//新增商品
if (POST('action')=='add'){
    header('Content-Type:application/json; charset=utf-8');
    $name = POST('name');
    $image = POST('image');
    $content = POST('content');
    $stock = POST('stock');
    $mold = POST('mold');
    $money = POST('money');
    if (empty($name) || empty($image) || empty($content) || empty($stock) || empty($mold) || empty($money)){
        $arr = array('state'=>'no','msg'=>'所有项不能为空！');
    }else{
        if ($TFYT_DB->query("insert into ".PREFIX."website_goods (`name`,`image`,`content`,`stock`,`mold`,`money`,`addtime`)values('{$name}','{$image}','{$content}','{$stock}','{$mold}','{$money}',NOW())")){
            $arr = array('state'=>'ok','msg'=>'添加成功！');
        }else{
            $arr = array('state'=>'no','msg'=>'添加失败，请稍后再试！');
        }
    }
    exit(json_encode($arr));
}elseif (POST('action')=='info'){
    $id = POST('id');
    if (empty($id)){
        $arr = array('state'=>'no','msg'=>'缺少参数！');
    }else{
        if ($info=$TFYT_DB->get_row("select * from ".PREFIX."website_goods where id='{$id}'")){
            $arr = array('state'=>'ok','msg'=>'获取成功！','data'=>object_array($info));
        }else{
            $arr = array('state'=>'no','msg'=>'系统找不到商品的信息！');
        }
    }
    exit(json_encode($arr));
}elseif (POST('action')=='del'){
    $id = POST('id');
    if (empty($id)){
        $arr = array('state'=>'no','msg'=>'系统找不到要删除的商品！');
    }else{
        if ($TFYT_DB->query("delete from ".PREFIX."website_goods where id='{$id}'")){
            $arr = array('state'=>'ok','msg'=>'删除成功！');
        }else{
            $arr = array('state'=>'no','msg'=>'删除失败，请稍后再试！');
        }
    }
    exit(json_encode($arr));
}

//获取数据
$mold = GET('mold');
if (empty($mold)){
    $list = $TFYT_DB->get_results("select * from ".PREFIX."website_goods order by id asc");
}else{
    $list = $TFYT_DB->get_results("select * from ".PREFIX."website_goods where mold='".GET('mold')."' order by id asc");
}

//加载头部
Load_Template(Mains.'head.php');

//加载视图
Load_Template(Mains.'admin_goods.php');

//加载底部
Load_Template(Mains.'foot.php');