package com.cwbuyer.main;

import android.app.Activity;
import android.content.Intent;
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.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.cwbuyer.adapter.CompanyAdapter;
import com.cwbuyer.format.EMode;
import com.cwbuyer.format.IDialog;
import com.cwbuyer.lib.DBCloud;
import com.cwbuyer.lib.DialogUtilis;
import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

// 廠商管理頁面
public class AccountList extends Activity {
	
	CompanyAdapter mAdapter = null;
	int mMode = EMode.EDIT;
	int mCountryId = 0;
	int of_line=Utilis.toInt(Utilis.getIni(this, "SYS", "IMPORT", 3));
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_layout);
		
		setTitle("廠商主檔");
		mMode = getIntent().getIntExtra("mode", EMode.EDIT);
		mCountryId = getIntent().getIntExtra("countryid", 0);
		mAdapter = new CompanyAdapter(this);
		ListView listview = (ListView)findViewById(R.id.listview1);
		listview.setAdapter(mAdapter);
		listview.setOnItemClickListener(new OnItemClickListener(){
			@SuppressWarnings("static-access")
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				if(mMode == EMode.GET){
					Intent intent = new Intent();
					intent.putExtra("fact_id", mAdapter.getFactNo(pos));
					intent.putExtra("fact_ph", mAdapter.getFactPh(pos));
					setResult(AccountList.this.RESULT_OK, intent);
					finish();
				}else{
					Utilis.runVibrate(AccountList.this);
					Intent intent = new Intent();
					intent.setClass(AccountList.this, AddCompany.class);
					String no = mAdapter.getFactNo(pos);
					intent.putExtra("factno", no);
					intent.putExtra("mode", EMode.EDIT);
					startActivity(intent);
				}
			}
		});
		listview.setOnItemLongClickListener(mLongClickListener);
		((Button)findViewById(R.id.btn_new)).setOnClickListener(new MainClick());
		((Button)findViewById(R.id.btn_exit)).setOnClickListener(new MainClick());
	}
	
	public void onResume(){
		super.onResume();
		if(mMode == EMode.GET){
			mAdapter.getData(this, mCountryId);
		}else{
			mAdapter.getData(this, 0);
		}
		mAdapter.notifyDataSetChanged();
		
		DBCloud.aa0_aa1_auto_qfact(this);
		
	}
	
	class MainClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			Utilis.runVibrate(AccountList.this);
			int id = v.getId();
			switch (id) {
				case R.id.btn_new:
				{
					// 新增廠商資料
					Intent intent = new Intent();
					intent.setClass(AccountList.this, AddCompany.class);
					intent.putExtra("mode", EMode.ADD);
					startActivity(intent);
					break;
				}
				case R.id.btn_exit:
				{
					finish();
					break;
				}
			}
		}
	}
	private LongClick mLongClickListener = new LongClick();
	class LongClick implements OnItemLongClickListener {
		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				final int position, long arg3) {
			Utilis.runVibrate(AccountList.this);
			DialogUtilis.showDialog(AccountList.this, "請選擇以下功能", -1,
					new String[] { "編輯", "刪除" }, new IDialog() {
						@Override
						public void onDialogFinish(int pos, String choice) {
							switch (pos) {
							case 0: // 編輯資料
								Intent intent = new Intent();
								intent.setClass(AccountList.this,
										AddCompany.class);
								String no = mAdapter.getFactNo(position);
								intent.putExtra("factno", no);
								intent.putExtra("mode", EMode.EDIT);
								startActivity(intent);
								break;
							case 1: // 刪除資料
								deleteData(position);
								break;
							}
						}
					});
			return false;
		}
	}
	
	private void deleteData(int pos){
		SQLiteDatabase db = Utilis.getDB(this);
		try{
			String factno = mAdapter.getFactNo(pos);
			if (of_line==0)			
				db.execSQL("delete from qfact where factno ='" +factno + "'");
			else{
				db.execSQL("update qfact set STATE='2' where factno ='" +factno + "'");
				DBCloud.aa1_aa0_qfact(AccountList.this);
			}
			Toast.makeText(AccountList.this, "刪除資料成功", Toast.LENGTH_SHORT).show();
			// 更新資料
			mAdapter.getData(AccountList.this, mCountryId);
			mAdapter.notifyDataSetChanged();
		}catch(Exception e){
			Toast.makeText(AccountList.this, "刪除資料失敗", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
		db.close();
	}
}
