def update_reviews():
    inputs = list(request.form.values())
    key = [inputs[0]]
    data, _ = sql_command('SELECT * from reviews WHERE rid = ?', key)
    if len(data) == 0:
        command = '''INSERT INTO reviews VALUES (?,?,?,?,?)'''
        update_(command, inputs)
        print('data inserted!')
        flash('Data inserted', 'success')

    else:  # update row
        command = '''UPDATE reviews
                    SET rid = ?, bid = ?, uid = ?, stars = ?, text = ?
                    WHERE rid = ?;
                    '''
        update_(command, inputs+key)
        print('data updated!')
        flash('Data updated', 'success')

    return redirect(url_for('reviews'))
def update_checkins():
    inputs = list(request.form.values())
    key = [inputs[0]]
    data, _ = sql_command('SELECT * from checkins WHERE bid = ?', key)

    if len(data) == 0:
        command = '''INSERT INTO checkins VALUES (?,?,?,?,?,?,?,?)'''
        update_(command, inputs)
        print('data inserted!')
        flash('Data inserted', 'success')

    else:  # update row
        command = '''UPDATE checkins
                    SET bid = ?, Sunday = ?, Monday = ?, Tuesday = ?, Wednesday = ?, Thursday = ?, Friday = ?, Saturday = ?
                    WHERE bid = ?;
                    '''
        update_(command, inputs+key)
        print('data updated!')
        flash('Data updated', 'success')

    return redirect(url_for('checkins'))
def update_business():
    inputs = list(request.form.values())
    key = [inputs[0]]
    data, _ = sql_command('SELECT * from business WHERE bid = ?', key)

    if len(data) == 0:
        command = '''INSERT INTO business VALUES (?,?,?,?,?,?)'''
        update_(command, inputs)
        print('data inserted!')
        flash('Data inserted', 'success')

    else:  # update row
        command = '''UPDATE business
                    SET bid = ?, bname = ?, categories = ?, active = ?, review_count = ?, stars = ?
                    WHERE bid = ?;
                    '''
        update_(command, inputs+key)
        print('data updated!')
        flash('Data updated', 'success')

    return redirect(url_for('business'))
def getdata():
    command = request.args.get("command")
    data, col = sql_command(command)
    return jsonify({"data": data, "col": col})
def checkins():
    data, _ = sql_command("SELECT * FROM checkins;")
    active = ['no', 'no', 'no', 'no', 'active', 'no', 'no']
    return render_template("checkins.html", data=data, active=active)
def reviews():
    data, _ = sql_command("SELECT * FROM reviews;")
    active = ['no', 'no', 'no', 'active', 'no', 'no', 'no']
    return render_template("reviews.html", data=data, active=active)
def users():
    data, _ = sql_command("SELECT * FROM users;")
    active = ['no', 'no', 'active', 'no', 'no', 'no', 'no']
    return render_template("users.html", data=data, active=active)
def business():
    data, _ = sql_command("SELECT * FROM business;")
    active = ['no', 'active', 'no', 'no', 'no', 'no', 'no']
    return render_template("business.html", data=data, active=active)