<?php

namespace app\controller;

use app\model\{Article as MArticle, Category, DownloadModel, Model, NoticeModel};
use page\DxtcPageA;
use think\Paginator;

class Info extends Base
{
    public function index()
    {
        $keyword = input('keyword/s', '');
        $categoryId = input("category_id", 0);
        if (!empty($categoryId)) {
            $category = Category::where('id', $categoryId)->find();
            $parentCategory = Category::where('id', $category['parent_id'])->find();
        } else {
            $parentCategory = Category::where('id', Category::CATEGORY_INFO)->find();
            $category = Category::where('parent_id', $parentCategory['id'])->order('sort asc')->find();
        }

        if (!empty($category['template_list'])) {
            return $this->redirect($category['template_list']);
        }

        $categoryId = $category['id'];

        $categoryChildren = Category::where('parent_id', $parentCategory['id'])->order('sort asc')->field('id,title')->select();

        $description = $category['seo_description'] ?: $this->system['seo_description'];
        $keywords = $category['seo_keywords'] ?: $this->system['seo_keywords'];
        $title = $category['seo_title'] ?: $category['title'] . ' | ' . $this->system['seo_title'];
        $this->setSeo($title, $keywords, $description);
        $listSort = ['a.top' => 'desc', 'a.sort' => 'desc'];

        // 自定义分页驱动
        app('think\App')->bind(Paginator::class, DxtcPageA::class);

        $items = MArticle::getList($categoryId, 6, $keyword, [], 1, $listSort, false);

        $this->data['items'] = MArticle::parseList($items);
        $this->data['category'] = $category;
        $this->data['parentCategory'] = $parentCategory;
        $this->data['categoryChildren'] = $categoryChildren;
        $this->data['categoryId'] = $categoryId;
        $this->data['bodyClass'] = 'main';

        return $this->view($category['template_list'] ?? '');
    }

    // 人才招揽
    public function job()
    {
        $keyword = input('keyword/s', '');
        $categoryId = input("category_id", 0);
        if (!empty($categoryId)) {
            $category = Category::where('id', $categoryId)->find();
            $parentCategory = Category::where('id', $category['parent_id'])->find();
        } else {
            $parentCategory = Category::where('id', Category::CATEGORY_INFO)->find();
            $category = Category::where('parent_id', $parentCategory['id'])->where('template_list', 'info/job')->order('sort asc')->find();
        }

        if (!empty($category['template_list'])) {
            $this->redirect($category['template_list']);
        }

        $categoryId = $category['id'];

        $categoryChildren = Category::where('parent_id', $parentCategory['id'])->order('sort asc')->field('id,title')->select();

        $description = $category['seo_description'] ?: $this->system['seo_description'];
        $keywords = $category['seo_keywords'] ?: $this->system['seo_keywords'];
        $title = $category['seo_title'] ?: $category['title'] . ' | ' . $this->system['seo_title'];
        $this->setSeo($title, $keywords, $description);
        $listSort = ['a.sort' => 'desc'];

        // 自定义分页驱动
        app('think\App')->bind(Paginator::class, DxtcPageA::class);

        $items = NoticeModel::getList($categoryId, 6, $keyword, [], 1, $listSort, false);

        $this->data['items'] = $items;
        $this->data['category'] = $category;
        $this->data['parentCategory'] = $parentCategory;
        $this->data['categoryChildren'] = $categoryChildren;
        $this->data['categoryId'] = $categoryId;

        return $this->view($category['template_list'] ?? '');
    }

    // 招采平台
    public function platform()
    {
        $keyword = input('keyword/s', '');
        $categoryId = input("category_id", 0);
        if (!empty($categoryId)) {
            $category = Category::where('id', $categoryId)->find();
            $parentCategory = Category::where('id', $category['parent_id'])->find();
        } else {
            $parentCategory = Category::where('id', Category::CATEGORY_INFO)->find();
            $category = Category::where('parent_id', $parentCategory['id'])->where('template_list', 'info/platform')->order('sort asc')->find();
        }

        $categoryId = $category['id'];

        $categoryChildren = Category::where('parent_id', $parentCategory['id'])->order('sort asc')->field('id,title')->select();

        $description = $category['seo_description'] ?: $this->system['seo_description'];
        $keywords = $category['seo_keywords'] ?: $this->system['seo_keywords'];
        $title = $category['seo_title'] ?: $category['title'] . ' | ' . $this->system['seo_title'];
        $this->setSeo($title, $keywords, $description);

        $cateList = Category::where('parent_id', $category['id'])
            ->where('model_id', 39)//公式公告
            ->order('sort asc')->column('title,id');

        // 自定义分页驱动
        app('think\App')->bind(Paginator::class, DxtcPageA::class);

        $list = [];
        foreach ($cateList as &$cate) {
            $cate['total'] = NoticeModel::where('category_id', $cate['id'])->when(!empty($keyword), function ($q) use ($keyword) {
                $q->where('title', 'like', '%'.$keyword.'%');
            })->count();
        }

        $downloadList = DownloadModel::where('category_id', Category::CATEGORY_INFO_DOWNLOAD)
            ->when(!empty($keyword), function ($q) use ($keyword) {
                $q->where('title', 'like', '%'.$keyword.'%');
            })
            ->order('sort desc')->select();

        $this->data['list'] = $list;
        $this->data['cateList'] = $cateList;
        $this->data['downloadList'] = $downloadList;
        $this->data['category'] = $category;
        $this->data['parentCategory'] = $parentCategory;
        $this->data['categoryChildren'] = $categoryChildren;
        $this->data['categoryId'] = $categoryId;

        return $this->view();
    }

    //详情
    public function detail($id = 0)
    {
        if ($id <= 0) {
            return $this->error('错误页面');
        }
        $article = MArticle::getById($id);
        if (empty($article)) {
            return $this->error('无此文章');
        }

        $category = Category::where('id', $article['category_id'])->find();
        $parentCategory = Category::where('id', $category['parent_id'])->find();

        $categoryChildren = Category::where('parent_id', $parentCategory['id'])->order('sort asc')->field('id,title')->select();

        $description = $article['seo_description'] ?: $this->system['seo_description'];
        $keywords = $article['seo_keywords'] ?: $this->system['seo_keywords'];
        $title = $article['seo_title'] ?: $article['title'] . ' | ' . $this->system['seo_title'];

        $this->setSeo($title, $keywords, $description);
        $this->data['item'] = MArticle::parseInfo($article);
        $this->data['category'] = $category;
        $this->data['parentCategory'] = $parentCategory;
        $this->data['categoryChildren'] = $categoryChildren;
        $this->data['categoryId'] = $category['id'];
        return $this->view();
    }

    public function getNotice()
    {
        $page = input('page/d', 1);
        $size = input('size/d', 2);
        $cate = input('cate/d', 0);
        $keyword = input('keyword/s', '');

        $page = $page ?: 1;
        $size = $size ?: 2;

        $res = [
            'total' => 0,
            'page' => $page,
            'size' => $size,
            'data' => [],
        ];

        if (empty($cate)) {
            return $this->json(0,'success', $res);
        }

        $where = [];
        if (!empty($keyword)) {
            $where[] = ['title', 'like', '%'.$keyword.'%'];
        }

        $where[] = ['category_id', '=', $cate];
        $q = NoticeModel::where($where);
        $total = $q->count();

        if ($total > 0) {
            $list = $q->page($page)->limit($size)->order('sort desc')->select();
            $list->each(function ($item) {
                $item->create_at = date('Y.m.d', $item->create_time);
            });
            $res['total'] = $total;
            $res['page'] = $page;
            $res['size'] = $size;
            $res['data'] = $list;
        }

        return $this->json(0, 'success', $res);
    }
}