<?php
namespace app\api\controller;

use app\model\Archives as ArchivesModel;
use support\Request;
use taoser\facade\Validate;
use hg\apidoc\annotation as Apidoc;

/**
 * 文章模块
 */
class ArticleController extends BaseController{
    public $noNeedLogin = ['*'];
    /**
     * 列表
     * @Apidoc\Query("category_id", type="int", require=true, desc="分类ID",default=10)
     * @Apidoc\Query("page", type="int", require=true, desc="页码",default=1)
     * @Apidoc\Query("limit", type="int", require=true, desc="分页大小",default=10)
     */
    public function list(){
        $limit = (int)input('limit',10);
        $category_id = (int)input('category_id',0);

        $model = ArchivesModel::where('status','1')->where('type','article');
        if($category_id){
            $model = $model->where('category_id',$category_id);
        }
        $list = $model->order('id','desc')->paginate($limit);
        $user_id=0;
        try {
            $user_id = \support\Jwt\JwtToken::getCurrentId();
        } catch (\Throwable $th) {
        }
        $list->each(function($item)use($user_id){
            if(!$user_id){
                $item->is_read = 0;
            }
            $item->is_read = cache('article_read_'.$item->id.'_'.$user_id)?:0;
            
            return $item;
        });
        return $this->success(__('successful'),$list->toArray());

    }
    /**
     * faq
     * @Apidoc\Query("page", type="int", require=true, desc="页码",default=1)
     * @Apidoc\Query("limit", type="int", require=true, desc="分页大小",default=10)
     */
    public function faq(){
        $limit = (int)input('limit',10);
        $model = ArchivesModel::alias('a')
            ->join('content c', 'a.id = c.id')
            ->where('a.status','1')
            ->where('a.type','article')
            ->where('a.category_id',9);
        $list = $model->Field('a.title,a.id,c.content')->order('a.id','desc')->paginate($limit);
        return $this->success(__('successful'),$list->toArray());

    }
    /**
     * 详情
     * @Apidoc\Query("id", type="int", require=true, desc="ID")
     */
    public function detail(){
        $appid = input('id');
        /** @var ArchivesModel $vo */
        $vo = ArchivesModel::where('id',$appid)->find();
        if($vo) {
            $addon = \app\model\Content::where('id', $vo->id)->find()->toArray();
            if ($addon) {
                $vo->setAddonData($addon);
            }
            $user_id=0;
            try {
                $user_id = \support\Jwt\JwtToken::getCurrentId();
            } catch (\Throwable $th) {
            }
            if($user_id){
                cache('article_read_'.$vo->id.'_'.$user_id,1);
            }
            return $this->success(__('successful'),$vo->toArray());
        }else{
            return $this->error(__("Article does not exist"));
        }
    } 
    /**
     * 单页详情
     * @Apidoc\Query("id", type="int", require=true, desc="ID")
     * @Apidoc\Query("name", type="string", require=true, desc="二选1")
     */
    public function singpage(){
        $appid = input('id');
        $name = input('name');
        /** @var ArchivesModel $vo */
        if($name){
            $vo = ArchivesModel::where('name',$name)->find();
        }else{
            if($appid){
                $vo = ArchivesModel::where('id',$appid)->find();
            }
        }
        if($vo) {
            $addon = \app\model\Content::where('id', $vo->id)->find()->toArray();
            if ($addon) {
                $vo->setAddonData($addon);
            }
            return $this->success(__('successful'),$vo->toArray());
        }else{
            return $this->error(__("Article does not exist"));
        }
    }
    /**
     * 幻灯片
     * @Apidoc\Query("id", type="int", require=true, desc="ID")
     */
    public function slide(){
        $list = [
            ['image'=>domain().'/storage/slide/1.jpg','title'=>''],
            ['image'=>domain().'/storage/slide/2.webp','title'=>''],
            ['image'=>domain().'/storage/slide/3.webp','title'=>''],
            ['image'=>domain().'/storage/slide/4.jpg','title'=>''],
        ];
        return $this->success(__('successful'),$list);
    }

    /**
     * 设为已读
     * @Apidoc\Query("id", type="int", require=true, desc="ID,多个逗号隔开")
     */
    function mask_as_read(){
        $ids = input('id');
        $user_id = \support\Jwt\JwtToken::getCurrentId();
        if(!$user_id){
            return $this->success(__('successful'));
        }
        $ids = explode(',',$ids);
        foreach ($ids as $id) {
            $key = 'article_read_'.$id.'_'.$user_id;
            cache($key,1);
        }
        return $this->success(__('successful'));
    }
}