from flask import Flask, request, render_template
import subprocess
import os

base_dir = os.path.dirname(os.path.abspath(__file__))
app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        try:
            place = request.form['place']
            start_date = request.form['start_date']
            end_date = request.form['end_date']
            
            if place == "forest":
                url1 = request.form['url1']
                url2 = request.form.get('url2')  # Use get method to handle if key is not present
                script_path = os.path.join(base_dir, 'python_code', 'find_available_rooms_forest.py')
                
                # Call the script with the necessary arguments
                args = ['python', script_path, start_date, end_date, url1]
                if url2:  # Check if url2 is not empty
                    args.append(url2)
                subprocess.call(args)

            elif place == "ecology":
                number_of_person = request.form['participants']
                ecology_option = request.form['ecology_option']
                
                script_path = os.path.join(base_dir, 'python_code', 'find_available_rooms_ecology.py')
                
                # Call the script with the necessary arguments
                subprocess.call(['python', script_path, ecology_option, start_date, end_date, number_of_person])
            
            return 'Submitted'
        except Exception as e:
            print(e)
            return 'Error processing the request'
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=False)
