def productdetails(request):
    if request.method == 'POST' and request.POST['operation'] == 'edit':
        user_id = request.POST['editThis']
        product1 = tb_product()
        product1.connectToDB('localhost', 'root', '')
        products1 = product1.getData(user_id)
        product1.closeConnection()
        return render(request, 'productdetails.html', {'products': products1})
    else:
        return redirect(home)
def productactive(request):
    if 'useradmin' in request.COOKIES:
        product = tb_product()
        product.connectToDB('localhost', 'root', '')
        ID = request.GET['activeThis']
        status = request.GET['toggle1']
        product.updateActive(ID, status)
        product.closeConnection()
        return redirect(producthome)
    else:
        return redirect(touserhome)
def producthome(request):
    if 'useradmin' in request.COOKIES:
        product = tb_product()
        product.connectToDB('localhost', 'root', '')
        if request.method == "POST":
            if request.POST['operation'] == 'delete':
                product.deleteProduct(request.POST['delThis'])
                product.closeConnection()
                return redirect(producthome)
        products = product.getAllData()
        product.closeConnection()
        return render(request, 'product/home.html', {
            'products': products,
            'total': len(products)
        })
    else:
        return redirect(touserhome)
def insertednewproduct(request):
    if 'useradmin' in request.COOKIES and request.method == 'POST':
        product = tb_product()
        product.connectToDB('localhost', 'root', '')
        pname = request.POST['pname']
        pdetails = request.POST['pdetails']
        pprice = request.POST['pprice']
        photo = request.FILES['photo']
        category = request.POST['category']
        filePath = "/product/{}.jpg".format(pname)
        f = open("ecom_app/static/images/{}".format(filePath), 'wb')
        for i in photo:
            f.write(i)
        f.close()
        product.insertProduct(pname, pdetails, pprice, filePath, category)
        product.closeConnection()
        return redirect(producthome)
    else:
        return redirect(touserhome)
def editordermaster(request):
    if 'useradmin' in request.COOKIES:
        if request.method == 'POST' and request.POST['operation'] == 'edit':
            user_id = request.POST['editThis']
            product = tb_product()
            product.connectToDB('localhost', 'root', '')
            products = product.getData(user_id)
            product.closeConnection()
            category = tb_category()
            category.connectToDB('localhost', 'root', '')
            categories = category.getAllCategoryNames()
            category.closeConnection()
            return render(request, 'product/edit.html', {
                'products': products,
                'categories': categories
            })
        else:
            return redirect(touserhome)
    else:
        return redirect(touserhome)
def editedproduct1(request):
    if 'useradmin' in request.COOKIES and request.method == 'POST':
        product = tb_product()
        product.connectToDB('localhost', 'root', '')
        id = request.POST['id']
        pname = request.POST['pname']
        pdetails = request.POST['pdetails']
        pprice = request.POST['pprice']
        category = request.POST['category']
        filePath = "/product/{}.jpg".format(pname)
        try:
            photo = request.FILES['photo']
            f = open("ecom_app/static/images{}".format(filePath), 'wb')
            for i in photo:
                f.write(i)
            f.close()
        except:
            pass
        product.editProduct(pname, pdetails, pprice, filePath, category, id)
        product.closeConnection()
        return redirect(home)
    else:
        return redirect(home)
def product(request):
    product = tb_product()
    product.connectToDB('localhost', 'root', '')
    products = product.getAllData()
    product.closeConnection()
    return render(request, 'product.html', {'products': products, 'total': len(products)})