<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\SysNavigationModel;
use app\admin\validate\v1\NavigationValidate;

class Navigation
{
    // 列表
    public function list()
    {
        $name = request()->param([
            'name',
        ]);

        $navs = SysNavigationModel::field([
                'id',
                'name'
            ])
            ->withSearch(['name'], ['name' => $name['name']??null])
            ->language(request()->lang_id)
            ->select();

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

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

        $navs = SysNavigationModel::field([
                'id',
                'name',
                'desc',
                'created_at'
            ])
            ->withSearch(['name'], ['name' => $name['name']??null])
            ->language(request()->lang_id)
            ->paginate([
                'list_rows' => $name['size'],
                'page' => $name['page'],
            ]);

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

    // 详情
    public function read()
    {
        $id = request()->param('id');

        $nav = SysNavigationModel::withoutField([
                'at_page',
                'unique_label',
                'language_id',
                'created_at',
                'updated_at',
                'deleted_at'
            ])
            ->bypk($id)
            ->find();
        if (empty($nav)) {
            return error('导航不存在');
        }

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

    // 新增
    public function save()
    {
        $post = request()->post([
            'unique_label' => '',
            'name',
            'at_platform' => 'pc',
            'desc',
            'status' => 1
        ]);
        if (empty($post['unique_label'])) {
            $post['unique_label'] = uniqid("NAV_");
        }
        $data = array_merge($post, ['language_id' => request()->lang_id]);

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

        $nav = SysNavigationModel::create($data);
        if ($nav->isEmpty()) {
            return error('操作失败');
        }

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

    // 修改
    public function update()
    {
        $id  = request()->param('id');
        $put = request()->put([
            'name',
            'at_platform' => 'pc',
            'desc',
            'status' => 1
        ]);

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


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

        if (!$nav->save($put)) {
            return error('操作失败');
        }

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

    // 删除
    public function delete()
    {
        $id = request()->param('id');

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

        if (!$nav->delete()) {
            return error('操作失败');
        }

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