def customer_reg():
    if request.method == 'POST':
        try:
            name = request.form['name']
            email = request.form['email']
            phone = request.form['phone']
            password = request.form['password']
            confirmpass = request.form['confirmpass']

            if password != confirmpass:
                flash('Passwords dont match!', 'danger')
                return redirect(url_for('customer_reg'))
            elif (CustomerModel.check_email_exist(email)):
                flash('Email already in use', 'danger')
                return redirect(url_for('customer_reg'))
            else:
                # protect the password by harshing it
                hashedpass = bcrypt.generate_password_hash(password,
                                                           10).decode('utf-8')
                # add the customer to the database
                customer = CustomerModel(name=name,
                                         email=email,
                                         phone_number=phone,
                                         password=hashedpass)
                customer.insert_record()

                flash(
                    'Your account has been successfully created.Please Login',
                    'success')
                return redirect(url_for('customer_login'))

        except Exception as e:
            print(e)

    return render_template('customerreg.html')
def customer_login():
    if request.method == 'POST':
        email = request.form['email']
        password = request.form['password']

        # check if an email exists
        if CustomerModel.check_email_exist(email=email):
            # validate the password
            if CustomerModel.validate_password(email=email, password=password):
                # set the customer session
                session['email'] = email
                session['uid'] = CustomerModel.get_customer_id(email)
                # redirect him to the homepage
                return redirect(url_for('home'))
            else:
                flash('Invalid login credential', 'danger')
                return redirect(url_for('customer_login'))
        else:
            flash('Invalid login credential', 'danger')
            return redirect(url_for('customer_login'))

    return render_template('custlogin.html')