<?php

namespace app\api\model;

use think\Model;
use think\Db;

class Doclist extends Model
{
    public function index()
    {
//        $user = Db::name('folder')->where('state', '=', -1)->find();
//        return $user;
//        $sql = Db::name('folder')->getLastSql();
////        var_dump($sql);
    }

    //获取文件夹名称
    public function articleList($folderid,$origin,$pagenum)
    {
//       var_dump($folderid);
       if($folderid){
           $where['folderid'] = array('=',$folderid);
           $where['state'] = array('=',0);
           $articlelist = Db::name('doclist')->field('docid,title,type,updatetime')->where($where)->order('createtime')->limit($origin,$pagenum)->select();
//           $sql = Db::name('doclist')->getLastSql();
//           var_dump($sql);


       }else{
           $articlelist=[];
       }
        return $articlelist;

    }

    public function articleDetail($docid)
    {

      if($docid){
          $where = ['docid' => $docid];
          $articldetail = Db::name('doclist')->field('docid,title,type,updatetime,doc')->where($where)->find();
          return $articldetail;
      }
    }
    public function addArticle($title, $folderid,$type,$doc)
    {

        $docid = randomkeys(18);
//        $where = ['folderid' => $folderid];

        $docname = Db::name('doclist')->where(['docid' => $docid])->count();

        $rs = new Doclist();
        if ($docname == 0) {
            $data = [
                'docid' =>$docid,
                'title' => $title ,
                'type' => $type,
                'doc' => $doc,
                'folderid' => $folderid,
                'createtime' =>  date("Y-m-d H:i:s", time())
            ];
            $rs->isUpdate(false)->save($data);
//                   $sql = Db::name('doclist')->getLastSql();
//                   var_dump($sql);
            $return = $rs->docid;
            return $return;

        }


    }
    public function editArticle($title, $folderid,$doc,$docid)
    {
        $rs = new Doclist();


        $where = [];
        $data = [
            'title' => $title,
            'doc' =>$doc,
            'updatetime' => date("Y-m-d H:i:s", time()),

        ];

        if ($docid) {
            $where['docid'] = array('=',$docid);
        }
            $rs = $this->where($where)->update($data);

            if ($rs === false) {
                return false;
            } else {
                return true;
            }



//        var_dump($rs);die();
//                $sql = Db::name('folder')->getLastSql();
//       var_dump($sql);

    }

    public function delArticle($docid)
    {
        $rs = new Doclist();
        $where = ['docid' => $docid];
        $data = [
            'state' => -1,
            'updatetime' => date("Y-m-d H:i:s", time())
        ];

        $rs = $this->where($where)->update($data);
        if ($rs === false) {
            return false;
        } else {
            return true;
        }


//        var_dump($rs);die();
//                $sql = Db::name('folder')->getLastSql();
//       var_dump($sql);

    }
}
