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.TruckInvoiceBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.service.bll.UserLoginBll;
import cn.net.caoyao.cpzshandset.service.thread.ThreadReqTools;
import cn.net.cpzslibs.prot.handset.bean.res.ResponsibleUserInfoBean;

public class TruckInvoiceListAdapter extends BaseAdapter{
	
	private List<TruckInvoiceBean> aBeans;
	private int NO_DATE_VIEW = 0;
	private int NORMAL_VIEW = 1;
	private int viewStyle = 1;//0:显示订单编号  1:之前的形式，显示班组信息   2：显示负责人信息  3：web端创建订单信息
	private UserLoginBll loginBll;
	public TruckInvoiceListAdapter(List<TruckInvoiceBean> aBeans,int viewStyle) {
		this.aBeans = aBeans;
		this.viewStyle = viewStyle;
	}
	
	public void refreshData(List<TruckInvoiceBean> 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);
				holder.tv_invoice_option_mode = (TextView) convertView.findViewById(R.id.tv_invoice_option_mode);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			holder.tv_invoice_serial.setText((aBeans.size()- position)+"");
			TruckInvoiceBean b = aBeans.get(position);
			holder.tv_invoice_list_time.setText("创建时间：" + b.getTimestamp());
			if (viewStyle == 3) {
				holder.tv_invoice_option_mode.setVisibility(View.GONE);
				if(loginBll == null) loginBll = new UserLoginBll(context);
				holder.tv_invoice_list_invoiceno.setText("订  单  号：" + b.getOrderno());
				holder.tv_invoice_list_time.setText("单位名称：" + b.getConsignee());
			} if (viewStyle == 2) {
				holder.tv_invoice_option_mode.setVisibility(View.VISIBLE);
				if(loginBll == null) loginBll = new UserLoginBll(context);
				ResponsibleUserInfoBean infoBean = loginBll.queryUserInfoByAccount(b.getResponsiblePersonId());
				if (infoBean != null) {
					holder.tv_invoice_list_invoiceno.setText("负  责  人：" + infoBean.getUsername());
				} else {
					holder.tv_invoice_list_invoiceno.setText("负  责  人：无");
				}
				if (b.getOption_mode() == Config.BATCH_OPTION_LABEL_TYPE) {
					holder.tv_invoice_option_mode.setText("操作方式：托盘发货");
				} else {
					holder.tv_invoice_option_mode.setText("操作方式：单标发货");
				}
			} else if(viewStyle == 1){
					holder.tv_invoice_list_invoiceno.setText("发运班组：" + b.getGruopInfo());
			}else {
				holder.tv_invoice_list_invoiceno.setText("订  单  号：" + b.getOrderno());
			}
			
			
			holder.tv_invoice_list_trainno.setText("车  牌  号："+b.getTruckno());
			if (b.getStatus() == Config.SUBMIT_STATUS_NOT_SUBMIT) {
				holder.tv_invoice_list_status.setText("提交状态：未提交");
			} else {
				holder.tv_invoice_list_status.setText("提交状态：提交失败（错误码："+b.getError_code()+"）\n原        因：" +ThreadReqTools.getErrorMsg(b.getError_code()) );
			}
			
		}
		
		
		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;
		private TextView tv_invoice_option_mode;
	}

}
