package com.rehome.sgbaxj.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.TextView;

import com.rehome.sgbaxj.DBModel.Xjzjrw;
import com.rehome.sgbaxj.R;
import com.rehome.sgbaxj.contans.Contans;
import com.rehome.sgbaxj.utils.SPUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by ruihong on 2018/7/23.
 */

public class ZjrwxjAdapter extends BaseAdapter {

    private Context context;
    private List<Xjzjrw> list;
    private CallBack mCallBack;

    public ZjrwxjAdapter(Context context, List<Xjzjrw> list, CallBack mCallBack) {
        this.context = context;
        this.list = list;
        this.mCallBack = mCallBack;
    }

    public interface CallBack {
        void Click(View view);
    }

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

    @Override
    public Object getItem(int i) {
        return list.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.zjrwxj_item, viewGroup, false);
            holder.cb = view.findViewById(R.id.cb);
            holder.tv_zjd = view.findViewById(R.id.tv_zjd);
            holder.tv_sbs = view.findViewById(R.id.tv_sbs);
            holder.tv_zyfzr = view.findViewById(R.id.tv_zyfzr);
            holder.tv_zjsj = view.findViewById(R.id.tv_zjsj);


            view.setTag(holder);
            AutoUtils.autoSize(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.cb.setChecked(list.get(i).isChecked());
        holder.tv_zjd.setText(list.get(i).getWjbmc() + " - " + list.get(i).getTitle());
        holder.tv_sbs.setText(list.get(i).getSbnum());
        holder.tv_zyfzr.setText(list.get(i).getProusername());
        holder.tv_zjsj.setText(list.get(i).getZjtime());

        String YHID = (String) SPUtils.get(context, Contans.USERID, "");

        if (list.get(i).getProuserid().equals(YHID)) {//当前用户是主要负责人则可以下载
            holder.cb.setEnabled(true);
        } else {

            holder.cb.setChecked(false);
            holder.cb.setEnabled(false);

        }


        holder.cb.setTag(i);
        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCallBack.Click(view);
            }
        });
        return view;

    }


    static class ViewHolder {
        CheckBox cb;
        TextView tv_zjd;
        TextView tv_sbs;
        TextView tv_zyfzr;
        TextView tv_zjsj;
    }
}
