<?php

namespace modules\department;

use app\common\library\Menu;
use app\admin\model\MenuRule;
use ba\module\moduleException;
use ba\module\Server;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use think\Exception;

class Department
{
    private $uid = 'department';

    public function install()
    {
        $pMenu    = MenuRule::where('name', 'auth')->value('id');
        $newMenus = [
            [
                'type'      => 'menu',
                'title'     => '部门管理',
                'name'      => 'department/department',
                'path'      => 'department/department',
                'icon'      => 'fa fa-sitemap',
                'menu_type' => 'tab',
                'component' => '/src/views/backend/department/department/index.vue',
                'keepalive' => '0',
                'pid'       => $pMenu ? $pMenu : 0,
                'children'  => [
                    ['type' => 'button', 'title' => '查看', 'name' => 'department/department/index'],
                    ['type' => 'button', 'title' => '添加', 'name' => 'department/department/add'],
                    ['type' => 'button', 'title' => '编辑', 'name' => 'department/department/edit'],
                    ['type' => 'button', 'title' => '删除', 'name' => 'department/department/del'],
                    ['type' => 'button', 'title' => '快速排序', 'name' => 'department/department/sortable'],
                ],
            ],
            [
                'type'      => 'menu',
                'title'     => '岗位管理',
                'name'      => 'department/jobs',
                'path'      => 'department/jobs',
                'icon'      => 'fa fa-id-badge',
                'menu_type' => 'tab',
                'component' => '/src/views/backend/department/jobs/index.vue',
                'keepalive' => '1',
                'pid'       => $pMenu ? $pMenu : 0,
                'children'  => [
                    ['type' => 'button', 'title' => '查看', 'name' => 'department/jobs/index'],
                    ['type' => 'button', 'title' => '添加', 'name' => 'department/jobs/add'],
                    ['type' => 'button', 'title' => '编辑', 'name' => 'department/jobs/edit'],
                    ['type' => 'button', 'title' => '删除', 'name' => 'department/jobs/del'],
                    ['type' => 'button', 'title' => '快速排序', 'name' => 'department/jobs/sortable'],
                ],
            ]
        ];
        Menu::create($newMenus);
    }

    public function uninstall()
    {
        Menu::delete('department/department', true);
        Menu::delete('department/jobs', true);
    }

    public function enable()
    {
        Menu::enable('department/department');
        Menu::enable('department/jobs');
    }

    public function disable()
    {
        Menu::disable('department/department');
        Menu::disable('department/jobs');
    }

    public function update()
    {
        // 兼容系统v1.1.2的语言包按需加载
        // 寻找安装时备份中的lang/pages文件，如果有，还原到lang/backend内而不是原位置
        $ebakDir = root_path() . 'modules' . DIRECTORY_SEPARATOR . 'ebak' . DIRECTORY_SEPARATOR;
        $zipFile = $ebakDir . $this->uid . '-install.zip';
        $zipDir  = false;
        if (is_file($zipFile)) {
            try {
                $zipDir = Server::unzip($zipFile);
            } catch (moduleException|Exception $e) {
                // skip
            }
        }
        if ($zipDir) {
            $oldBaInputs = [
                path_transform('web\src\lang\pages\zh-cn\department\department.ts'),
                path_transform('web\src\lang\pages\en\department\department.ts'),
                path_transform('web\src\lang\pages\zh-cn\department\jobs.ts'),
                path_transform('web\src\lang\pages\en\department\jobs.ts'),
            ];
            foreach ($oldBaInputs as $oldBaInput) {
                @unlink(root_path() . $oldBaInput);
            }

            foreach (
                $iterator = new RecursiveIteratorIterator(
                    new RecursiveDirectoryIterator($zipDir, FilesystemIterator::SKIP_DOTS),
                    RecursiveIteratorIterator::SELF_FIRST
                ) as $item
            ) {
                $ebakFile = path_transform($iterator->getSubPathName());
                if (!$item->isDir() && in_array($ebakFile, $oldBaInputs)) {
                    $newFileName = str_replace(DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR . 'backend' . DIRECTORY_SEPARATOR, $ebakFile);
                    copy($item, root_path() . $newFileName);
                }
            }
        }
        deldir($zipDir);
        del_empty_dir(root_path() . 'web/src/lang/pages/en');
        del_empty_dir(root_path() . 'web/src/lang/pages/zh-cn');
    }

}