def homeMain():
    if request.method == 'POST':
        print(request.form['name'])
        data = request.form['name']
        item = session.query(Items).filter_by(name=data).one()
        if item.user_id == current_user.id:
            session.delete(item)
            failed = False
            try:
                session.commit()
            except Exception as e:
                session.rollback()
                session.flush()
                failed = True
                if not failed:
                    flash(f'Deleted Successfully', 'success')
                else:
                    flash(f'Not Deleted Successfully', 'danger')

                return redirect(url_for('homeMain'))
        else:
            flash(f'User is not Authenticated to Delete the Item', 'danger')

    return render_template('main.html',
                           categorie=session.query(Categories).all(),
                           items=session.query(Items)
                           .order_by(Items.id.desc()).all(),
                           current_user=current_user)
def add_category():
    if not current_user.is_authenticated:
        return redirect(url_for('homeMain'))
    name = ''
    if request.method == 'POST':
        for key in request.form:
            if key == 'ItemName':
                name = request.form[key]
                print(name)
        category = Categories(category_name=name, user_id=current_user.id)
        session.add(category)
        failed = False
        try:
            session.commit()
        except Exception as e:
            session.rollback()
            session.flush()
            failed = True

        if not failed:
            flash(f'Added Successfully', 'success')
        else:
            flash(f'Not Added Successfully', 'danger')
        return redirect(url_for('homeMain'))
    return render_template('add_category.html')
def getCategories(categories_name):
    if request.method == 'POST':
        print(request.form['name'])
        data = request.form['name']
        item = session.query(Items).filter_by(name=data).one()
        session.delete(item)
        failed = False
        try:
            session.commit()
        except Exception as e:
            session.rollback()
            session.flush()
            failed = True

        if not failed:
            flash(f'Deleted Successfully', 'success')
        else:
            flash(f'Not Deleted Successfully', 'danger')

        return redirect(url_for('getCategories'))

    cat_id = session.query(Categories). \
        filter(Categories.category_name == categories_name).first()
    item = session.query(Items). \
        filter(Items.cat_id == cat_id.category_id).all()
    print(cat_id.category_id)
    return render_template('view_items.html',
                           categorie=session.query(Categories).all(),
                           items=item,
                           current_user=current_user)
def editItem(itemName):
    if not current_user.is_authenticated:
        return redirect(url_for('homeMain'))
    name = ''
    desc = ''
    cat_id = ''
    print(itemName)
    item = session.query(Items).filter(Items.name == itemName).first()
    cat = session.query(Categories). \
        filter(Categories.category_id == item.cat_id).first()
    categories = session.query(Categories).all()
    if request.method == 'POST':
        for key in request.form:
            if key == 'ItemName':
                name = request.form[key]
                print(name)

            if key == 'ItemDesc':
                desc = request.form[key]
                print(desc)

            if key == 'ItemSelect':
                cat_id = request.form[key]
                print(cat_id)
        if item.user_id == current_user.id:
            print(name + ' ' + desc + ' ' + cat_id + "Data to be Updated")
            item = session.query(Items).filter_by(id=item.id).one()
            item.name = name
            item.description = desc
            item.cat_id = cat_id
            item.user_id = current_user.id
            session.add(item)
            failed = False
            try:
                session.commit()
            except Exception as e:
                session.rollback()
                session.flush()
                failed = True

            if not failed:
                flash(f'Updated Successfully', 'success')
            else:
                flash(f'Not Updated Successfully', 'danger')

            return redirect(url_for('homeMain'))
        else:
            flash(f'User is not Authenticated to Update this Item', 'danger')
    return render_template('edit_item.html',
                           item=item, cat=cat, categories=categories)
def addItem():
    if not current_user.is_authenticated:
        return redirect(url_for('homeMain'))
    name = ''
    desc = ''
    cat_id = ''
    categories = session.query(Categories).all()
    if request.method == 'POST':
        for key in request.form:
            if key == 'ItemName':
                name = request.form[key]
                print(name)

            if key == 'ItemDesc':
                desc = request.form[key]
                print(desc)

            if key == 'ItemSelect':
                cat_id = request.form[key]
                print(cat_id)

        item = Items(name=name,
                     description=desc,
                     user_id=current_user.id, cat_id=cat_id)
        session.add(item)
        try:
            session.commit()
        except Exception as e:
            session.rollback()
            session.flush()
            failed = True
            if not failed:
                flash(f'Added Successfully', 'success')
            else:
                flash(f'Not Added Successfully', 'danger')

        return redirect(url_for('homeMain'))

    return render_template('addItem.html', categories=categories)