package com.szu;

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

import com.szu.accountsoft.dao.DBOpenHelper;
import com.szu.accountsoft.dao.FlagDAO;
import com.szu.accountsoft.dao.InaccountDAO;
import com.szu.accountsoft.dao.OutaccountDAO;
import com.szu.accountsoft.model.Tb_flag;
import com.szu.accountsoft.model.Tb_inaccount;
import com.szu.accountsoft.model.Tb_outaccount;
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.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
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 Showinfo extends Activity {
	public static final String FLAG = "id";// 定义一个常量，用来作为请求码
	Button btnoutinfo, btnininfo, btnflaginfo;// 创建3个Button对象
	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.showinfo);// 设置布局文件

		lvinfo = (ListView) findViewById(R.id.lvinfo);// 获取布局文件中的ListView组件

		ShowInfo();// 默认显示支出信息

	

		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(Showinfo.this, FlagManage.class);
				
				intent.putExtra("id", map.get("id").toString());
				startActivity(intent);
			}
		});
		
		
		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(Showinfo.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("id").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_flag", whereClause, whereArgs);
									Toast.makeText(Showinfo.this, "删除成功！", Toast.LENGTH_SHORT).show();
								} catch (Exception e) {
									// TODO: handle exception
									Toast.makeText(Showinfo.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;
			}
		});
	}

	private void ShowInfo() // 用来根据传入的管理类型，显示相应的信息
	{
		helper = new DBOpenHelper(Showinfo.this);
		db = helper.getWritableDatabase();
		
		String sql = "select * from tb_flag;";
		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("id", cursor.getString(cursor.getColumnIndex("_id")));
			map.put("time", cursor.getString(cursor.getColumnIndex("time")));
			map.put("address", cursor.getString(cursor.getColumnIndex("address")));
			
			data.add(map);
		}
		cursor.close();
		
		  listItem = new SimpleAdapter(Showinfo.this, 
				data,  //数据来源
				R.layout.showinfo, 
				new String[] {"id", "time",  "address"},
				new int[] {R.id.id, R.id.time,R.id.address});
		lvinfo.setAdapter(listItem);
	}

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