<?php


namespace wstmart\app\controller;


class Note extends Base
{
    public function index(){
        $userId = (int)session('WST_USER.userId');
        $model = model("note")->field(true)
            ->where(["user_id"=>$userId])->order("update_time", "desc")
            ->select();
        return WSTReturn("OK", 1, $model);
    }

    public function detail(){
        $userId = (int)session('WST_USER.userId');
        if(($id = (int)input( 'id', 0)) > 0){
            $detail = model("note")->field(true)
                ->where(["user_id"=>$userId, "id"=>$id])->find();
            if(!$detail) return WSTReturn("该条内容已被删除",0);
            return WSTReturn("OK", 1, $detail);
        }
        return WSTReturn("异常请求",0);
    }

    public function save(){
        $userId = (int)session('WST_USER.userId');
        $id = (int)input( 'post.id', 0);
        $title = input("post.title");
        $content = input("post.content");
        if(empty($title)) return WSTReturn("请填写标题",0);
        if(empty($content)) return WSTReturn("请填写内容",0);
        if($id > 0){
            $detail = model("note")->field(true)
                ->where(["user_id"=>$userId, "id"=>$id])->select();
            if(!$detail) return WSTReturn("该条内容已被删除",0);
            model("note")->where(["user_id"=>$userId, "id"=>$id])
                ->save([
                    "title"=>$title,
                    "content"=>$content,
                ]);
            return WSTReturn("成功", 1);
        }elseif($id == 0){
            model("note")->save([
                "title"=>$title,
                "content"=>$content,
                "user_id"=>$userId,
                "id"=>$id
            ]);
            return WSTReturn("成功", 1);
        }
        return WSTReturn("异常请求",0);
    }

    public function creditIndex(){
        $userId = (int)session('WST_USER.userId');
        $model = model("note_credit")->field(true)
            ->where(["user_id"=>$userId])->order("update_time", "desc")
            ->select();
        return WSTReturn("OK", 1, $model);
    }

    public function creditDetail(){

    }

    public function creditCreate(){

    }

    public function creditAdd(){

    }
}