//
//  SMSViewController.swift
//  SocketChat
//
//  Created by Justine on 16/5/31.
//  Copyright © 2016年 AppCoda. All rights reserved.
//

import UIKit
import Alamofire
class SMSViewController: UIViewController {
    @IBOutlet weak var mobile: UITextField!
    @IBOutlet weak var smscode: UITextField!
    let serverid = GET_SERVER_ID as! String
    let userid = GET_USER_ID as! Int
    let loginname = GET_USER_NAME as! String
    var afmobile:String?
    @IBAction func requireCode(sender: UIButton) {
        var remainingSeconds: Int = 60
        sender.enabled = false
        sender.setTitle(String(remainingSeconds), forState: UIControlState.Disabled)
        
        //请求验证码
        afmobile = trimstring(self.mobile.text!,trimchar: " ")
        /*待优化手机号匹配规则
         
         
         
         
         
        */
        let param:[String:AnyObject] = ["serverID":serverid,
                     "loginName":loginname,
                     "userID":userid,
                     "mobile":self.afmobile!
                     ]
        request(.POST, RequireSMSURL, parameters: param).responseJSON { (R) in
            guard R.result.error == nil else{
                print("弹窗提示验证码请求失败,请求错误")
                return
            }
            print("弹窗提示验证码发送成功")
        }
        
        
        
    }
    @IBAction func checkCode(sender: UIButton){
        
        let param:[String:AnyObject] = ["serverID":serverid,
                                        "loginName":loginname,
                                        "userID":userid,
                                        "mobile":self.mobile.text!,
                                        "code":self.smscode.text!
        ]
        //发送验证码到服务端比对 正确则RootView 反之AlertController
        request(.POST, CheckSMSURL, parameters: param).responseJSON { (R) in
            guard R.result.error == nil else{
                print("弹窗请求错误原因")
                print("请求出错")
                return
            }
            let data = R.result.value!
            print("弹窗提示登录成功，然后显示主界面")
            print(data["message"])
            self.mobile.resignFirstResponder()
            self.smscode.resignFirstResponder()
            APPDELEGATE.showRootViewContorller()
            
        }
        
    }

    func timer(times:Int){
        
    }
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
