//
//  CreateAccount1.swift
//  Mindtap_Official
//
//  Created by Lunar on 5/26/21.
//

import UIKit
import FirebaseAuth
import Firebase

class CreateAccount1: UIViewController {
    
    @IBOutlet weak var firstNameEntry: UITextField!
    
    @IBOutlet weak var lastNameEntry: UITextField!
    @IBOutlet weak var phoneNumberEntry: UITextField!
    @IBOutlet weak var DOBEntry: UITextField!
    @IBOutlet weak var badInfo: UILabel!
    
    var firstName = ""
    var lastName = ""
    var phoneNumber = ""
    var DOB = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    func validateFields() -> String? {
        
        // Check that all fields are filled in
        if firstNameEntry.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            lastNameEntry.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            phoneNumberEntry.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||
            DOBEntry.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
            
            return "Please fill in all fields."
            
        }
        
        
        return nil
    }
    

        
    @IBAction func nextButton(_ sender: UIButton) {
        let error = validateFields()
        
        if error != nil{
            showError(error!)
            
        } else{
        
                               
            
    

    
            // Create cleaned versions of the data
            self.firstName = firstNameEntry.text!.trimmingCharacters(in: .whitespacesAndNewlines)
            self.lastName = lastNameEntry.text!.trimmingCharacters(in: .whitespacesAndNewlines)
            self.phoneNumber = phoneNumberEntry.text!.trimmingCharacters(in: .whitespacesAndNewlines)
            self.DOB = DOBEntry.text!.trimmingCharacters(in: .whitespacesAndNewlines)
            print( firstName, lastName, phoneNumber, DOB)

            
            
            // define password variable
              
            
            performSegue(withIdentifier: "NextStep", sender: self)
            
            //send email and password information to next vc
            func prepare(for segue : UIStoryboardSegue,sender: Any?){
                let vc = segue.destination as! CreateAccount2
                vc.finalFirstName = self.firstName
                vc.finalLastName = self.lastName
                vc.finalPhoneNumber = self.phoneNumber
                vc.finalDOB = self.DOB
                }
            
            
        }
    }
    
        
    @IBAction func BackToLogin(_ sender: UIButton) {
        performSegue(withIdentifier: "BackToLogin", sender: self)
        
    }
    
            

    
    
    func showError(_ message:String) {
        
        badInfo.text = message
        badInfo.alpha = 1
    }
    
}
    
    


