<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\ArticleCategoryModel;
use app\admin\validate\v1\ArticleCategoryValidate;

/**
 * 文章分类管理控制器
 */
class ArticleCategory
{
    // 分类列表
    public function list()
    {
        $param = request()->param([
            'name' => '',
            'is_show',
        ]);
        $categorys = ArticleCategoryModel::field([
                'id', 
                'pid',
                'name'
            ])
            ->language(request()->lang_id)
            ->withSearch(['name'], ['name' => $param['name']])
            ->where(function($query) use($param) {
                if (isset($param['is_show'])) {
                    $query->where('is_show', '=', $param['is_show']);
                }
            })
            ->order('sort', 'asc')
            ->select();

        return success('获取成功',array_to_tree($categorys->toArray(), 0, 'pid', 1, false));
    }

    // 分类分页列表
    public function index()
    {
        $param = request()->param([
            'name'
        ]);

        $category = ArticleCategoryModel::withoutField([
                'language_id', 
                'deleted_at'
            ])
            ->language(request()->lang_id)
            ->where(function($query) use($param) {
                if (isset($param['name'])) {
                    $query->withSearch(['name'], ['name' => $param['name']]);
                }
            })
            ->order('sort', 'asc')
            ->select();

        return success('获取成功', array_to_tree($category->toArray(), 0, 'pid', 1, false));
    }

    // 分类详情
    public function read()
    {
        $id = request()->param('id');
        $category = ArticleCategoryModel::withoutField(['language_id', 'deleted_at'])
            ->bypk($id)
            ->find();
        if (is_null($category)) {
            return error('文章分类不存在');
        }
        return success('获取成功', $category);
    }

    // 添加分类
    public function save()
    {
        $post = request()->post([
            'unique_label',
            'name',
            'icon',
            'pid',
            'sort'    => 0,
            'is_show' => 1
        ]);

        $data = array_merge($post, [
            'language_id'  => request()->lang_id,
            'unique_label' => $post['unique_label'] ?? uniqid('BANNER_')
        ]);
        $valiate = new ArticleCategoryValidate;
        if (!$valiate->scene('add')->check($data)) {
            return error($valiate->getError());
        }

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

    // 更新分类
    public function update()
    {
        $id  = request()->param('id');
        $put = request()->put([
            'name',
            'icon',
            'pid',
            'sort',
            'is_show'
        ]);
        $data = array_merge($put, ['language_id' => request()->lang_id]);
        
        $valiate = new ArticleCategoryValidate;
        if (!$valiate->scene('edit')->check(array_merge($data, ['id' => $id]))) {
            return error($valiate->getError());
        }

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

        return success('操作成功');
    }

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

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

        return success('操作成功');
    }

    // 删除分类
    public function delete()
    {
        $id = request()->param('id');
        $category = ArticleCategoryModel::bypk($id)->find();
        if (is_null($category)) {
            return error('请确认操作对象是否存在');
        }
        if (!$category->useSoftDelete('deleted_at', date('Y-m-d H:m:s', time()))->delete()) {
            return error('操作失败');
        }
        return success('操作成功');
    }
}
