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.ShipmentProductBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.service.bll.InvoiceBll;
import cn.net.caoyao.cpzshandset.service.thread.ThreadReqTools;

public class ShipmentProductListAdapter extends BaseAdapter{
	
	private List<ShipmentProductBean> aBeans;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	private InvoiceBll invoiceBll;
	
	public ShipmentProductListAdapter(List<ShipmentProductBean> aBeans,InvoiceBll invoiceBll) {
		this.aBeans = aBeans;
		this.invoiceBll = invoiceBll;
	}
	public void refreshData(List<ShipmentProductBean> aBeans){
		this.aBeans = aBeans;
		notifyDataSetChanged();
	}

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

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

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public int getItemViewType(int position) {
		return aBeans.size() == 0?NO_DATE_VIEW : NORMAL_VIEW;
	}
	
	

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@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 {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.adapter_train_invoice_item, parent, false);
				holder = new ViewHolder();
				holder.tv_invoice_serial = (TextView) convertView.findViewById(R.id.tv_invoice_serial);
				holder.tv_invoice_list_invoiceno = (TextView) convertView.findViewById(R.id.tv_invoice_list_invoiceno);
				holder.tv_invoice_list_time = (TextView) convertView.findViewById(R.id.tv_invoice_list_time);
				holder.tv_invoice_list_status = (TextView) convertView.findViewById(R.id.tv_invoice_list_status);
				holder.tv_invoice_list_trainno = (TextView) convertView.findViewById(R.id.tv_invoice_list_trainno);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
//			holder.tv_invoice_serial.setText((aBeans.size()- position)+"");
			holder.tv_invoice_serial.setText((position+1)+"");
			ShipmentProductBean b = aBeans.get(position);
			holder.tv_invoice_list_invoiceno.setText("产品名称：" + b.getHuowu_name());
			holder.tv_invoice_list_trainno.setText("预计件数：" + b.getItem_num());
			int label_count = invoiceBll.queryLabelCountByShipmentProductId(b.getDriver_id(), b.getId(), 4);
			holder.tv_invoice_list_time.setText("扫码总数：" + label_count);
//			holder.tv_invoice_list_time.setVisibility(View.GONE);
			if (b.getNative_status() == Config.SUBMIT_STATUS_NOT_SUBMIT) {
				holder.tv_invoice_list_status.setText("提交状态：未提交");
				convertView.setBackgroundColor(context.getResources().getColor(R.color.white));
			} else if (b.getNative_status() == Config.SUBMIT_STATUS_SUBMIT_SUCCESS) {
				holder.tv_invoice_list_status.setText("提交状态：已发货");
				convertView.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_submit_success_color));
			} else {
				holder.tv_invoice_list_status.setText("提交状态：提交失败（错误码："+b.getError_code()+"）\n原        因：" +ThreadReqTools.getErrorMsg(b.getError_code()) );
				convertView.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
			}
		}
		
		
		return convertView;
	}
	
	private class ViewHolder{
		private TextView tv_invoice_serial;
		private TextView tv_invoice_list_invoiceno;
		private TextView tv_invoice_list_time;
		private TextView tv_invoice_list_status;
		private TextView tv_invoice_list_trainno;
	}

}
