<?php
$rt = \Phpcmf\Service::M('table')->install_schema([
    'tables' => [
        'member_login' => [
            'comment' => '登录日志记录',
            'drop' => false,
            'fields' => [
                'id' => 'int(10) unsigned NOT NULL AUTO_INCREMENT',
                'uid' => 'mediumint(8) unsigned DEFAULT NULL COMMENT \'会员uid\'',
                'type' => 'varchar(30) NOT NULL COMMENT \'登录方式\'',
                'loginip' => 'varchar(50) NOT NULL COMMENT \'登录Ip\'',
                'logintime' => 'int(10) unsigned NOT NULL COMMENT \'登录时间\'',
                'useragent' => 'varchar(255) NOT NULL COMMENT \'客户端信息\'',
            ],
            'indexes' => [
                'PRIMARY KEY (`id`)',
                'KEY `uid` (`uid`)',
                'KEY `loginip` (`loginip`)',
                'KEY `logintime` (`logintime`)',
            ],
        ],
        'member_group' => [
            'comment' => '用户组表',
            'drop' => false,
            'fields' => [
                'id' => 'smallint(5) unsigned NOT NULL AUTO_INCREMENT',
                'name' => 'text NOT NULL COMMENT \'用户组名称\'',
                'price' => 'decimal(10,2) NOT NULL COMMENT \'售价\'',
                'unit' => 'tinyint(1) unsigned NOT NULL COMMENT \'价格单位:1积分，0金钱\'',
                'days' => 'int(10) unsigned NOT NULL COMMENT \'生效时长\'',
                'apply' => 'tinyint(1) unsigned NOT NULL COMMENT \'是否申请\'',
                'register' => 'tinyint(1) unsigned NOT NULL COMMENT \'是否允许注册\'',
                'setting' => 'text NOT NULL COMMENT \'用户组配置\'',
                'displayorder' => 'smallint(5) NOT NULL COMMENT \'排序\'',
            ],
            'indexes' => [
                'PRIMARY KEY (`id`)',
                'KEY `displayorder` (`displayorder`)',
            ],
        ],
        'member_level' => [
            'comment' => '用户组级别表',
            'drop' => false,
            'fields' => [
                'id' => 'smallint(5) unsigned NOT NULL AUTO_INCREMENT',
                'gid' => 'smallint(5) unsigned NOT NULL COMMENT \'用户id\'',
                'name' => 'varchar(255) NOT NULL COMMENT \'名称\'',
                'stars' => 'int(10) NOT NULL COMMENT \'图标\'',
                'value' => 'int(10) unsigned NOT NULL COMMENT \'升级值要求\'',
                'note' => 'text NOT NULL COMMENT \'备注\'',
                'apply' => 'tinyint(1) unsigned NOT NULL COMMENT \'是否允许升级\'',
                'setting' => 'text NOT NULL COMMENT \'等级配置\'',
                'displayorder' => 'smallint(5) NOT NULL COMMENT \'排序\'',
            ],
            'indexes' => [
                'PRIMARY KEY (`id`)',
                'KEY `value` (`value`)',
                'KEY `apply` (`apply`)',
                'KEY `gid` (`gid`)',
                'KEY `displayorder` (`displayorder`)',
            ],
        ],
        'member_group_index' => [
            'comment' => '用户组归属表',
            'drop' => false,
            'fields' => [
                'id' => 'int(10) unsigned NOT NULL AUTO_INCREMENT',
                'uid' => 'int(10) unsigned NOT NULL COMMENT \'用户id\'',
                'gid' => 'smallint(5) unsigned NOT NULL COMMENT \'用户组id\'',
                'lid' => 'smallint(5) unsigned NOT NULL COMMENT \'用户组等级id\'',
                'stime' => 'int(10) unsigned NOT NULL COMMENT \'开通时间\'',
                'etime' => 'int(10) unsigned NOT NULL COMMENT \'结束时间\'',
            ],
            'indexes' => [
                'PRIMARY KEY (`id`)',
                'KEY `uid` (`uid`)',
                'KEY `lid` (`lid`)',
                'KEY `gid` (`gid`)',
            ],
        ],
        'member_group_verify' => [
            'comment' => '用户组申请审核',
            'drop' => false,
            'fields' => [
                'id' => 'int(10) unsigned NOT NULL AUTO_INCREMENT',
                'uid' => 'mediumint(8) unsigned NOT NULL DEFAULT \'0\'',
                'username' => 'varchar(50) NOT NULL COMMENT \'用户名\'',
                'gid' => 'smallint(5) unsigned NOT NULL COMMENT \'用户组id\'',
                'lid' => 'smallint(5) unsigned NOT NULL COMMENT \'用户组等级id\'',
                'status' => 'tinyint(1) unsigned NOT NULL COMMENT \'状态\'',
                'price' => 'decimal(10,2) DEFAULT NULL COMMENT \'已费用\'',
                'content' => 'text NOT NULL COMMENT \'自定义字段信息\'',
                'inputtime' => 'int(10) unsigned NOT NULL',
            ],
            'indexes' => [
                'PRIMARY KEY (`id`)',
                'KEY `uid` (`uid`)',
                'KEY `status` (`status`)',
                'KEY `inputtime` (`inputtime`)',
            ],
        ],
        'member_setting' => [
            'comment' => '用户属性参数表',
            'drop' => false,
            'fields' => [
                'name' => 'varchar(50) NOT NULL',
                'value' => 'mediumtext NOT NULL',
            ],
            'indexes' => [
                'PRIMARY KEY (`name`)',
            ],
        ],
        'member_menu' => [
            'comment' => '用户组菜单表',
            'drop' => false,
            'fields' => [
                'id' => 'smallint(5) unsigned NOT NULL AUTO_INCREMENT',
                'pid' => 'smallint(5) unsigned NOT NULL COMMENT \'上级菜单id\'',
                'name' => 'text NOT NULL COMMENT \'菜单语言名称\'',
                'uri' => 'varchar(255) DEFAULT NULL COMMENT \'uri字符串\'',
                'url' => 'varchar(255) DEFAULT NULL COMMENT \'外链地址\'',
                'mark' => 'varchar(255) DEFAULT NULL COMMENT \'菜单标识\'',
                'hidden' => 'tinyint(1) unsigned DEFAULT NULL COMMENT \'是否隐藏\'',
                'icon' => 'varchar(255) DEFAULT NULL COMMENT \'图标标示\'',
                'group' => 'text DEFAULT NULL COMMENT \'用户组划分\'',
                'site' => 'text DEFAULT NULL COMMENT \'站点划分\'',
                'client' => 'text DEFAULT NULL COMMENT \'终端划分\'',
                'displayorder' => 'int(5) DEFAULT NULL COMMENT \'排序值\'',
            ],
            'indexes' => [
                'PRIMARY KEY (`id`)',
                'KEY `pid` (`pid`)',
                'KEY `mark` (`mark`)',
                'KEY `hidden` (`hidden`)',
                'KEY `uri` (`uri`)',
                'KEY `displayorder` (`displayorder`)',
            ],
        ],
        'member_oauth' => [
            'comment' => '快捷登录用户OAuth授权表',
            'drop' => false,
            'fields' => [
                'id' => 'int(10) NOT NULL AUTO_INCREMENT',
                'uid' => 'mediumint(8) unsigned NOT NULL COMMENT \'会员uid\'',
                'oid' => 'varchar(255) NOT NULL COMMENT \'OAuth返回id\'',
                'oauth' => 'varchar(255) NOT NULL COMMENT \'运营商\'',
                'avatar' => 'varchar(255) NOT NULL COMMENT \'头像\'',
                'unionid' => 'varchar(255) DEFAULT NULL COMMENT \'unionId\'',
                'nickname' => 'varchar(255) NOT NULL COMMENT \'昵称\'',
                'expire_at' => 'int(10) unsigned NOT NULL COMMENT \'绑定时间\'',
                'access_token' => 'varchar(255) DEFAULT NULL COMMENT \'保留\'',
                'refresh_token' => 'varchar(255) DEFAULT NULL COMMENT \'保留\'',
            ],
            'indexes' => [
                'PRIMARY KEY (`id`)',
                'KEY `uid` (`uid`)',
            ],
        ],
        'member_verify' => [
            'comment' => '会员审核表',
            'drop' => false,
            'fields' => [
                'id' => 'smallint(5) unsigned NOT NULL AUTO_INCREMENT',
                'uid' => 'mediumint(8) unsigned NOT NULL COMMENT \'会员uid\'',
                'tid' => 'tinyint(1) unsigned NOT NULL COMMENT \'类别\'',
                'status' => 'tinyint(1) unsigned NOT NULL COMMENT \'状态\'',
                'content' => 'mediumtext NOT NULL COMMENT \'自定义字段信息\'',
                'result' => 'text NOT NULL COMMENT \'处理结果\'',
                'updatetime' => 'int(10) unsigned NOT NULL COMMENT \'处理时间\'',
                'inputtime' => 'int(10) unsigned NOT NULL COMMENT \'提交时间\'',
            ],
            'indexes' => [
                'PRIMARY KEY (`id`)',
                'KEY `tid` (`tid`)',
                'KEY `uid` (`uid`)',
                'KEY `status` (`status`)',
                'KEY `inputtime` (`inputtime`)',
            ],
        ],
    ],
    'seeds' => [
        ['table' => 'member_group', 'data' => [
            'id' => 1,
            'name' => '注册用户',
            'price' => 0.00,
            'unit' => 0,
            'days' => 0,
            'apply' => 1,
            'register' => 1,
            'setting' => '{"level":{"auto":"0","unit":"0","price":"0"},"verify":"0"}',
            'displayorder' => 0,
        ]],
        ['table' => 'member_group_index', 'data' => [
            'id' => 1,
            'uid' => 1,
            'gid' => 1,
            'lid' => 0,
            'stime' => 0,
            'etime' => 0,
        ]],
        ['table' => 'member_setting', 'data' => [
            'name' => 'config',
            'value' => '{"edit_name":"1","edit_mobile":"1","logintime":"","verify_msg":"","pagesize":"","pagesize_mobile":"","pagesize_api":""}',
        ]],
        ['table' => 'member_setting', 'data' => [
            'name' => 'login',
            'value' => '{"code":"1"}',
        ]],
        ['table' => 'member_setting', 'data' => [
            'name' => 'register',
            'value' => '{"close":"0","groupid":"1","field":["username","email"],"cutname":"0","unprefix":"","code":"1","verify":"","preg":"","notallow":""}',
        ]],
        ['table' => 'member_setting', 'data' => [
            'name' => 'auth2',
            'value' => '{"1":{"public":{"home":{"show":"0","is_category":"0"},"form_public":[],"share_category_public":{"show":"1","add":"1","edit":"1","code":"1","verify":"1","exp":"","score":"","money":"","day_post":"","total_post":""},"category_public":[],"mform_public":"","form":null,"share_category":null,"category":null,"mform":null}}}',
        ]],
    ],
]);
if (empty($rt['code'])) {
    log_message('error', 'Member install_schema: '.($rt['msg'] ?? ''));
    return dr_return_data(0, dr_lang('安装失败').'：'.($rt['msg'] ?? ''));
}

\Phpcmf\Service::M('Menu')->db->resetDataCache();
\Phpcmf\Service::M()->table('member_menu')->clear_all();

$menu = require $path.'Config/Menu.php';
foreach ($menu['member'] as $mark => $top) {
    // 插入顶级菜单
    $mark = strlen($mark) > 2 ? $mark : '';
    $top_id = \Phpcmf\Service::M('Menu')->_add('member', 0, $top, $mark, true);
    // 插入链接菜单
    if ($top_id) {
        foreach ($top['link'] as $mark2 => $link) {
            \Phpcmf\Service::M('Menu')->_add('member', $top_id, $link);
        }
    }
}
