<?php

namespace App\Controller\admin\api;

use App\Controller\AbstractController;
use App\Enums\ArticlePublishedStatusEnum;
use App\FormModel\admin\articles\ModifyModel;
use App\FormModel\admin\news\NewsFormModel;
use App\Helpers\AppHelper;
use App\Model\AppArticle;
use App\Model\AppBrand;
use App\Model\AppNews;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Controller(prefix: 'admin/api/news')]
class NewsController extends AbstractController
{
    /**
     * 文章列表
     * @url /admin/api/news
     * @return array
     */
    #[RequestMapping(path:'', methods: 'get')]
    public function index(): array
    {
        $createdFilter = $this->request->query('created_at', [date('Y-m-d', strtotime('-1 month')), date('Y-m-d', time())]);
        $title = $this->request->input('title', '');
        foreach ($createdFilter as $index => &$item) {
            if ($index == 0) {
                $item = strtotime($item . ' 00:00:00');
            }

            if ($index == 1) {
                $item = strtotime($item . ' 23:59:59');
            }
        }

        $ids = [];
        $query = AppNews::query()
            ->where('is_delete', 0)
            ->select(['id'])
            ->when($title, fn($q) => $q->where('title', 'like', "%{$title}%"))
            ->whereBetween('created_at', $createdFilter)
            ->orderBy('id', 'desc');
        $pagination = $query->paginate($this->request->input('limit', 10), page: $this->request->input('page'));
        foreach ($pagination->items() as $item) {
            $ids[] = $item->id;
        }

        $value = AppNews::query()->whereIn('id', $ids)->orderBy('id', 'desc')->get()->toArray();
        // $value = AppNews::find($ids, ['title', 'is_record'])->toArray();
        return ['code' => 0, 'msg' => 'ok', 'count' => $pagination->total(), 'data' => $value];
    }

    /**
     * 查看文章详情信息
     * @url /admin/api/news/view
     */
    #[RequestMapping(path:'view', methods: 'get')]
    public function view(): \Psr\Http\Message\ResponseInterface
    {
        $query = AppNews::query()
            ->where('id', $this->request->query('id'));

        return $this->response->json(['code' => 0, 'msg' => 'ok', 'data' => $query->first()]);
    }

    /**
     * 更新文章内容
     * @url /admin/api/news/update
     */
    #[RequestMapping(path:'update', methods: 'post')]
    public function update()
    {
        $model = new NewsFormModel();
        $model->setAttributes($this->request->post());
        $model->update();
        return $this->response->json(['code' => 0, 'msg' => 'ok']);
    }

    /**
     * 新增新新闻接口
     * @url /admin/api/news/insert
     */
    #[RequestMapping(path:'insert', methods: 'post')]
    public function insert()
    {
        $model = new NewsFormModel();
        $model->setAttributes($this->request->post(), ['title', 'keywords', 'description', 'cover', 'content']);
        $model->insert();
        return $this->response->json(['code' => 0, 'msg' => 'ok']);
    }

    /**
     * 删除新新闻接口
     * @url /admin/api/news/delete
     */
    #[RequestMapping(path:'delete', methods: 'post')]
    public function delete(): \Psr\Http\Message\ResponseInterface
    {
        $id = $this->request->post('id');
        $query = AppNews::find($id);
        $query->is_delete = 1;
        $query->deleted_at = time();
        $query->save();
        return $this->response->json(['code' => 0, 'msg' => 'ok']);
    }
}