<?php


namespace app\admin\controller;


use think\facade\Db;
use think\facade\Request;

class Web extends Base
{
    protected $middleware = ['app\middleware\CheckValidata::class'];

    //导航栏列表
    public function navList()
    {
        $where['web_id']  = Request::param('web_id',1);
        $data = Db::name('web_nav')->where($where)->order(['sort'=>'desc','nav_id'=>'asc'])->select()->toArray();
        return $this->returnJson($data);
    }

    //修改|添加导航栏
    public function editNav()
    {
        $nav_id = Request::param('nav_id');
        $data['nav_name'] = Request::param('nav_name');
        $data['sort'] = Request::param('sort',0);
        $data['url'] = Request::param('url','');
        $data['web_id'] = Request::param('web_id',1);
        $pic = Request::param('pic');
        $data['pic'] = str_replace(config('app.alioss.pic_url'),'',$pic);
        if($nav_id) {
            $where['nav_id'] = $nav_id;
            $operation = Db::name('web_nav')->where($where)->update($data);
        } else {
            $operation = Db::name('web_nav')->insert($data);
        }
        if($operation) {
            return $this->returnJson();
        } else {
            return $this->returnJson([],'操作失败!',400);
        }
    }

    //删除导航栏
    public function deleteNav()
    {
        $where['nav_id']  = Request::param('nav_id',1);
        $res = Db::name('web_nav')->where($where)->delete();
        if($res) {
            return $this->returnJson();
        } else {
            return $this->returnJson([],'删除失败!',400);
        }
    }

    //对不同的导航栏下的文字进行管理
    public function editWord()
    {
        $data['content'] = json_encode(Request::param('content'));
        $data['web_id'] =$whe['web_id'] = Request::param('web_id');
        $data['nav_id'] = $whe['nav_id']=Request::param('nav_id');
        $exit = Db::name('web_word')->where($whe)->find();
        if($exit) {
            $operation = Db::name('web_word')->where($whe)->update($data);
        } else {
            $operation = Db::name('web_word')->insert($data);
        }
        if($operation) {
            return $this->returnJson();
        } else {
            return $this->returnJson([],'操作失败!',400);
        }
    }

    //获取导航栏的相对于的文字内容
    public function getWord()
    {
        $nav_id = Request::param('nav_id');
        $where['nav_id'] = $nav_id;
        $res = Db::name('web_word')->where($where)->find();
        $result = json_decode($res['content'],true);
        return $this->returnJson($result);
    }

    //删除导航栏的文字内容
    public function delWord()
    {
        $nav_id = Request::param('nav_id');
        $where['nav_id'] = $nav_id;
        $res = Db::name('web_word')->where($where)->delete();
        if($res) {
            return $this->returnJson();
        } else {
            return $this->returnJson([],'删除失败!',400);
        }
    }




}