<?php

namespace Modules\Task;

use Illuminate\Support\Facades\Artisan;

class Task
{

    protected static string $mainTitle = '任务管理';

    /**
     * Notes: 模块初始化要做的一些操作
     *
     * @Author: 玄尘
     * @Date  : 2021/9/26 11:23
     */
    public static function install()
    {
        Artisan::call('migrate', [
            '--path' => 'modules/Task/Database/Migrations',
        ]);

        self::createAdminMenu();
    }

    protected static function createAdminMenu()
    {
        $menu = config('admin.database.menu_model');

        $exists = $menu::where('title', self::$mainTitle)->exists();
        if (! $exists) {
            $main = $menu::create([
                'parent_id' => 0,
                'order'     => 3,
                'title'     => '任务管理',
                'icon'      => 'fa-archive',
            ]);
            $main->children()
                ->create([
                    'order' => 0,
                    'title' => '任务列表',
                    'icon'  => 'fa-align-left',
                    'uri'   => 'tasks',
                ]);

            $main->children()
                ->create([
                    'order' => 1,
                    'title' => '分类管理',
                    'icon'  => 'fa-align-left',
                    'uri'   => 'tasks/categories',
                ]);
            $main->children()
                ->create([
                    'order' => 3,
                    'title' => '任务记录',
                    'icon'  => 'fa-align-left',
                    'uri'   => 'tasks/users',
                ]);
        }
    }

    /**
     * Notes: 卸载模块的一些操作
     *
     * @Author: 玄尘
     * @Date  : 2021/9/26 11:24
     */
    public static function uninstall()
    {
        $menu = config('admin.database.menu_model');

        $mains = $menu::where('title', self::$mainTitle)->get();

        foreach ($mains as $main) {
            $main->delete();
        }
    }

}
