<?php

namespace app\api\controller;

use think\Request;
use think\Controller;
use app\api\model\Folder;
use app\api\model\Doclist;
use think\Cache;
class Index extends Controller
{

    public function _initialize()
    {
        $token = input('post.token');
        $old_token =Cache::get('token');
        if($old_token){
            if($token!=$old_token){
                $rs = json_encode(code([], 10003, 'token不合法'));
                exit($rs);

            }
        }else{
            $rs = json_encode(code([], 10003, 'token不存在或已过期'));
            exit($rs);
        }


    }


    public function folderList()
    {
        $request = Request::instance();
        $model = new Folder();
        $parentid = $request->param('parentid');
        $res = $model->folderList($parentid);
        return json(code([$res], 0, 'ok'));


    }

    public function folderAdd()
    {
        $name = input('post.name');
//        var_dump($name);
        $parentid = input('post.parentid');
        $model = new Folder();

        if (empty($name)) {
            return json(code([], 10001, '名称不能为空'));
        } else {
            $res = $model->addFolder($name, $parentid);

            if ($res['code']>=0) {

                return json(code([$res], 0, '添加成功'));
            } else {
                return json(code([], 10002, '添加失败'));
            }
        }
    }

    public function folderEdit()
    {
        $name = input('post.name');
        $parentid = input('post.parentid');
        $folderid = input('post.folderid');

        $model = new Folder();
        if (empty($folderid)) {
            return json(code([], 10001, '参数不足'));
        }
        if (empty($name)) {
            return json(code([], 10001, '名称不能为空'));
        } else {
            $res = $model->editFolder($name, $parentid, $folderid);

            if ($res) {

                return json(code([], 0, '修改成功'));
            } else {
                return json(code([], 10002, '修改失败'));
            }
        }
    }

    public function folderDel()
    {

        $folderid = input('post.folderid');
        $model = new Folder();

        if (empty($folderid)) {
            return json(code([], 10001, '没有folderid'));
        } else {
            $res = $model->delFolder($folderid);

            if ($res) {

                return json(code([], 0, '删除成功'));
            } else {
                return json(code([], 10002, '删除失败'));
            }
        }
    }

    public function articleList()
    {
        $folderid = input('post.folderid');

        $model = new Doclist();
        $folderid = input('post.folderid');
        $post['origin'] = input('request.origin');
        $post['pagenum'] = input('request.pagenum');
        $post['current'] = input('request.current');
        if (!isset($folderid) || empty($folderid)) {
            return json(code([], 10001, 'folderid为空'));
        }
        if (!isset($post['origin']) || empty($post['origin'])) {
            $post['origin'] = 0;
        }

        //每页显示多少条数据
        if (!isset($post['pagenum']) || empty($post['pagenum'])) {
            $post['pagenum'] = 20;
        }

        //当前页
        if (isset($post['current']) && !empty($post['current']) && $post['current'] >= 1) {
            $data['current'] = ceil($post['current']);
            if (($data['current'] - 1) >= 1) {
                $post['origin'] = ($post['current'] - 1) * $post['pagenum'];
            }
        } else {

            $data['current'] = $post['origin'] / $post['pagenum'];
        }
        $data['pagenum'] = $post['pagenum'];
        if ($data['current'] <= 1) {
            $data['current'] = 1;
        }

        $result = $model->articleList($folderid, $post['origin'], $post['pagenum']);
        //var_dump($result);
        if ($result) {
            return json(code([$result], 0, '请求成功'));
        } else {
            return json(code([$result], 0, 'ok'));
        }

    }

    public function articleDetail()
    {
        $docid = input('post.docid');
        $model = new Doclist();
        $detail = $model->articleDetail($docid);
        if ($detail) {
            return json(code([$detail], 0, '请求成功'));
        } else {
            return json(code([], 10002, '请求失败'));
        }
    }

    public function articleAdd()
    {
        $title = input('post.title');
        $folderid = input('post.folderid');
        $type = (int)input('post.type');
        $doc = input('post.doc');
        $model = new Doclist();
        if (empty($type)) {
            return json(code([], 10001, '类型不能为空'));
        }
        if (empty($title)) {
            return json(code([], 10001, '标题不能为空'));
        } else {
            $res = $model->addArticle($title, $folderid, $type, $doc);

            if ($res) {

                return json(code([$res], 0, '添加成功'));
            } else {
                return json(code([$res], 10002, '添加失败'));
            }
        }
    }

    public function articleEdit()
    {
        $title = input('post.title');
        $folderid = input('post.folderid');
        $doc = input('post.doc');
        $docid = input('post.docid');
        $model = new Doclist();
        if (empty($folderid) || empty($title) || empty($doc) || empty($docid)) {
            return json(code([], 10001, '参数不足'));
        }
        $res = $model->editArticle($title, $folderid, $doc, $docid);
        if ($res) {
            return json(code([], 0, '修改成功'));
        } else {
            return json(code([], 10002, '修改失败'));
        }
    }

    public function articleDel()
    {

        $docid = input('post.docid');
        $model = new Doclist();

        if (empty($docid)) {
            return json(code([], 10001, '没有docid'));
        } else {
            $res = $model->delArticle($docid);

            if ($res) {

                return json(code([], 0, '删除成功'));
            } else {
                return json(code([], 10002, '删除失败'));
            }
        }
    }
}
