<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\VideoCategoryModel;
use app\admin\validate\v1\VideoCategoryValidate;

/**
 * 视频分类控制器
 */
class VideoCategory
{
    /**
     * 视频分类列表数据
     */
    public function list()
    {
        $param = request()->param([
            'name' => '',
            'is_show'
        ]);
        $categorys = VideoCategoryModel::field([
                'id',
                'name',
            ])
            ->withSearch(['name'], [
                'name' => $param['name']??null
            ])
            ->language(request()->lang_id)
            ->where(function($query) use($param) {
                if (isset($param['is_show'])) {
                    $query->where('is_show', '=', $param['is_show']);
                }
            })
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->select();

        return success('获取成功', $categorys);
    }

    /**
     * 视频分类分页数据
     */
    public function index()
    {
        $param = request()->param([
            'name'   => '',
            'page/d' => 1,
            'size/d' => 10,
        ]);

        $categorys = VideoCategoryModel::withoutField([
                'language_id',
                'created_at',
                'updated_at'
            ])
            ->withSearch(['name'], [
                'name' => $param['name']??null
            ])
            ->language(request()->lang_id)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->paginate([
                'list_rows' => $param['size'],
                'page'      => $param['page'],
            ]);

        return success('获取成功', $categorys);
    }

    /**
     * 视频分类详情
     */
    public function read()
    {
        $id = request()->param('id');

        $category = VideoCategoryModel::withoutField([
                'language_id',
                'created_at',
                'updated_at'
            ])
            ->bypk($id)
            ->find();
        if (empty($category)) {
            return error('分类不存在');
        }

        return success('获取成功', $category);
    }

    /**
     * 视频分类添加
     */
    public function save()
    {
        $post = request()->post([
            'name',
            'sort',
            'is_show'
        ]);
        $data = array_merge($post, ['language_id' => request()->lang_id]);

        $validate = new VideoCategoryValidate;
        if (!$validate->scene('create')->check($data)) {
            return error($validate->getError());
        }

        $category = new VideoCategoryModel;
        if (!$category->save($data)) {
            return error('操作失败');
        }
        return success('操作成功');
    }

    /**
     * 视频分类更新
     */
    public function update()
    {
        $id  = request()->param('id');
        $put = request()->put([
            'name',
            'sort',
            'is_show'
        ]);

        $validate = new VideoCategoryValidate;
        if (!$validate->scene('update')->check(array_merge($put, ['id' => $id]))) {
            return error($validate->getError());
        }

        $category = VideoCategoryModel::bypk($id)->find();
        if (empty($category)) {
            return error('请确认操作对象是否存在');
        }

        if (!$category->save($put)) {
            return error('操作失败');
        }
        return success('操作成功');
    }

    /**
     * 设置视频分类排序值
     */
    public function sort()
    {
        $id   = request()->param('id');
        $sort = request()->post('sort');

        $validate = new VideoCategoryValidate;
        if (!$validate->scene('sort')->check(['id' => $id,'sort' => $sort])) {
            return error($validate->getError());
        }

        $category = VideoCategoryModel::bypk($id)->find();
        if (!$category) {
            return error('请确认操作对象是否存在');
        }

        $category->sort = $sort;
        if (!$category->save()) {
            return error('操作失败');
        }
        return success('操作成功');
    }

    /**
     * 视频分类删除
     */
    public function delete()
    {
        $id = request()->param('id');

        $category = VideoCategoryModel::bypk($id)->find();
        if (!$category) {
            return error('请确认操作对象是否存在');
        }

        // 检查该分类下是否存在视频
        if ($category->video()->count()) {
            return error('该分类下存在视频，请先删除视频');
        }

        if (!$category->delete()) {
            return error('操作失败');
        }
        return success('操作成功');
    }
}
