def upload_image():
    try:
        file = request.files['file']
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # print('upload_image filename: ' + filename)
        id = request.form.get("idUser")
        user = User.query.get(id)
        user.avatar = 'images/' + filename
        db.session.commit()
        return render_template('info.html', list_book_category=utils.get_book_category())
    except:
        return render_template('info.html', list_book_category=utils.get_book_category())
def register():
    if request.method == 'POST':
        username = request.form.get("username")
        password = request.form.get("password", "")
        password2 = request.form.get("password2", "")
        phone = request.form.get("phonenumber")
        if(password == password2 ):
            password = hashlib.md5(password.encode("utf-8")).hexdigest()
            user = User(username=username, password=password, id_UserType=2,phone=phone)
            db.session.add(user)
            token = s.dumps(username, salt='email-confirm')
            TEXT = 'Thannks for sign up! Please enter this link to active your account: http://127.0.0.1:8999/confirm_email/'+token+'/'+username
            SUBJECT='BOOKSTORE: Verify Account'
            message = 'Subject: {}\n\n{}'.format( SUBJECT, TEXT)

            server = smtplib.SMTP_SSL('smtp.googlemail.com', 465)
            server.login('*****@*****.**', 'kimdong20012000')
            server.sendmail('*****@*****.**',username , msg=message)
            db.session.commit()
            return '<h1>Please Confirm your email to active account</h1>'
        else:
            return render_template('base/base.html', list_recommend_book_new=utils.recommend_bookNew(),
                               list_recommend_book=utils.recommend_book(),
                               list_best_sale_book=utils.best_sale_book(),
                               list_book_image=utils.load_book_image(),
                               list_book_category=utils.get_book_category(), err='Mật khẩu không trùng nhau')
    return redirect('/')
def updateInfoUser():
    err_msg = ""
    if request.method == 'POST':
        id = request.form.get("idUser")
        user = User.query.get(id)
        user.name = request.form.get("name")
        user.birthday = request.form.get("birthday")
        user.phone = request.form.get("phone")
        user.address = request.form.get("address")
        user.district = request.form.get("district")
        user.city = request.form.get("city")
        user.gender = int( request.form.get("gender"))
        db.session.commit()
        return render_template('info.html', list_book_category=utils.get_book_category())
def bill_detail(id_bill):
    name = ''
    address =''
    phone=''
    status=0
    list_all_bill = utils.get_all_bill()
    total_amount = 0
    id_b = id_bill
    list_book = utils.load_Book()
    for i in list_all_bill:
        if(str(i.id) == id_bill):
            name = i.name_delivery
            address = i.address_delivery
            phone =i.phone
            status = i.status
            total_amount = i.total_price

    list_item = utils.get_list_item_of_bill(id_bill)
    return  render_template('billdetail.html', list_book_category=utils.get_book_category() , list_item = list_item, name = name, address = address, phone = phone, status = status, total_amount = total_amount, idbill = id_b, list_book = list_book)
def login_admin():
    err_msg = ""
    if request.method == 'POST':
        username = request.form.get("username")
        password = request.form.get("password", "")
        password = hashlib.md5(password.encode("utf-8")).hexdigest()
        user = User.query.filter(User.username == username, User.password == password).first()
        if user:
            if (user.active_mail==1):
                # flash('Logged in successfully.')
                login_user(user=user)
            elif(user.active_mail==0):
                return '<h1>Please Confirm your email to active account</h1>'
        else:
            # flash("Login failed !", category='error')
            return render_template('base/base.html', list_recommend_book_new=utils.recommend_bookNew(),
                                   list_recommend_book=utils.recommend_book(),
                                   list_best_sale_book=utils.best_sale_book(),
                                   list_book_image=utils.load_book_image(),
                                   list_book_category=utils.get_book_category(), err='Sai tài khoản hoặc mật khẩu')

            # return redirect(url_for('login_admin'))
    return redirect('/')
def index():
    return render_template('base/base.html',list_recommend_book_new = utils.recommend_bookNew(), list_recommend_book = utils.recommend_book(), list_best_sale_book= utils.best_sale_book(), list_book_image=utils.load_book_image(), list_book_category=utils.get_book_category(), list_book_literature=utils.list_book_literature())
def info():
    return render_template('info.html',  list_book_category=utils.get_book_category())
def load_detail_book_by_id(id_book):
    book=utils.get_book_by_id(id_book)
    return render_template('single.html', book = book, list_image = utils.get_image_by_id_book(id_book),list_book_category=utils.get_book_category())
def searchCategory(id_category):
    listcate = Book.query.filter(Book.id_category == id_category).all()
    n = len(listcate)
    return render_template('search.html', listBook=listcate, len = n, listImage = utils.loadImageByListIdBook(listcate), list_book_category=utils.get_book_category(),list_book= utils.load_Book(), list_book_image=utils.load_book_image())
def search():
    name=request.form.get('Search')
    listBook = Book.query.filter(Book.name.like('%' + name + '%')).all()

    n = len(listBook)
    return render_template('search.html', listBook = listBook , list_book_category=utils.get_book_category(),len = n,  listImage = utils.loadImageByListIdBook(listBook))
def confirm_pay():
    id_cart, list_item = utils.list_item_of_user(current_user.id)
    total_quantity, total_amount = utils.cart_stats(current_user.id)
    dola = "{:.2f}".format(total_amount/23000)
    return render_template('confirm__pay.html', id_cart = id_cart, list_item = list_item, total_amount = total_amount, total_quantity=total_quantity, list_book = utils.load_Book(),list_book_category=utils.get_book_category(), list_image = utils.get_all_image(), dola = dola)
def about():
    return render_template('about.html',  list_book_category=utils.get_book_category())
def order_history():
    return render_template('order_history.html',list_book_category=utils.get_book_category(), list_bill = utils.get_list_bill(current_user.id), list_bill_detail=BillDetail.query.all())
def index2():
    return render_template('base/base.html',list_book_category=utils.get_book_category())
def payment():
    id_cart, list_item = utils.list_item_of_user(current_user.id)
    total_quantity, total_amount = utils.cart_stats(current_user.id)
    return render_template('payment.html', id_cart = id_cart, list_item = list_item, total_amount = total_amount, total_quantity=total_quantity, list_book = utils.load_Book(),list_book_category=utils.get_book_category(), list_image = utils.get_all_image())