<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\SysNavigationItemModel;
use app\admin\validate\v1\NavigationItemValidate;

/**
 * 导航项管理控制器
 */
class NavigationItem
{
    // 导航列表树
    public function index()
    {
        $param = request()->get([
            'name',
            'nav_id',
            'created_at'
        ]);

        $navigations = SysNavigationItemModel::alias('item')
            ->field([
                'item.id',
                'item.pid',
                'item.name',
                'item.sort',
                'item.blank',
                'item.status',
                'item.created_at',
                'nav.name' => 'nav_name'
            ])
            ->join('sys_navigation nav', 'nav.id = item.nav_id')
            ->where('nav.language_id', '=', request()->lang_id)
            ->where(function($query) use($param) {
                if (!empty($param['nav_id'])) {
                    $query->where('item.nav_id', '=', $param['nav_id']);
                }
                if (!empty($param['name'])) {
                    $query->where('item.name', 'like', "%{$param['name']}%");
                }
                if (!empty($param['created_at'])) {
                    $value = explode(',', $param['created_at']);
                    if (count($value) > 1) {
                        if ($value[1] == $value[0]) {
                            $value[1] = date('Y-m-d 23:59:59', strtotime($value[1]));
                        }
                        $query->whereBetweenTime('item.created_at', $value[0], $value[1]);
                    } else {
                        $query->whereTime('item.created_at', '>=', $value[0]);
                    }
                }
            })
            ->order(['item.nav_id' => 'asc', 'item.sort' => 'asc', 'item.id' => 'asc'])
            ->select();

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

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

        $nav = SysNavigationItemModel::field([
                'id',
                'pid',
                'name',
                'desc',
                'image',
                'nav_id',
                'sort',
                'status',
                'blank',
                'link_to',
                'link'
            ])
            ->bypk($id)
            ->find();
        if (empty($nav)) {
            return error('导航不存在');
        }

        // 组装用于前台回显的链接数据
        $nav['link_echo_data'] = System::getEchoDataBySystemPageUrl($nav['link_to'], $nav['link']);

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

    // 导航新增
    public function save()
    {
        $post = request()->post([
            'pid',
            'nav_id',
            'name',
            'desc',
            'icon',
            'image',
            'link_to' => 'custom',
            'link',
            'sort',
            'blank'  => 0,
            'status' => 1
        ]);

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

        $nav_item = SysNavigationItemModel::create($post);
        if ($nav_item->isEmpty()) {
            return error('操作失败');
        }
        return success('操作成功');
    }

    // 导航更新
    public function update()
    {
        $id  = request()->param('id');
        $put = request()->put([
            'pid',
            'nav_id',
            'name',
            'desc',
            'icon',
            'image',
            'link_to',
            'link',
            'sort',
            'blank'  => 0,
            'status' => 1
        ]);

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

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

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

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

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

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

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

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

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