package com.example.administrator.zhapp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.entity.KcpdInfo;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * 货架列表适配器
 */
public class KcpdAdapter extends BaseAdapter implements View.OnClickListener {

    private List<KcpdInfo.kcpd> infos;
    private Context context;
    private Callback mCallback;

    public interface Callback {
        void cbClick(View view);
    }

    public KcpdAdapter(List<KcpdInfo.kcpd> infos, Context context, Callback mCallback) {
        this.infos = infos;
        this.context = context;
        this.mCallback = mCallback;
    }

    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public Object getItem(int i) {
        return infos.get(i);
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.list_item1, viewGroup, false);
            holder.tv_ms = (TextView) view.findViewById(R.id.tv_ms);
            holder.tv_sl = (TextView) view.findViewById(R.id.tv_sl);
            holder.tv_wl = (TextView) view.findViewById(R.id.tv_wl);
            holder.tv_hg = (TextView) view.findViewById(R.id.tv_hg);
            holder.cb = (CheckBox) view.findViewById(R.id.cb_isprint);
            holder.tv_pdsl = (TextView) view.findViewById(R.id.tv_pdsl);
            holder.tv_pdzt = (TextView) view.findViewById(R.id.tv_pdzt);
            holder.tv_hjzy = (TextView) view.findViewById(R.id.tv_hjzy);
            holder.tv_wzbm = (TextView) view.findViewById(R.id.tv_wzbm);
            view.setTag(holder);
            AutoUtils.autoSize(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tv_ms.setText(infos.get(i).getDescription());
        holder.tv_wl.setText(infos.get(i).getBinnum());
        holder.tv_sl.setText(infos.get(i).getCurbal() + "");
        holder.tv_hg.setText(infos.get(i).getItemnum());
        holder.cb.setChecked(infos.get(i).isFlag());
        holder.tv_pdsl.setText(infos.get(i).getPdsl());
//        holder.tv_hjzy.setText(infos.get(i).getLastcost());
        holder.tv_hjzy.setText(infos.get(i).getStocktype());
        holder.tv_wzbm.setText(infos.get(i).getItemnum());

        if (infos.get(i).getIspd().equals("1")) {
            holder.tv_pdzt.setText("已盘点");
            int color = context.getResources().getColor(R.color.red);
            holder.tv_pdzt.setTextColor(color);
            holder.tv_ms.setTextColor(color);
            holder.tv_wl.setTextColor(color);
            holder.tv_sl.setTextColor(color);
            holder.tv_hg.setTextColor(color);
            holder.tv_pdsl.setTextColor(color);
//        holder.tv_hjzy.setText(infos.get(i).getLastcost());
            holder.tv_hjzy.setTextColor(color);
            holder.tv_wzbm.setTextColor(color);
        } else {


            holder.tv_pdzt.setText("未盘点");
            holder.tv_pdzt.setTextColor(Color.GRAY);
            holder.tv_ms.setTextColor(Color.GRAY);
            holder.tv_wl.setTextColor(Color.GRAY);
            holder.tv_sl.setTextColor(Color.GRAY);
            holder.tv_hg.setTextColor(Color.GRAY);
            holder.tv_pdsl.setTextColor(Color.GRAY);
//        holder.tv_hjzy.setText(infos.get(i).getLastcost());
            holder.tv_hjzy.setTextColor(Color.GRAY);
            holder.tv_wzbm.setTextColor(Color.GRAY);
        }

        holder.cb.setOnClickListener(this);
        holder.cb.setTag(i);

        return view;
    }

    @Override
    public void onClick(View view) {
        mCallback.cbClick(view);
    }


    static class ViewHolder {
        TextView tv_sl;
        TextView tv_ms;
        TextView tv_wl;
        TextView tv_hg;
        CheckBox cb;
        TextView tv_pdsl;
        TextView tv_pdzt;
        TextView tv_hjzy;
        TextView tv_wzbm;
    }
}
