//
//  NoteViewController.swift
//  Kalker

import UIKit
import Alamofire
import SwiftyJSON

class NoteViewController: UIViewController {

    var idCaregiver: Int!
    var idPatient: Int!
    
    @IBOutlet weak var subView: UIView!
    @IBOutlet weak var noteTextField: UITextView!
    @IBOutlet weak var saveButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.hideKeyboardWhenTappedAround()
        
        subView.layer.cornerRadius = 5
        subView.layer.masksToBounds = true
        
        noteTextField.becomeFirstResponder()
        
        loadNote()
    }
    
    func loadNote() {
        let parameters: Parameters = ["parameter": 1,
                                      "idPatient": idPatient!,
                                      "idCaregiver": idCaregiver]
        
        Alamofire.request("https://kalkerapp.pl/api/note.php", method: .post, parameters: parameters)
            .responseJSON { response in
                if let jsonData = response.result.value {
                    let json = JSON(jsonData)
                    self.noteTextField.text = json[0]["note"].string!
                }
        }
    }
    
    @IBAction func saveNote(_ sender: UIButton) {
        clickAnimation(button: saveButton)
        let parameters: Parameters = ["parameter": 2,
                                      "idPatient": idPatient!,
                                      "idCaregiver": idCaregiver,
                                      "note": noteTextField.text!]
        
        Alamofire.request("https://kalkerapp.pl/api/note.php", method: .post, parameters: parameters)
            .responseJSON { response in
                if let jsonData = response.result.value {
                    let json = JSON(jsonData)
                    print(json)
                }
        }
        
        dismiss(animated: true, completion: nil)
    }

    @IBAction func closeButton(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewDidLayoutSubviews() {
        saveButton.setWhiteButton()
    }
}
