package com.szu;

import java.util.Calendar;

import com.szu.accountsoft.dao.DBOpenHelper;
import com.szu.accountsoft.dao.InaccountDAO;
import com.szu.accountsoft.dao.OutaccountDAO;
import com.szu.accountsoft.model.Tb_inaccount;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
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.Toast;

public class InfoManage extends Activity {
	
	DBOpenHelper helper;
	protected static final int DATE_DIALOG_ID = 0;// 创建日期对话框常量
	TextView tvtitle, textView;// 创建两个TextView对象
	EditText txtMoney, txtTime, txtHA, txtMark;// 创建4个EditText对象
	Spinner spType;// 创建Spinner对象
	Button btnEdit, btnDel;// 创建两个Button对象
	String[] strInfos;// 定义字符串数组
	String strid, strType;// 定义两个字符串变量，分别用来记录信息编号和管理类型

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

	OutaccountDAO outaccountDAO = new OutaccountDAO(InfoManage.this);// 创建OutaccountDAO对象
	InaccountDAO inaccountDAO = new InaccountDAO(InfoManage.this);// 创建InaccountDAO对象

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infomanage);// 设置布局文件
		
		findView();

		
		setTitle("请修改你的信息");
		Intent intent = getIntent();// 创建Intent对象
		final String id = intent.getStringExtra("id");
		
		displayData(id);
		
		btnDel.setEnabled(false);
		txtHA.setEnabled(false);
		txtMoney.setEnabled(false);
		txtTime.setEnabled(false);
		txtMark.setEnabled(false);
		

		txtTime.setOnClickListener(new OnClickListener() {// 为时间文本框设置单击监听事件
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showDialog(DATE_DIALOG_ID);// 显示日期选择对话框
			}
		});

		btnEdit.setOnClickListener(new OnClickListener() {// 为修改按钮设置监听事件
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				// 弹出信息提示
				
				
				txtHA.setEnabled(true);
				txtMark.setEnabled(true);
				txtMoney.setEnabled(true);
				txtTime.setEnabled(true);
				btnDel.setEnabled(true);
				btnEdit.setEnabled(false);
				Toast.makeText(InfoManage.this, "可进行修改！", Toast.LENGTH_SHORT)
						.show();
			}
		});

		btnDel.setOnClickListener(new OnClickListener() {// 为删除按钮设置监听事件
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				Tb_inaccount tb_inaccount = new Tb_inaccount();// 创建Tb_inaccount对象
				tb_inaccount.setid(Integer.parseInt(id));// 设置编号
				tb_inaccount.setMoney(Double.parseDouble(txtMoney.getText()
						.toString()));// 设置金额
				tb_inaccount.setTime(txtTime.getText().toString());// 设置时间
				tb_inaccount.setType(spType.getSelectedItem().toString());// 设置类别
				tb_inaccount.setHandler(txtHA.getText().toString());// 设置付款方
				tb_inaccount.setMark(txtMark.getText().toString());// 设置备注
				inaccountDAO.update(tb_inaccount);// 更新收入信息
				
				Toast.makeText(InfoManage.this, "修改成功！", Toast.LENGTH_SHORT).show();
				
				btnDel.setEnabled(false);
				btnEdit.setEnabled(true);
				txtHA.setEnabled(false);
				txtMoney.setEnabled(false);
				txtTime.setEnabled(false);
				txtMark.setEnabled(false);
			}
		});

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

	private void displayData(String id) {
		// TODO Auto-generated method stub
		helper = new DBOpenHelper(this);
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from tb_inaccount where _id = ? ;";
		Cursor cursor = db.rawQuery(sql, new String[]{id});
		cursor.moveToFirst();
		
		
		String money = cursor.getString(cursor.getColumnIndex("money"));
		String time = cursor.getString(cursor.getColumnIndex("time"));
		String type = cursor.getString(cursor.getColumnIndex("type"));
		String handle = cursor.getString(cursor.getColumnIndex("handler"));
		String mark = cursor.getString(cursor.getColumnIndex("mark"));
		
		
		txtMoney.setText(money);
		txtTime.setText(time);
		spType.setPrompt("你当前选择是："+type);
		txtHA.setText(handle);
		txtMark.setText(mark);
		
	}

	private void findView() {
		// TODO Auto-generated method stub
		tvtitle = (TextView) findViewById(R.id.inouttitle);// 获取标题标签对象
		textView = (TextView) findViewById(R.id.tvInOut);// 获取地点/付款方标签对象
		txtMoney = (EditText) findViewById(R.id.txtInOutMoney);// 获取金额文本框
		txtTime = (EditText) findViewById(R.id.txtInOutTime);// 获取时间文本框
		txtTime.setFocusableInTouchMode(false);
		spType = (Spinner) findViewById(R.id.spInOutType);// 获取类别下拉列表
		txtHA = (EditText) findViewById(R.id.txtInOut);// 获取地点/付款方文本框
		txtMark = (EditText) findViewById(R.id.txtInOutMark);// 获取备注文本框
		btnEdit = (Button) findViewById(R.id.btnInOutEdit);// 获取修改按钮
		btnDel = (Button) findViewById(R.id.btnInOutDelete);// 获取删除按钮
	}

	@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));
	}
}
