<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use App\Models\Menu;

class MenusTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        return $this->update();
        // 商品管理-(商品列表,商品种类,商品品牌)
        $id = DB::table('menus')->insertGetId(['parent_id' => 0, 'code' => 'GOODS_MANAGE', 'name' => '商品管理', 'seq' => 0]);
        DB::table('menus')->insert([
            ['parent_id' => $id, 'code' => 'GOODS_LIST', 'name' => '商品列表', 'seq' => 0],
            ['parent_id' => $id, 'code' => 'GOODS_TYPE', 'name' => '商品种类', 'seq' => 1],
            ['parent_id' => $id, 'code' => 'GOODS_BRAND', 'name' => '商品品牌', 'seq' => 2],
            ['parent_id' => $id, 'code' => 'GOODS_SKU_LOCATION', 'name' => '商品货架', 'seq' => 3],
            ['parent_id' => $id, 'code' => 'GOODS_COMBINATION', 'name' => '组合商品', 'seq' => 4],
        ]);
        // 店铺管理
        DB::table('menus')->insertGetId(['parent_id' => 0, 'code' => 'SHOP_MANAGE', 'name' => '店铺管理', 'seq' => 10]);
        // 用户管理
        DB::table('menus')->insertGetId(['parent_id' => 0, 'code' => 'USER_MANAGE', 'name' => '用户管理', 'seq' => 20]);
        // 系统管理-(角色管理,权限管理,系统日志)
        $id = DB::table('menus')->insertGetId(['parent_id' => 0, 'code' => 'SYSTEM_MANAGE', 'name' => '系统管理', 'seq' => 30]);
        DB::table('menus')->insert([
            ['parent_id' => $id, 'code' => 'ROLE_MANAGE', 'name' => '角色管理', 'seq' => 0],
//            ['parent_id' => $id,'code' => 'PERMISSION_MANAGE', 'name' => '权限管理', 'seq' => 1],
            ['parent_id' => $id, 'code' => 'SYSTEM_LOG', 'name' => '系统日志', 'seq' => 2],
        ]);
        // 平台
        $id = DB::table('menus')->insertGetId(['parent_id' => 0, 'code' => 'PLAT', 'name' => '平台', 'seq' => 40]);
        DB::table('menus')->insert([
            ['parent_id' => $id, 'code' => 'PLAT_GOODS_LIST', 'name' => '货品列表', 'seq' => 0],
            ['parent_id' => $id, 'code' => 'PLAT_ORDER_LIST', 'name' => '订单列表', 'seq' => 1],
        ]);
        // 团购
        DB::table('menus')->insertGetId(['parent_id' => 0, 'code' => 'GROUP_MANAGEMENT', 'name' => '团购管理', 'seq' => 1]);
    }

    public function update()
    {
        $routes = include(resource_path('lang/zh-CN/permission.php'));
        foreach ($routes as $code => $route) {
            if (false === strpos($code, '.')) {
                DB::table('menus')->updateOrInsert(
                    ['id' => $route['id'], 'parent_id' => $route['parent_id'], 'code' => $code],
                    ['name' => $route['name'], 'show' => $route['show']]
                );
            }
        }
    }
}
