package com.szu;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.szu.accountsoft.dao.FlagDAO;
import com.szu.accountsoft.model.Tb_flag;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Accountflag extends Activity {
	EditText txtFlag;// 创建EditText组件对象
	Button btnflagSaveButton;// 创建Button组件对象
	Button btnflagCancelButton;// 创建Button组件对象
	TextView txtTime, txtLocation;
	String str_address;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.accountflag);
		
		txtTime = (TextView) findViewById(R.id.txtTime);
		txtLocation = (TextView) findViewById(R.id.txtLocation);
		txtFlag = (EditText) findViewById(R.id.txtFlag);// 获取便签文本框
		btnflagSaveButton = (Button) findViewById(R.id.btnflagSave);// 获取保存按钮
		btnflagCancelButton = (Button) findViewById(R.id.btnflagCancel);// 获取取消按钮
		
		txtTime.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Date now = new Date(); 
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式


				String hehe = dateFormat.format( now ); 
				System.out.println(hehe); 

				Calendar c = Calendar.getInstance();//可以对每个时间域单独修改

				int year = c.get(Calendar.YEAR); 
				int month = c.get(Calendar.MONTH); 
				int date = c.get(Calendar.DATE); 
				int hour = c.get(Calendar.HOUR_OF_DAY); 
				int minute = c.get(Calendar.MINUTE); 
				int second = c.get(Calendar.SECOND); 
				txtTime.setText(year + "-" + (month+1) + "-" + date + " " +hour + ":" +minute + ":" + second);
			}
		});
		
		txtLocation.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(Accountflag.this, CopyOfMyLocation.class);
				startActivityForResult(intent, 1);
			}
		});
		btnflagSaveButton.setOnClickListener(new OnClickListener() // 为保存按钮设置监听事件
		{
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String strFlag = txtFlag.getText().toString().trim();// 获取便签文本框的值
				if (txtFlag.getText().toString().trim().equals("")) // 判断获取的值不为空
				{
					Toast.makeText(Accountflag.this, "内容不能为空！",Toast.LENGTH_SHORT).show();
				}
				else 
				{
					FlagDAO flagDAO = new FlagDAO(Accountflag.this);// 创建FlagDAO对象
					Tb_flag tb_flag = new Tb_flag(
							txtTime.getText().toString(),
							txtLocation.getText().toString(),
							 strFlag);// 创建Tb_flag对象
					flagDAO.add(tb_flag);// 添加便签信息
					// 弹出信息提示
					
					
					Toast.makeText(Accountflag.this, "备忘录数据添加成功！",Toast.LENGTH_SHORT).show();
				}
			}
		});

		btnflagCancelButton.setOnClickListener(new OnClickListener() {// 为取消按钮设置监听事件
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						txtFlag.setText("");// 清空便签文本框
					}
				});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		Bundle bundle;
		switch (resultCode) {
		case 1:
			bundle = data.getExtras();
			str_address = bundle.getString("address");
			txtLocation.setText(str_address);
			break;
		default:
			break;
		}
		
	}
}
