from flask import Flask,render_template,request,redirect
import csv

app = Flask(__name__)


@app.route('/')
@app.route('/index.html')
def home():
    return render_template('index.html')

@app.route('/work.html')
def home1():
    return render_template('work.html')

@app.route('/thankyou.html')
def home2():
    return render_template('thankyou.html')

def write_to_file(data):
	with open('database.txt', mode='a') as database:
		email=data['email']
		subject=data['subject']
		message=data['message']
		file=database.write(f'\n {email},{subject},{message}')


def write_to_csv(data):
	with open('database.csv',newline='', mode='a') as database:
		email=data['email']
		subject=data['subject']
		message=data['message']
		csv_writer= csv.writer(database, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
		csv_writer.writerow([email,subject,message])

@app.route('/submit_form', methods=['POST', 'GET'])
def submit():  
	if request.method == 'POST':
		data = request.form.to_dict()
		write_to_csv(data)
		return redirect('thankyou.html')
	else:
		return 'something went wrong try again'


if __name__ == "__main__":
	app.run(debug=True)