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.ExchangeCargoBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.service.bll.BatchLabelBll;
import cn.net.caoyao.cpzshandset.service.bll.SheepDealerBll;
import cn.net.caoyao.cpzshandset.service.bll.SingleLabelBll;
import cn.net.caoyao.cpzshandset.service.thread.ThreadReqTools;
import cn.net.cpzslibs.prot.handset.bean.res.DealerBean;

public class ExCargoHisListAdapter extends BaseAdapter{
	private List<ExchangeCargoBean> aBeans;
	private SheepDealerBll dealerBll;
	private SingleLabelBll singleLabelBll;
	private BatchLabelBll batchLabelBll;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	public ExCargoHisListAdapter(List<ExchangeCargoBean> aBeans,SheepDealerBll dealerBll,SingleLabelBll singleLabelBll,BatchLabelBll batchLabelBll){
		this.aBeans = aBeans;
		this.dealerBll = dealerBll;
		this.singleLabelBll = singleLabelBll;
		this.batchLabelBll = batchLabelBll;
	}
	
	public void refreshExCargoHisData(List<ExchangeCargoBean> 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 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_activate_his_list_item, parent, false);
				holder = new ViewHolder();
				holder.view_adapter_activate_his_root = convertView.findViewById(R.id.view_adapter_activate_his_root);
				holder.tv_activate_his_serial = (TextView) convertView.findViewById(R.id.tv_activate_his_serial);
				holder.tv_activate_his_p_number = (TextView) convertView.findViewById(R.id.tv_activate_activate_type);
				holder.tv_activate_his_p_name = (TextView) convertView.findViewById(R.id.tv_activate_his_p_name);
				holder.tv_activate_his_p_date = (TextView) convertView.findViewById(R.id.tv_activate_his_p_date);
				holder.tv_activate_his_label_count = (TextView) convertView.findViewById(R.id.tv_activate_his_label_count);
				holder.tv_activate_his_upload_status = (TextView) convertView.findViewById(R.id.tv_activate_his_upload_status);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			holder.tv_activate_his_serial.setText((aBeans.size()- position)+"");
//			ActivateBean aBean = aBeans.get(aBeans.size()- position-1);
			ExchangeCargoBean aBean = aBeans.get(position);
			//0：未提交    1：提交失败
			if (aBean.getStatus() == 0) {
				holder.view_adapter_activate_his_root.setBackgroundColor(context.getResources().getColor(R.color.white));
				holder.tv_activate_his_upload_status.setText("状态：未提交");
			} else {
				holder.view_adapter_activate_his_root.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
				holder.tv_activate_his_upload_status.setText("状态：提交失败(错误码："+aBean.getError_code()+")" + "\n原因：" + ThreadReqTools.getErrorMsg(aBean.getError_code()));
			}
			DealerBean fromDealer = dealerBll.queryDealerById(aBean.getFrom_dealer_id());
			holder.tv_activate_his_p_name.setText("调货来源：" + fromDealer.getName());;
			DealerBean toDealer = dealerBll.queryDealerById(aBean.getTo_dealer_id());
			holder.tv_activate_his_p_date.setText("调到商家：" + toDealer.getName());
			int count = 0;
			if (aBean.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
//				holder.tv_activate_his_p_number.setText("扫码方式：单标");
				count = singleLabelBll.queryLabelCountById(HandsetOperation.OPERATION_EXCHANGE_CARGO_CONSTANT.getOption(), aBean.getId());
//				LogUtil.d(TConfig.Echo, aBean.getId() +" 单标激活数量：" + count);
			} else {
//				holder.tv_activate_his_p_number.setText("扫码方式：批量");
				count = batchLabelBll.queryBatchCountById(HandsetOperation.OPERATION_EXCHANGE_CARGO_CONSTANT.getOption(), aBean.getId());
			}
			holder.tv_activate_his_p_number.setText("调货数量：" + aBean.getExchange_count());
			holder.tv_activate_his_label_count.setText("实际数量：" + count);
		}
		
		
		return convertView;
	}
	
	@Override
	public int getItemViewType(int position) {
		return aBeans.size() == 0?NO_DATE_VIEW : NORMAL_VIEW;
	}
	
	

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



	private class ViewHolder{
		private View view_adapter_activate_his_root;
		private TextView tv_activate_his_serial,tv_activate_his_p_name,tv_activate_his_p_number,tv_activate_his_p_date,tv_activate_his_label_count,tv_activate_his_upload_status;
	}
	

}
