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.SaleBean;
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 DealerOutHisListAdapter extends BaseAdapter {

	private List<SaleBean> beans;
	private SingleLabelBll labelBll;
	private BatchLabelBll batchLabelBll;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	private HandsetOperation option_type = HandsetOperation.OPERATION_DEALER_OUT_CONSTANT;
	private SheepDealerBll dealerBll;


	public DealerOutHisListAdapter(List<SaleBean> beans,SingleLabelBll labelBll,BatchLabelBll batchLabelBll,SheepDealerBll dealerBll) {
		this.beans = beans;
		this.labelBll = labelBll;
		this.dealerBll = dealerBll;
		this.batchLabelBll = batchLabelBll;
	}
	public void refreshData(List<SaleBean> 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) {
		// TODO Auto-generated method stub
		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);
			String tipsStr = "还没有" + option_type.getOptionName()+"数据，快去创建吧^_^";
			tv_no_data_tips.setText(tipsStr);
		} else {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.adapter_sale_his_list_item, parent, false);
				holder = new ViewHolder();
				holder.lin_sale_adapter_item_root = convertView.findViewById(R.id.lin_sale_adapter_item_root);
				holder.tv_sale_his_serial = (TextView) convertView.findViewById(R.id.tv_sale_his_serial);
				holder.tv_sale_his_describe = (TextView) convertView.findViewById(R.id.tv_sale_his_describe);
				holder.tv_sale_his_count = (TextView) convertView.findViewById(R.id.tv_sale_his_count);
				holder.tv_sale_his_status = (TextView) convertView.findViewById(R.id.tv_sale_his_status);
				holder.tv_sale_area = (TextView) convertView.findViewById(R.id.tv_sale_area);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv_sale_his_serial.setText(beans.size() - position + "");
			SaleBean b = beans.get(position);

			if (dealerBll != null) {
				DealerBean dealerBean = dealerBll.queryDealerById(b.getDealer_id());
				String dealerName = (dealerBean == null ? "无" :dealerBean.getName());
				holder.tv_sale_area.setText("经  销  商：" + dealerName);
			}
			
			
			
			int count = 0;
			if (b.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
				holder.tv_sale_his_count.setText("出库方式：单标出库");
				count = labelBll.queryLabelCountById(option_type.getOption(),b.getId());
				holder.tv_sale_his_describe.setText("标签数量：" + count+" / " + b.getPlan_num());
			} else if (b.getOption_mode() == Config.BATCH_OPTION_LABEL_TYPE) {
				holder.tv_sale_his_count.setText("出库方式：批量出库");
				count = batchLabelBll.queryBatchCountById(option_type.getOption(),b.getId());
				holder.tv_sale_his_describe.setText("标签数量：" + count+" / " + b.getPlan_num());
			}
			if (b.getStatus() == 0) {
				holder.lin_sale_adapter_item_root.setBackgroundColor(context.getResources().getColor(R.color.white));
				holder.tv_sale_his_status.setText("提交状态：未提交");
			} else {
				holder.lin_sale_adapter_item_root
						.setBackgroundColor(context.getResources().getColor(R.color.adapter_item_error_color));
				holder.tv_sale_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_sale_adapter_item_root;
		private TextView tv_sale_his_serial, tv_sale_his_describe, tv_sale_his_count, tv_sale_his_status, tv_sale_area;

	}

}
