package com.ynnu.stumanage.user_main.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.ynnu.stumanage.R;
import com.ynnu.stumanage.function.Activity.AddInfo;
import com.ynnu.stumanage.function.Activity.FindPsw_Result;
import com.ynnu.stumanage.httpHelper.HttpHelper;
import com.ynnu.stumanage.session.Userid.Userid;
import com.ynnu.stumanage.systembartint.SystemBartint;

import SystemBarTint.SystemBarTintManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FindPsw extends Activity {
	private EditText stu_id = null;
	private EditText oldpsw = null;
	private EditText newpsw1 = null;
	private EditText newpsw2 = null;
	private Button confirm_btn = null;
	private HttpHelper httphelper = new HttpHelper();
	private HttpEntity httpentity = null;
	private HttpResponse httpresponse = null;
	private String result = "";
	private String url = "http://" + "112.74.200.103"+ "/PersonalMarkManageService/FindPsw";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// È¡Ïû±êÌâÀ¸
		setContentView(R.layout.findpassword);
		// °²×¿°æ±¾´óÓÚ4.4ÉèÖÃÎª³Á½þÊ½×´Ì¬À¸
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			SystemBartint.setTranslucentStatus(true, FindPsw.this);
		}
		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setStatusBarTintColor(Color.parseColor("#2B78E4"));

		// °ó¶¨×é¼þ
		stu_id = (EditText) findViewById(R.id.stu_id);
		oldpsw = (EditText) findViewById(R.id.old_psw);
		newpsw1 = (EditText) findViewById(R.id.new_psw1);
		newpsw2 = (EditText) findViewById(R.id.new_psw2);
		confirm_btn = (Button) findViewById(R.id.findpsw_btn);

		confirm_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (checkfindpsw()) {
					Intent intent = new Intent(FindPsw.this,
							MainActivity.class);
					startActivity(intent);
					finish();

				} else {
					if (!result.equals("")) {
						if (result.equals("erro_stu_id")) {
							HttpHelper.showMsg(FindPsw.this, "Ñ§ºÅ²»´æÔÚ");
						}
						if (result.equals("erro_oldpsw")) {
							HttpHelper.showMsg(FindPsw.this, "Ô­Ê¼ÃÜÂë´íÎó£¬ÇëÖØÐÂÊäÈë");
						}
					}
				}
			}
		});

	}

	public boolean checkfindpsw() {
		String stu_id1 = stu_id.getText().toString().trim();
		String oldpsw1 = oldpsw.getText().toString().trim();
		String newpsw11 = newpsw1.getText().toString().trim();
		String newpsw12 = newpsw2.getText().toString().trim();
		if (stu_id1.length() == 0) {
			HttpHelper.showMsg(FindPsw.this, "ÇëÊäÈëÑ§ºÅ");
			return false;
		}
		if (oldpsw1.length() == 0) {
			HttpHelper.showMsg(FindPsw.this, "ÇëÊäÈëÔ­ÃÜÂë");
			return false;
		}
		if (newpsw11.length() == 0) {
			HttpHelper.showMsg(FindPsw.this, "ÇëÊäÈëÐÂÃÜÂë");
			return false;
		}
		if (newpsw12.length() == 0) {
			HttpHelper.showMsg(FindPsw.this, "ÇëÈ·ÈÏÐÂÃÜÂë");
			return false;
		}
		if (!newpsw11.equals(newpsw12)) {
			HttpHelper.showMsg(FindPsw.this, "Á½´ÎÃÜÂë²»Ò»ÖÂ");
			return false;
		}
		List<NameValuePair> updatepsw = new ArrayList<NameValuePair>();
		NameValuePair stu_id = new BasicNameValuePair("stu_id", stu_id1);
		NameValuePair oldpswinfo = new BasicNameValuePair("oldpsw", oldpsw1);
		NameValuePair newpswinfo = new BasicNameValuePair("newpsw", newpsw12);
		updatepsw.add(stu_id);
		updatepsw.add(oldpswinfo);
		updatepsw.add(newpswinfo);
		httpresponse = httphelper.sendpost(url, updatepsw);
		httpentity = httpresponse.getEntity();
		result = httphelper.handle(httpentity);
		if (result.equals("true")) {
			return true;
		} else {
			return false;
		}

	}

	// ¼àÌý·µ»Ø°´Å¥
	public void return2(View v) {
		Intent intent = new Intent(FindPsw.this, MainActivity.class);
		startActivity(intent);
		finish();
	}

	// ÎïÀí¼ü·µ»Ø¼à¿Ø
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(FindPsw.this, MainActivity.class);
			startActivity(intent);
			finish();

		}
		return super.onKeyDown(keyCode, event);
	}

}
