package com.szu;

import java.util.ArrayList;
import java.util.HashMap;

import com.szu.accountsoft.dao.DBOpenHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class Outaccountinfo extends Activity {
	public static final String FLAG = "id";// 定义一个常量，用来作为请求码
	ListView lvinfo;// 创建ListView对象
	String strType = "";// 创建字符串，记录管理类型
	
	private DBOpenHelper helper;// 创建DBOpenHelper对象
	private SQLiteDatabase db;// 创建SQLiteDatabase对象
	
	ArrayList<HashMap<String, Object>> data;
	SimpleAdapter listItem;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.outaccountinfo);// 设置布局文件
		lvinfo = (ListView) findViewById(R.id.lvoutaccountinfo);// 获取布局文件中的ListView组件

		ShowInfo();// 调用自定义方法显示支出信息

		lvinfo.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent,
					View view, final int position, final long id) {
				// TODO Auto-generated method stub
				Dialog alertDialog = new AlertDialog.Builder(Outaccountinfo.this)
							.setTitle("请选择")
							.setMessage("你确定删除这条信息？")
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int i) {
								// TODO Auto-generated method stub
								
								int mListPos = position;
								HashMap<String, Object>map = data.get(mListPos);
								
								Long id = Long.parseLong(map.get("outid").toString());
								
								if(delete(id))
								{
									data.remove(mListPos);
									 listItem.notifyDataSetChanged(); 
									 onCreate(savedInstanceState);
								}
								
								
							}

							private boolean delete(Long id) {
								// TODO Auto-generated method stub
								String whereClause = "_id = ?";
								String[] whereArgs = new String[] {String.valueOf(id)};
								try {
									db.delete("tb_outaccount", whereClause, whereArgs);
									Toast.makeText(Outaccountinfo.this, "删除成功！", Toast.LENGTH_SHORT).show();
								} catch (Exception e) {
									// TODO: handle exception
									Toast.makeText(Outaccountinfo.this, "删除失败", Toast.LENGTH_LONG).show();
									return false;
								}
								return true;
							}
						})
							.setNegativeButton("退出", new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									dialog.dismiss();
								}
							})
							.show();
			
				
				return false;
			}
		});
		
		lvinfo.setOnItemClickListener(new OnItemClickListener()// 为ListView添加项单击事件
		{
			// 覆写onItemClick方法
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				int mListPos = position;
				HashMap<String, Object>map = data.get(mListPos);
				
				Intent intent = new Intent(Outaccountinfo.this, OutfoManage.class);
				
				intent.putExtra("id", map.get("outid").toString());
				startActivity(intent);
			}
		});
	}

	private void ShowInfo() {// 用来根据传入的管理类型，显示相应的信息
		helper = new DBOpenHelper(Outaccountinfo.this);
		db = helper.getWritableDatabase();
		
		String sql = "select * from tb_outaccount;";
		Cursor cursor = db.rawQuery(sql, null);
		
		data = new ArrayList<HashMap<String,Object>>();
		
		while(cursor.moveToNext())
		{
			HashMap<String, Object>map = new HashMap<String, Object>();
			map.put("outid", cursor.getString(cursor.getColumnIndex("_id")));
			map.put("outmoney", cursor.getString(cursor.getColumnIndex("money"))+"元");
			map.put("outamount", cursor.getString(cursor.getColumnIndex("amount")));
			map.put("outtime", cursor.getString(cursor.getColumnIndex("time")));
			map.put("outtype", cursor.getString(cursor.getColumnIndex("type")));
			map.put("address", cursor.getString(cursor.getColumnIndex("address")));
			map.put("outmark", cursor.getString(cursor.getColumnIndex("mark")));
			
			
			System.out.println(cursor.getString(cursor.getColumnIndex("type")));
			data.add(map);
		}
		cursor.close();
		
		  listItem = new SimpleAdapter(Outaccountinfo.this, 
				data,  //数据来源
				R.layout.outaccountinfo_listview, 
				new String[] {"outid", "outtype",  "outamount","outmoney", "outtime"},
				new int[] {R.id.outid, R.id.outtype,R.id.outamount,R.id.outmoney, R.id.outtime});
		lvinfo.setAdapter(listItem);
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();// 实现基类中的方法
		ShowInfo();// 显示收入信息
	}
}