<?php

namespace app\admin\controller;

use app\admin\service\ArticleCateService;
use support\spark\model\system\ArticleCate;
use support\Request;
use support\spark\exception\ApiException;
use support\spark\model\system\Article;
use support\View;
/*
 * @Title: 公告分类
 * @Description: 公告分类的增删改查
 */
class ArticleCateController extends Curd
{
    /**
     * 获取列表
     */
    public function index()
    {
        if (request()->isAjax()) {
            $articleCateService = new ArticleCateService();
            $res = $articleCateService->getList(request()->all());
            return sparkSuccess($res);
        }
        return view('article_cate/index');
    }

    /**
     * 添加
     */
    public function add()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $articleCateService = new ArticleCateService();
            $res = $articleCateService->addArticleCate($param);
            return sparkSuccess($res);
        }

        return view('article_cate/add');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $articleCateService = new ArticleCateService();
            $res = $articleCateService->editArticle($param);
            return sparkSuccess($res);
        }
        $id =  request()->input('id');
        $articleCateModel = new ArticleCate();
        View::assign([
            'info' => $articleCateModel->getInfoById($id)
        ]);

        return view('article_cate/edit');
    }

    /**
     * 删除
     */
    public function del()
    {
        $id = request()->input('id');
        $articleModel = new Article();
        $has=$articleModel->getInfoByWhere(['cate_id'=>$id]);
        if (!empty($has)) {
            throw  new  ApiException('该分类下有文章不可删除', -1);
        }
        $articleCateModel = new ArticleCate();
         $articleCateModel->delById($id);
        return sparkSuccess(dataReturn(0, '删除成功'));
    }
}