package cn.net.caoyao.cpzshandset.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.City;
import cn.net.caoyao.cpzshandset.bean.WarehouseBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.bll.CityBll;
import cn.net.caoyao.cpzshandset.service.bll.SingleLabelBll;
import cn.net.caoyao.cpzshandset.service.bll.UserLoginBll;
import cn.net.caoyao.cpzshandset.service.thread.ThreadReqTools;
import cn.net.caoyao.cpzshandset.service.tools.SaleAddressUtil;
import cn.net.cpzslibs.prot.handset.bean.res.ResponsibleUserInfoBean;

public class WarehouseHisListAdapter extends BaseAdapter {
	
	private List<WarehouseBean> beans;
	private SingleLabelBll labelBll;
	private CityBll cityBll;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	private int option_type;
	private UserLoginBll loginBll;
	private int out_warehouse = HandsetOperation.OPERATION_OUT_WAREHOUSE_CONSTANT.getOption();

	public WarehouseHisListAdapter(List<WarehouseBean> beans, SingleLabelBll labelBll,int option_type) {
		this.beans = beans;
		this.labelBll = labelBll;
		this.option_type = option_type;
	}
	
	public WarehouseHisListAdapter(List<WarehouseBean> beans, SingleLabelBll labelBll,int option_type,UserLoginBll loginBll) {
		this.beans = beans;
		this.labelBll = labelBll;
		this.option_type = option_type;
//		this.dealerBll = dealerBll;
		this.loginBll = loginBll;
	}

	public void refreshData(List<WarehouseBean> beans) {
		this.beans = beans;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return beans.size() == 0?1:beans.size();
	}

	@Override
	public Object getItem(int position) {
		return beans.size() == 0?position:beans.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		Context context = parent.getContext();
		if (getItemViewType(position) == NO_DATE_VIEW) {
			// 显示无数据界面
			convertView = LayoutInflater.from(context).inflate(R.layout.adapter_no_data_view, parent, false);
			TextView tv_no_data_tips = (TextView) convertView.findViewById(R.id.tv_no_data_tips);
			tv_no_data_tips.setText("目前还没有数据，快去创建吧^_^");
		} else {
//			LogUtil.e(TConfig.Echo, "convertView:" + convertView);
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.adapter_warehouse_his_list_item, parent,
						false);
				holder = new ViewHolder();
				holder.lin_warehouse_adapter_item_root = convertView.findViewById(R.id.lin_warehouse_adapter_item_root);
				holder.tv_warehouse_his_serial = (TextView) convertView.findViewById(R.id.tv_warehouse_his_serial);
				holder.tv_warehouse_his_describe = (TextView) convertView.findViewById(R.id.tv_warehouse_his_describe);
				holder.tv_warehouse_his_count = (TextView) convertView.findViewById(R.id.tv_warehouse_his_count);
				holder.tv_warehouse_his_status = (TextView) convertView.findViewById(R.id.tv_warehouse_his_status);
				holder.tv_warehouse_his_sale_area = (TextView) convertView.findViewById(R.id.tv_warehouse_his_sale_area);
				convertView.setTag(holder);
//				LogUtil.d(TConfig.Echo,"创建view"  + position);
			} else {
//				LogUtil.i(TConfig.Echo,"复用view"  + position);
				holder = (ViewHolder) convertView.getTag();
			}
			
			holder.tv_warehouse_his_serial.setText(beans.size() - position + "");
			WarehouseBean b = beans.get(position);
			
			if (option_type == out_warehouse) {//判断是否为出库
				holder.tv_warehouse_his_sale_area.setVisibility(View.VISIBLE);
//				if(cityBll == null) cityBll = new CityBll(context);
//				City mCity = cityBll.queryCityByCode(b.getSale_area_code());
//				
//				String saleAreaStr = (mCity==null?"全国":SaleAddressUtil.getAddressStrByCity(mCity));
//				
//				holder.tv_warehouse_his_sale_area.setText("销售区域："+saleAreaStr);
				
				if (cityBll != null) {
					City mCity = cityBll.queryCityByCode(b.getSale_area_code());
					String salesArea = (mCity == null ? "全国": SaleAddressUtil.getAddressStrByCity(mCity));
					holder.tv_warehouse_his_sale_area.setText("销售区域：" + salesArea);
				}
				
//				if (dealerBll != null) {
//					DealerInfoBean dealerBean = dealerBll.queryDealerById(b.getDealer_id());
//					String dealerName = (dealerBean == null ? "无" :dealerBean.getName());
//					holder.tv_warehouse_his_sale_area.setText("经  销  商：" + dealerName);
//				}
				if (loginBll != null) {
					ResponsibleUserInfoBean uBean = loginBll.queryUserInfoByAccount(b.getTo_sale_user_account());
					String username = (uBean== null ? "无" : uBean.getUsername());
					holder.tv_warehouse_his_sale_area.setText("销售人员：" + username);
				}
			} else {
				holder.tv_warehouse_his_sale_area.setVisibility(View.GONE);
			}
			
			String describe = StringUtils.isEmpty(b.getDescribe()) ? "无" : b.getDescribe();
			holder.tv_warehouse_his_describe.setText("描        述：" + describe);
			int count = labelBll.queryLabelCountById(b.getOption_type(), b.getId());// 该数据在数据库中去统计
			holder.tv_warehouse_his_count.setText("数        量：" + count);
			if (b.getPlan_num() > 0) {
				holder.tv_warehouse_his_count.setText("数        量：" + count+" / " + b.getPlan_num());
			}
			if (b.getStatus() == 0) {
				holder.lin_warehouse_adapter_item_root
						.setBackgroundColor(context.getResources().getColor(R.color.white));
				holder.tv_warehouse_his_status.setText("状        态：未提交");
			} else {
				holder.lin_warehouse_adapter_item_root
						.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
				holder.tv_warehouse_his_status.setText("状        态：提交失败(错误码:" + b.getError_code() + ")\n原        因：" + ThreadReqTools.getErrorMsg(b.getError_code()));
			}
		}

		return convertView;
	}
	
	@Override
	public int getItemViewType(int position) {
		return beans.size() == 0?NO_DATE_VIEW : NORMAL_VIEW;
	}
	
	@Override
	public int getViewTypeCount() {
		return 2;
	}

	private class ViewHolder {
		private View lin_warehouse_adapter_item_root;
		private TextView tv_warehouse_his_serial, tv_warehouse_his_describe, tv_warehouse_his_count,
				tv_warehouse_his_status,tv_warehouse_his_sale_area;

	}

}
