<?php

namespace App\Controller\admin\api;

use App\Controller\AbstractController;
use App\Model\AppNewsColumn;
use App\Model\AppNewsSecondColumn;
use App\Model\AppWebsiteConfig;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: 'admin/api/website')]
class WebsiteController extends AbstractController
{
    /**
     * @url /admin/api/website/config
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'config', methods: 'get')]
    public function config(): ResponseInterface
    {
        $query = AppWebsiteConfig::query()->where('is_delete', 0)->get()->toArray();
        return $this->response->json([
            'code' => 0,
            'data' => $query
        ]);
    }

    /**
     * @url /admin/api/website/config/insert
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'config/insert', methods: 'post')]
    public function configInsert(): ResponseInterface
    {
        $model = new AppWebsiteConfig();
        $model->app_company = $this->request->post('app_company');
        $model->app_description = $this->request->post('app_description');
        $model->app_name = $this->request->post('app_name');
        $model->app_domain = $this->request->post('app_domain');
        $model->app_filing = $this->request->post('app_filing');
        $model->app_filing_url = $this->request->post('app_filing_url');
        $model->app_logo = $this->request->post('app_logo');
        $model->app_extra = $this->request->post('app_extra');
        $model->app_template = $this->request->post('template');
        $model->app_extra_tag = $this->request->post('app_extra_tag');
        $model->app_baidu_zhanzhang = $this->request->post('app_baidu_zhanzhang');
        $model->save();

        return $this->response->json([]);
    }

    /**
     * @url /admin/api/website/config/delete
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'config/delete', methods: 'post')]
    public function configDelete(): ResponseInterface
    {
        $id = $this->request->post('id');

        $query = AppWebsiteConfig::find($id);
        if (!$query) {
            return $this->response->json(['code' => 400, 'msg' => 'id 有误']);
        }

        $query->is_delete = 1;
        $query->save();
        return $this->response->json([
            'code' => 0,
            'data' => $query
        ]);
    }

    /**
     * @url /admin/api/website/config/view
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'config/view', methods: 'get')]
    public function configView(): ResponseInterface
    {
        $id = $this->request->input('id');

        $query = AppWebsiteConfig::query()->where(['id' => $id])->first()->toArray();
        if (!$query) {
            return $this->response->json(['code' => 400, 'msg' => 'id 有误']);
        }
        $query['template'] = $query['app_template'];
        return $this->response->json(['code' => 0, 'msg' => 'ok', 'data' => $query]);
    }

    /**
     * @url /admin/api/website/config/save
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'config/save', methods: 'post')]
    public function configSave(): ResponseInterface
    {
        $id = $this->request->post('id');

        $query = AppWebsiteConfig::find($id);
        if (!$query) {
            return $this->response->json(['code' => 400, 'msg' => 'id 有误']);
        }

        $query->app_company = $this->request->post('app_company');
        $query->app_description = $this->request->post('app_description');
        $query->app_name = $this->request->post('app_name');
        $query->app_domain = $this->request->post('app_domain');
        $query->app_keywords = $this->request->post('app_keywords');
        $query->app_filing = $this->request->post('app_filing');
        $query->app_filing_url = $this->request->post('app_filing_url');
        $query->app_logo = $this->request->post('app_logo');
        $query->app_extra = $this->request->post('app_extra');
        $query->app_template = $this->request->post('template');
        $query->app_extra_tag = $this->request->post('app_extra_tag');
        $query->app_baidu_zhanzhang = $this->request->post('app_baidu_zhanzhang');

        $query->save();

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


    /**
     * 一级栏目列表
     * @url /admin/api/website/first-column
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'first-column', methods: 'get')]
    public function firstColumn(): ResponseInterface
    {
        $query = AppNewsColumn::query()->where('is_delete', 0)->get()->toArray();

        foreach ($query as &$item) {
            $item['website'] = AppWebsiteConfig::find($item['website'])->app_name;
        }

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

    /**
     * 新增一级栏目
     * @url /admin/api/website/first-column/insert
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'first-column/insert', methods: 'post')]
    public function firstColumnInsert(): ResponseInterface
    {
        $model = new AppNewsColumn();
        $model->name = $this->request->post('name');
        $model->website = $this->request->post('website');
        $model->url = $this->request->post('url');
        $model->desc = $this->request->post('desc');
        $model->save();

        return $this->response->json([]);
    }

    /**
     * @url /admin/api/website/first-column/view
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'first-column/view', methods: 'get')]
    public function firstColumnView(): ResponseInterface
    {
        $id = $this->request->input('id');

        $query = AppNewsColumn::query()->where(['id' => $id])->first()->toArray();
        if (!$query) {
            return $this->response->json(['code' => 400, 'msg' => 'id 有误']);
        }

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

    /**
     * @url /admin/api/website/first-column/save
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'first-column/save', methods: 'post')]
    public function firstColumnSave(): ResponseInterface
    {
        $id = $this->request->post('id');

        $query = AppNewsColumn::find($id);
        if (!$query) {
            return $this->response->json(['code' => 400, 'msg' => 'id 有误']);
        }

        $query->website = $this->request->post('website');
        $query->name = $this->request->post('name');
        $query->url = $this->request->post('url');
        $query->desc = $this->request->post('desc');
        $query->save();

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

    /**
     * 二级栏目列表
     * @url /admin/api/website/second-column
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'second-column', methods: 'get')]
    public function secondColumn(): ResponseInterface
    {
        $query = AppNewsSecondColumn::query()->where('is_delete', 0);
        $pagination = $query->paginate($this->request->input('limit', 10), page: $this->request->input('page'));
        $data = $query->get()->toArray();
        foreach ($data as &$item) {
            $query = AppWebsiteConfig::find($item['website']);
            $item['website'] = $query->app_name;
            $item['rid'] = AppNewsColumn::find($item['rid'])->name;
        }

        return $this->response->json([
            'code' => 0,
            'data' => $data,
            'count' => $pagination->total()
        ]);
    }

    /**
     * 新增二级栏目
     * @url /admin/api/website/second-column/insert
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'second-column/insert', methods: 'post')]
    public function secondColumnInsert(): ResponseInterface
    {
        $model = new AppNewsSecondColumn();
        $model->name = $this->request->post('name');
        $model->website = $this->request->post('website');
        $model->url = $this->request->post('url');
        $model->desc = $this->request->post('desc');
        $model->rid = $this->request->post('rid');
        $model->save();

        return $this->response->json([]);
    }

    /**
     * @url /admin/api/website/second-column/view
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'second-column/view', methods: 'get')]
    public function secondColumnView(): ResponseInterface
    {
        $id = $this->request->input('id');

        $query = AppNewsSecondColumn::query()->where(['id' => $id])->first()->toArray();
        if (!$query) {
            return $this->response->json(['code' => 400, 'msg' => 'id 有误']);
        }

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

    /**
     * @url /admin/api/website/second-column/save
     * @return ResponseInterface
     */
    #[RequestMapping(path: 'second-column/save', methods: 'post')]
    public function secondColumnSave(): ResponseInterface
    {
        $id = $this->request->post('id');

        $query = AppNewsSecondColumn::find($id);
        if (!$query) {
            return $this->response->json(['code' => 400, 'msg' => 'id 有误']);
        }

        $query->website = $this->request->post('website');
        $query->name = $this->request->post('name');
        $query->url = $this->request->post('url');
        $query->desc = $this->request->post('desc');
        $query->rid = $this->request->post('rid');
        $query->save();

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