<?php
namespace app\controller\manager;

use app\model\ArticleTags;
use think\Exception;
use think\exception\ValidateException;

/**
 * 标签管理
 *
 * Class Tag
 * @package app\controller\manager
 */
class Tag extends Base
{
    public function index()
    {
        $whereMap       = [];
        $paginateRows   = [
            'list_rows' => 30,
            'query'     => [],
        ];

        $this->data['items'] = ArticleTags::getListWithPaginate($whereMap, $paginateRows, false);
        return $this->view();
    }


    public function edit()
    {
        $id         = $this->request->param('id/d', 0);
        $tagInfo    = ArticleTags::findInfoById($id);
        if (empty($tagInfo)) {
            return $this->json(4004, '没有相关的记录');
        }

        if ($this->request->isPost()) {
            $itemData = $this->request->param('item/a', []);
            if (isset($itemData['id'])) {
                unset($itemData['id']);
            }

            try {
                $this->validate($itemData, [
                    'seo_title|SEO标题' => 'max:100',
                    'seo_keyword|SEO关键词' => 'max:200',
                    'seo_description|SEO描述' => 'max:200',
                ]);
                $tagInfo->save($itemData);
            } catch (ValidateException $e) {
                return $this->json(4001, $e->getError());
            } catch (Exception $e) {
                return $this->json(5001, '系统繁忙，修改失败！');
            }

            return $this->json(0, '修改成功！');
        }

        $this->data['id']   = $id;
        $this->data['item'] = $tagInfo;
        return $this->view();
    }

}