package com.szu;

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

import com.szu.accountsoft.dao.OutaccountDAO;
import com.szu.accountsoft.model.Tb_outaccount;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class AddOutaccount extends Activity {
	protected static final int DATE_DIALOG_ID = 0;// 创建日期对话框常量
	EditText txtMoney, txtTime, txtAddress, txtMark, txtNum;// 创建4个EditText对象
	Spinner spType;// 创建Spinner对象
	Button btnSaveButton;// 创建Button对象“保存”
	Button btnCancelButton;// 创建Button对象“取消”
	Button mylocation, time, sum_but, but_next;
	
	TextView txtsum;
	String address_str;
	
	double sum;

	private int mYear;// 年
	private int mMonth;// 月
	private int mDay;// 日

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addoutaccount);// 设置布局文件
		txtMoney = (EditText) findViewById(R.id.txtMoney);// 获取金额文本框
		txtTime = (EditText) findViewById(R.id.txtTime);// 获取时间文本框
		txtNum = (EditText) findViewById(R.id.editNum);
		sum_but = (Button) findViewById(R.id.sum_button1);
		but_next = (Button) findViewById(R.id.btnNext);
		
		txtsum = (TextView) findViewById(R.id.Txtsum);
		time = (Button)findViewById(R.id.butTime);
		txtTime.setFocusableInTouchMode(false);
		txtAddress = (EditText) findViewById(R.id.txtAddress);// 获取地点文本框
		txtMark = (EditText) findViewById(R.id.txtMark);// 获取备注文本框
		spType = (Spinner) findViewById(R.id.spType);// 获取类别下拉列表
		btnSaveButton = (Button) findViewById(R.id.btnSave);// 获取保存按钮
		btnCancelButton = (Button) findViewById(R.id.btnCancel);// 获取取消按钮
		mylocation = (Button) findViewById(R.id.mylocation_button1);
		
		

		
		but_next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
				Intent intent = new Intent(AddOutaccount.this, AddOutaccount.class);
				startActivity(intent);
				btnSaveButton.setEnabled(false);
			}
		});
		
		time.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 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);
				
			}
		});
		
		
		txtNum.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		
		sum_but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				if(txtMoney.getText().toString().trim().equals(""))
				{
					Toast.makeText(AddOutaccount.this, "金额输入值为空！", Toast.LENGTH_LONG).show();
				}
				else if(txtsum.getText().toString().trim().equals(""))
				{
					Toast.makeText(AddOutaccount.this, "数量输入值为空！", Toast.LENGTH_LONG).show();
				}
				else
				{
					Double money = Double.parseDouble(txtMoney.getText().toString().trim());
					Integer num = Integer.parseInt(txtNum.getText().toString().trim());
					sum = money*num;
					txtsum.setText("总共为："+sum+" 元");
				}
				
				
			}
		});
		
		txtTime.setOnClickListener(new OnClickListener() {// 为时间文本框设置单击监听事件

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showDialog(DATE_DIALOG_ID);// 显示日期选择对话框
			}
		});
		
		
		mylocation.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(AddOutaccount.this, MyLocation.class);
				startActivityForResult(intent, 1);
			}
		});
		
		
		btnSaveButton.setOnClickListener(new OnClickListener() // 为保存按钮设置监听事件
		{
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String strMoney = txtMoney.getText().toString().trim();// 获取金额文本框的值
				if (strMoney!=null&&strMoney.length()!=0) // 判断金额不为空
				{
					// 创建OutaccountDAO对象
					OutaccountDAO outaccountDAO = new OutaccountDAO(AddOutaccount.this);
					// 创建Tb_outaccount对象
					Tb_outaccount tb_outaccount = new Tb_outaccount(
							Double.parseDouble(txtMoney.getText().toString().trim()), 
							Integer.parseInt(txtNum.getText().toString().trim()),
							txtTime.getText().toString(), 
							spType.getSelectedItem().toString(),
							txtAddress.getText().toString(), 
							txtMark.getText().toString());
					
					
					outaccountDAO.add(tb_outaccount);// 添加支出信息
					// 弹出信息提示
					Toast.makeText(AddOutaccount.this, "支出添加成功！",Toast.LENGTH_SHORT).show();
				
					txtMoney.setText("");// 设置金额文本框为空
					txtTime.setText("");// 设置时间文本框为空
					txtAddress.setText("");// 设置地点文本框为空
					txtMark.setText("");// 设置备注文本框为空
					txtsum.setText("新增支出");
					spType.setSelection(0);// 设置类别下拉列表默认选择第一项
					
					txtMoney.setEnabled(false);
					txtNum.setEnabled(false);
					txtTime.setEnabled(false);
					txtAddress.setEnabled(false);
					txtMark.setEnabled(false);
					btnSaveButton.setEnabled(false);
					but_next.setEnabled(true);
				
				} 
				else 
				{
					Toast.makeText(AddOutaccount.this, "请输入支出金额！",Toast.LENGTH_SHORT).show();
				}
			}
		});

		btnCancelButton.setOnClickListener(new OnClickListener()// 为取消按钮设置监听事件
		{
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						txtMoney.setText("");// 设置金额文本框为空
						txtTime.setText("");// 设置时间文本框为空
						txtAddress.setText("");// 设置地点文本框为空
						txtMark.setText("");// 设置备注文本框为空
//						txtNum.setText("1");
						txtsum.setText("新增支出");
						spType.setSelection(0);// 设置类别下拉列表默认选择第一项
					}
				});

		final Calendar c = Calendar.getInstance();// 获取当前系统日期
		mYear = c.get(Calendar.YEAR);// 获取年份
		mMonth = c.get(Calendar.MONTH);// 获取月份
		mDay = c.get(Calendar.DAY_OF_MONTH);// 获取天数

		updateDisplay();// 显示当前系统时间
	}

	@Override
	protected Dialog onCreateDialog(int id)// 重写onCreateDialog方法
	{
		switch (id) {
		case DATE_DIALOG_ID:// 弹出日期选择对话框
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;// 为年份赋值
			mMonth = monthOfYear;// 为月份赋值
			mDay = dayOfMonth;// 为天赋值
			updateDisplay();// 显示设置的日期
		}
	};

	private void updateDisplay() {
		// 显示设置的时间
		txtTime.setText(new StringBuilder().append(mYear).append("-")
				.append(mMonth + 1).append("-").append(mDay));
	}
	
	@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();
			address_str = bundle.getString("address");
			txtAddress.setText(address_str);
			Toast.makeText(AddOutaccount.this, address_str, Toast.LENGTH_LONG).show();
			break;
		default:
			break;
		}
		
	}
}
