<?php

namespace app\admin\controller;

use think\Db;

class Nav extends Common
{
    //模型table
    private static $_model = 'model';
    //栏目table
    private static $_category = 'category';


    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        //获取已定义模型
        $model = Db::name(self::$_model)->field('id,name')->select();
        //已添加栏目
        $category = getAllCategory('all');

        $this->assign('model', $model);
        $this->assign('category', $category);
    }

    //导航页
    function index()
    {
        return $this->fetch();
    }

    //添加导航UI
    function add()
    {
        if (request()->isGet()) {
            //获取主题下的列表和展示模板
            $theme = get_system_value('site_theme');
            $dir = 'template/index/'. $theme. '/';
            $list_dir=glob($dir .'List_*');
            $list_dir2=glob($dir .'Guestbook_*');
            $list_dir = array_merge($list_dir, $list_dir2);
            $show_dir=glob($dir .'Show_*');
            $list_template = $show_template = [];
            foreach ($list_dir as $key => $value) {
                $list_template[] = str_replace($dir, '', $value);
            }
            foreach ($show_dir as $key => $value) {
                $show_template[] = str_replace($dir, '', $value);
            }
            $this->assign([
                'list_template' => $list_template,
                'show_template' => $show_template
                ]);
            return $this->fetch();
        } elseif (request()->isPost()) {
            $data = input('post.');
            //新增导航
            $flag = Db::name(self::$_category)->insert($data);
            if ($flag) {
                exit(json_encode(['status' => 1, 'msg' => '添加栏目成功','url' => url('nav/index'),'type' => 'nav']));
            }else{
                exit(json_encode(['status' => 0, 'msg' => '添加栏目失败','url' => url('nav/index'),'type' => 'nav']));
            }
        }

    }

    /*
     * 编辑导航
     */
    public function edit(){
        if (request()->isPost()) {
            $data = input('post.');
            $id = $data['id'];
            unset($data['id']);
            $flag = Db::name(self::$_category)->where(['id' => $id])->update($data);
            if ($flag !== false) {
                 exit(json_encode(['status' => 1, 'msg' => '修改栏目成功','url' => url('nav/index'),'type' => 'nav']));
            }else{
                 exit(json_encode(['status' => 0, 'msg' => '修改栏目失败','url' => url('nav/index'),'type' => 'nav']));
            }
        } else {
            $id = input('param.id/d',0);
            //获取主题下的列表和展示模板
            $theme = get_system_value('site_theme');
            
            $dir = 'template/index/'. $theme. '/';
            $list_dir=glob($dir .'List_*');
            $show_dir=glob($dir .'Show_*');
            foreach ($list_dir as $key => $value) {
                $list_template[] = str_replace($dir, '', $value);
            }
            foreach ($show_dir as $key => $value) {
                $show_template[] = str_replace($dir, '', $value);
            }
            $this->assign([
                'list_template' => $list_template,
                'show_template' => $show_template
                ]);
            $data = Db::name(self::$_category)->where(['id' => $id])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }
        
    }

    /*
     * 删除导航
     */
    public function dele(){
        $id = input('param.id/d',0);
        $flag = Db::name(self::$_category)->where(['id' => $id])->delete();
        if ($flag !== false) {
            exit(json_encode(['status' => 1, 'msg' => '删除成功']));
        }else{
            exit(json_encode(['status' => 0, 'msg' => '删除失败']));
        }
    }
}
