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 com.szu.accountsoft.model.Tb_outaccount;

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 OutfoManage extends Activity {
	DBOpenHelper helper;
	protected static final int DATE_DIALOG_ID = 0;// 创建日期对话框常量
	TextView tvtitle, textView;// 创建两个TextView对象
	EditText txtMoney, txtTime, txtAddress, txtMark, txtType, txtAmount;// 创建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(OutfoManage.this);// 创建OutaccountDAO对象
	InaccountDAO inaccountDAO = new InaccountDAO(OutfoManage.this);// 创建InaccountDAO对象
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.outfomanage);
		
		findView();

		
		setTitle("请修改你的信息");
		Intent intent = getIntent();// 创建Intent对象
		final String id = intent.getStringExtra("id");
		
		displayData(id);
		
		btnDel.setEnabled(false);
		txtAddress.setEnabled(false);
		txtMoney.setEnabled(false);
		txtTime.setEnabled(false);
		txtMark.setEnabled(false);
		txtAmount.setEnabled(false);
		txtType.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
				
				// 弹出信息提示
				
				
				txtAddress.setEnabled(true);
				txtMark.setEnabled(true);
				txtMoney.setEnabled(true);
				txtType.setEnabled(true);
				txtTime.setEnabled(true);
				btnDel.setEnabled(true);
				btnEdit.setEnabled(false);
				txtAmount.setEnabled(true);
				Toast.makeText(OutfoManage.this, "可进行修改！", Toast.LENGTH_SHORT).show();
			}
		});
		
		btnDel.setOnClickListener(new OnClickListener() {// 为删除按钮设置监听事件
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				Tb_outaccount tb_outaccount = new Tb_outaccount();// 创建Tb_inaccount对象
				
				tb_outaccount.setid(Integer.parseInt(id));// 设置编号
				tb_outaccount.setMoney((Double.parseDouble(txtMoney.getText().toString())));// 设置金额
				tb_outaccount.setAmount(Integer.parseInt(txtAmount.getText().toString().trim()));
				tb_outaccount.setTime(txtTime.getText().toString().trim());// 设置时间
				tb_outaccount.setType(txtType.getText().toString().trim());// 设置类别
				tb_outaccount.setAddress(txtAddress.getText().toString().trim());
				tb_outaccount.setMark(txtMark.getText().toString().trim());// 设置备注
				
				
				outaccountDAO.update(tb_outaccount);// 更新收入信息
				
				Toast.makeText(OutfoManage.this, "修改成功！", Toast.LENGTH_SHORT).show();
				
				btnDel.setEnabled(false);
				btnEdit.setEnabled(true);
				txtAddress.setEnabled(false);
				txtMoney.setEnabled(false);
				txtTime.setEnabled(false);
				txtMark.setEnabled(false);
				txtAmount.setEnabled(false);
				txtType.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_outaccount where _id = ? ;";
		Cursor cursor = db.rawQuery(sql, new String[]{id});
		cursor.moveToFirst();
		
		
		String money = cursor.getString(cursor.getColumnIndex("money"));
		String amount = cursor.getString(cursor.getColumnIndex("amount"));
		String time = cursor.getString(cursor.getColumnIndex("time"));
		String type = cursor.getString(cursor.getColumnIndex("type"));
		
		String address = cursor.getString(cursor.getColumnIndex("address"));
		String mark = cursor.getString(cursor.getColumnIndex("mark"));
		
		
		txtMoney.setText(money);
		txtAmount.setText(amount);
		txtTime.setText(time);
		txtType.setText(type);
		txtAddress.setText(address);
		txtMark.setText(mark);
	}
	private void findView() {
		// TODO Auto-generated method stub
		
		txtMoney = (EditText) findViewById(R.id.txtMoney);
		txtTime = (EditText) findViewById(R.id.txtTime);
		txtTime.setFocusableInTouchMode(false);
		txtAddress = (EditText) findViewById(R.id.txtAddress);
		txtMark = (EditText) findViewById(R.id.txtMark);
		txtType = (EditText) findViewById(R.id.spType);
		txtAmount = (EditText) findViewById(R.id.editNum);
		
		btnDel = (Button) findViewById(R.id.btnCancel);
		btnEdit = (Button) findViewById(R.id.btnSave);
		
	}
	
	@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));
	}

}
