def delete_user():
    text = input('请输入要删除的ID:')
    #users = users

    if text.isdigit():
        uid = int(text)
        cnt,result = db.Delete(uid)

        if cnt != 0:
            utils.success_info("删除用户ID为{0}的用户信息成功。".format(uid))
            print('用户删除成功')
        else:
            print(result)
            utils.warn_info("删除用户,用户ID不存在!")
            print('用户ID错误')
        '''
        for user in users:
            if user.get('id') == uid:
                db.Delete(uid)
                utils.success_info("删除用户{0}信息成功。".format(user['name']))
                #users.remove(user)
                print('用户删除成功')
                break'''

    else:
        utils.warn_info("删除用户,用户ID输入错误!")
        print('用户ID错误')
def is_unlock():
    lock_time = 0
    LOCK_FILE = utils.ReadConfigFile('conf.ini', 'LOG', 'LOGFILE')

    try:
        cxt = utils.ReadFile(LOCK_FILE)
        lock_time = float(cxt)
    except Exception as e:
        pass

    is_unlock = time.time() - lock_time > LOCK_INTERVAL

    if not is_unlock:
        utils.warn_info('锁定用户尝试登录')

    return is_unlock
def  modify_user(users):
    users = users
    user_find = []
    has_error = True

    text = input('请输入用户ID:')
    users = get_user()


    if text.isdigit():
        uid = int(text)
        for user in users:
            if user.get('id') == uid:
                text1 = input('请输入需要更新的用户信息(用户名,年龄, 电话号码, 地址),信息使用空格分隔:')
                user =text1.split()

                if len(user) != USER_INFO_NUM:
                    utils.warn_info("修改用户信息,输入用户信息格式错误!")
                    print('用户数据格式不对,请重新输入!')
                else:
                    name, age, tel, address = user
                    has_error = False

                    if not age.isdigit():
                        utils.warn_info("修改用户信息,输入年龄格式错误!")
                        print('用户数据格式不对,请重新输入!')
                        has_error = True

                    if  not tel.isdigit():
                        utils.warn_info("修改用户信息,输入电话号码格式错误!")
                        print('用户数据格式不对,请重新输入!')
                        has_error = True

                if not has_error:
                    for x in users:
                        if x.get('id') == uid:
                            x['name'], x['age'], x['tel'], x['address'] = user
                            cnt, result = db.Modify(x['name'], x['age'], x['tel'], x['address'], uid)
                            if cnt != 0:
                                utils.success_info("修改用户{0}信息成功。".format(user[0]))
                                print('用户信息修改成功')
                                #打印修改用户的信息
                                user_find.append(x)
                                print_data(user_find)
                            else:
                                print(result)
    else:
        utils.warn_info("修改用户信息,用户信息不存在!")
        print('用户ID错误')
def login():
    is_login = False

    for i in range(MAX_LOGIN_COUNT):
        username = input('请输入用户名:')
        password = getpass.getpass('请输入密码:')

        if username == ADMIN_NAME and password == ADMIN_PASSWD:
            req = requests.get('https://api.github.com/user', headers=HEADERS)
        else:
            req = requests.get('https://api.github.com/user', headers=HEADERS_ERROR)

        if req.ok:
            utils.success_info("管理员登陆成功!")
            print('欢迎管理员登录系统')
            is_login = True
            break
        if MAX_LOGIN_COUNT -1 > i:
            utils.warn_info("管理员用户名或密码有误,错误信息{0}{1}".format(req.status_code, req.reason))
            print('请重新输入用户名,密码!')
        else:
            utils.warn_info("管理员输入的密码错误三次,账号锁定。")
            print('登录失败,锁定用户')
    return is_login
def operate(users):
     while True:
        op = input('请输入操作(list/add/modify/query/delete/output/exit):').strip()
        users = get_user()
        if op == 'list':
            user_find = users
            print_data(user_find)
        elif op == 'add':
            add_user(users)
        elif op == 'modify':
            modify_user(users)
        elif op == 'query':
            query_user(users)
        elif op == 'delete':
            delete_user()
        elif op == 'output':
            utils.WriteExcel(OUTPUT_FILE, users)
            print('用户数据导出完毕!')
        elif op == 'exit':
            print('成功退出,已自动保存数据')
            break
        else:
            utils.warn_info("输入操作指令错误!")
            print('输入操作指令有误,请重新输入')
def add_user(users):
    users = users
    user_find = []

    text = input('请数据用户信息(用户名,年龄, 电话号码, 地址),信息使用空格分隔:')
    user = text.split()

    if len(user) != USER_INFO_NUM:
        utils.warn_info("添加用户,输入用户信息格式错误!")
        print('用户数据格式不对,请重新输入!')
    else:
        name, age, tel, address = user
        has_error = False
        if not age.isdigit():
            utils.warn_info("添加用户,输入年龄格式错误!")
            print('用户数据格式不对,请重新输入!')
            has_error = True

        if  not tel.isdigit():
            utils.warn_info("添加用户,输入电话号码格式错误!")
            print('用户数据格式不对,请重新输入!')
            has_error = True

        if not has_error:
            try:
                uid = max([x.get('id') for x in users] + [0]) + 1
            except Exception as e:
                uid = 1
            cnt, result = db.Insert(uid, name, int(age), tel, address)
            #users.append({'id': uid,'name': name, 'age': int(age),'tel': tel,'address': address})
            if cnt != 0:
                print('用户添加成功!')
                utils.success_info("添加用户{0}信息成功。".format(name))
                user_find.append({'id': uid,'name': name, 'age': int(age),'tel': tel,'address': address})
                print_data(user_find)
            else:
                print(result)