package com.example.administrator.zhapp.adapter;

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

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

import java.util.List;

/**
 * 已接收列表适配器
 */
public class ReceiveListAdapter extends BaseAdapter {


    private Context context;
    private List<ReceiveListInfo> infos;
    private CallBack mCallBack;

    public ReceiveListAdapter(List<ReceiveListInfo> infos, Context context,CallBack mCallBack) {
        this.infos = infos;
        this.context = context;
        this.mCallBack = mCallBack;
    }

    public interface CallBack
    {
        void onChanged(CompoundButton buttonView, boolean isChecked);
    }

    @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(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate( R.layout.receive_list, viewGroup,false);
            holder.tv_project = (TextView) view.findViewById(R.id.tv_project);
            holder.tv_shelves = (TextView) view.findViewById(R.id.tv_shelves);
            holder.tv_count = (TextView) view.findViewById(R.id.tv_count);
            holder.ch = (CheckBox) view.findViewById(R.id.cb_isprint);
            holder.tv_describe = (TextView) view.findViewById(R.id.tv_describe);
            holder.tv_attr = (TextView) view.findViewById(R.id.tv_attr);
            view.setTag(holder);
            AutoUtils.autoSize(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tv_project.setText(infos.get(i).getProject());
        holder.tv_shelves.setText(infos.get(i).getShelves());
        holder.tv_count.setText(infos.get(i).getCount() + "");
        holder.tv_describe.setText(infos.get(i).getDescribe());
        holder.ch.setChecked(infos.get(i).isSfqx());
        holder.tv_attr.setText(infos.get(i).getAttr());

        holder.ch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCallBack.onChanged(buttonView,isChecked);
            }
        });
        holder.ch.setTag(i);

        return view;
    }


    static class ViewHolder {
        TextView tv_project;//库存项目
        TextView tv_shelves;//货架
        TextView tv_count;//接收数量
        TextView tv_describe;//描述
        CheckBox ch;
        TextView tv_attr;//专业属性
    }
}
