<?php

namespace Modules\User;

use Illuminate\Support\Facades\Artisan;
use Modules\User\Models\SignConfig;

class User
{

    protected static string $mainTitle = '用户管理';

    /**
     * Notes   : 模块初始化要做的一些操作
     *
     * @Date   : 2021/3/12 11:34 上午
     * @Author : <Jason.C>
     */
    public static function install(): void
    {
        Artisan::call('migrate', [
            '--path' => 'modules/User/Database/Migrations',
        ]);

        self::createAdminMenu();
    }

    /**
     * Notes   : 创建后台菜单
     *
     * @Date   : 2021/3/17 9:48 上午
     * @Author : <Jason.C>
     */
    protected static function createAdminMenu(): void
    {
        SignConfig::updateOrCreate([
            'id' => 1,
        ], [
            'params' => [],
        ]);
        $menu = config('admin.database.menu_model');

        $main = $menu::create([
            'parent_id' => 0,
            'order'     => 10,
            'title'     => self::$mainTitle,
            'icon'      => 'fa-group',
        ]);

        $main->children()->createMany([
            [
                'order' => 0,
                'title' => '用户列表',
                'icon'  => 'fa-user',
                'uri'   => 'users',
            ],
            [
                'order' => 1,
                'title' => '用户账户',
                'icon'  => 'fa-bars',
                'uri'   => 'users/accounts',
            ],
            [
                'order' => 2,
                'title' => '用户库存',
                'icon'  => 'fa-bars',
                'uri'   => 'users/stocks',
            ],
            //            [
            //                'order' => 2,
            //                'title' => '账变规则',
            //                'icon'  => 'fa-bars',
            //                'uri'   => 'users/rules',
            //            ],
            [
                'order' => 3,
                'title' => '签到规则管理',
                'icon'  => 'fa-bars',
                'uri'   => 'users/signs/1/edit',
            ],
            [
                'order' => 4,
                'title' => '签到展示文字',
                'icon'  => 'fa-bars',
                'uri'   => 'users/sign_texts',
            ],
            [
                'order' => 5,
                'title' => '签到背景图片',
                'icon'  => 'fa-bars',
                'uri'   => 'users/sign_banners',
            ],
            [
                'order' => 6,
                'title' => '用户身份',
                'icon'  => 'fa-graduation-cap',
                'uri'   => 'users/identities',
            ],
            [
                'order' => 7,
                'title' => '身份变动记录',
                'icon'  => 'fa-graduation-cap',
                'uri'   => 'users/identity_logs',
            ],
            //            [
            //                'order' => 6,
            //                'title' => '客服中心',
            //                'icon'  => 'fa-group',
            //                'uri'   => 'users/services',
            //            ],
            [
                'order' => 8,
                'title' => '升级订单',
                'icon'  => 'fa-group',
                'uri'   => 'users/orders',
            ],
        ]);

        $subMenu = $main->children()->create([
            'order' => 10,
            'title' => '短信管理',
            'icon'  => 'fa-commenting',
            'uri'   => '',
        ]);

//        $certify = $main->children()->create([
//            'order' => 8,
//            'title' => '认证管理',
//            'icon'  => 'fa-certificate',
//            'uri'   => '',
//        ]);


        $subMenu->children()->createMany([
            [
                'order' => 1,
                'title' => '短信记录',
                'icon'  => 'fa-mobile',
                'uri'   => 'users/sms',
            ],
            [
                'order' => 2,
                'title' => '短信配置',
                'icon'  => 'fa-cogs',
                'uri'   => 'users/sms/configs',
            ],
            [
                'order' => 3,
                'title' => '短信网关',
                'icon'  => 'fa-map-signs',
                'uri'   => 'users/sms/gateways',
            ],
        ]);

//        $certify->children()->createMany([
//            [
//                'order' => 1,
//                'title' => '认证记录',
//                'icon'  => 'fa-align-justify',
//                'uri'   => 'users/certifications',
//            ],
//            [
//                'order' => 2,
//                'title' => '认证配置',
//                'icon'  => 'fa-cogs',
//                'uri'   => 'users/certifications/configs',
//            ],
//        ]);
    }

    /**
     * Notes   : 卸载模块的一些操作
     *
     * @Date   : 2021/3/12 11:35 上午
     * @Author : <Jason.C>
     */
    public static function uninstall(): void
    {
        $menu = config('admin.database.menu_model');

        $msgMenu = $menu::where('title', '短信管理')->get();
        foreach ($msgMenu as $menu) {
            $menu->delete();
        }

        $mains = $menu::where('title', self::$mainTitle)->get();
        foreach ($mains as $menu) {
            $menu->delete();
        }
    }

}
