<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\index\model\AttachmentCategoryModel;
use app\index\model\AttachmentModel;
use app\index\model\VideoCategoryModel;
use app\index\model\VideoModel;
use think\facade\View;

/**
 * 附件控制器
 */
class Attachment extends Common
{
    /**
     * 分页列表
     */
    public function index()
    {
        $param = request()->param([
            'id',
            'keyword' => '',
            'page/d'  => 1,
            'size/d'  => 12,
        ]);

        // 获取附件分类
        $categorys = AttachmentCategoryModel::field([
                'id',
                'name'
            ])
            ->language($this->lang_id)
            ->parent(0)
            ->isShow(true)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->select();
        View::assign('categorys', $categorys);

        // 获取附件
        $attachements = AttachmentModel::field([
                'id',
                'name',
                'desc',
                'image',
                'applicable_to',
                'support_platform',
                'attach',
            ])
            ->withSearch(['name'], ['name' => !empty($param['keyword']) ? trim($param['keyword']) : null])
            ->language($this->lang_id)
            ->category(!empty($param['id']) ? $param['id'] : $categorys[0]['id']??null)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->paginate([
                'list_rows' => $param['size'],
                'page'      => $param['page'],
                'query'     => [
                    'id' => $param['id']??null
                ]
            ]);
        if (!$attachements->isEmpty()) {
            $attachements->each(function($item) {
                if (is_array($item->attach)) {
                    $item->attach = array_map(function($v) {
                        $v['file_path'] = url_filesystem_detect($v['file_path']);
                        return $v;
                    }, $item->attach);
                }
            });
        }
        View::assign('attachements', $attachements);
        View::assign('page', $attachements->render());

        return View::fetch('index');
    }

    /**
     * 视频
     */
    public function video()
    {
        $param = request()->param([
            'id',
            'keyword',
            'page/d' => 1,
            'size/d' => 12
        ]);

        // 获取附件分类
        $attachment_categorys = AttachmentCategoryModel::field([
                'id',
                'name'
            ])
            ->language($this->lang_id)
            ->parent(0)
            ->isShow(true)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->select();
        View::assign('attachment_categorys', $attachment_categorys);

        // 获取视频分类
        $video_categorys = VideoCategoryModel::field([
                'id',
                'name'
            ])
            ->language($this->lang_id)
            ->isShow(true)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->select();
        View::assign('video_categorys', $video_categorys);

        // 获取视频
        $videos = VideoModel::field([
                'id',
                'name',
                'desc',
                'image',
                'video',
                'link'
            ])
            ->withSearch(['name'], ['name' => !empty($param['keyword']) ? trim($param['keyword']) : null])
            ->language($this->lang_id)
            ->category(!empty($param['id']) ? $param['id'] : $video_categorys[0]['id']??null)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->paginate([
                'list_rows' => $param['size'],
                'page'      => $param['page'],
                'query'     => [
                    'id' => $param['id']??null
                ]
            ]);
        
        if (!$videos->isEmpty()) {
            $videos->each(function($item) {
                $item->video = url_filesystem_detect($item->video);
            });
            $videos->setCollection($videos->getCollection()->chunk(2));
        }
        View::assign('videos', $videos);
        View::assign('page', $videos->render());

        return View::fetch('video');
    }
}
