package com.example.bpa.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.bpa.R;
import com.example.bpa.db.mode.BPA_GOODS;
import com.example.bpa.helper.T;
import com.example.bpa.view.inteface.MyClickListener;

import java.util.List;

/**
 * 商品数据表格
 */
public class sp_adapter extends ArrayAdapter<BPA_GOODS> {
    /**
     * 内部点击事件
     */
    private MyClickListener mListener;
    private List<BPA_GOODS> datas;
    public sp_adapter(@NonNull Context context, int resource, @NonNull List<BPA_GOODS> objects, MyClickListener listener) {
        super(context, resource, objects);
        mListener = listener;
        datas=objects;
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        BPA_GOODS bpa_goods = (BPA_GOODS) getItem(position);//得到当前项选中item实例
        //为每一个子项加载设定的布局
        View view = LayoutInflater.from(getContext()).inflate(R.layout.sp_item, parent, false);
        //分别获取 image view 和 textview 的实例
        TextView name = view.findViewById(R.id.name);
        TextView sort = view.findViewById(R.id.sort);
        TextView status = view.findViewById(R.id.status);
        TextView time = view.findViewById(R.id.time);

        Button button = view.findViewById(R.id.button_item);
        Button button_up = view.findViewById(R.id.button_item_Up);
        Button button_down = view.findViewById(R.id.button_item_Down);
        Button button_update = view.findViewById(R.id.button_item_update);
        Button button_xf = view.findViewById(R.id.button_xf);
        Button button_qy = view.findViewById(R.id.button_qy);
        Button button_jy = view.findViewById(R.id.button_jy);

        // 设置要显示的图片和文字
        name.setText(bpa_goods.name);
        time.setText(bpa_goods.createTime);
        sort.setText(bpa_goods.sort+"");
        status.setText(bpa_goods.status==1?"启用":"禁用");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try
                {
                    mListener.clickListener(view,bpa_goods);
                } catch (Exception e)
                {
                    T.show(view.getContext(),"删除商品出错:"+e.getMessage());
                }
            }
        });
        button_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try
                {
                    mListener.clickListener(view,bpa_goods);
                } catch (Exception e)
                {
                    T.show(view.getContext(),"商品排序出错:"+e.getMessage());
                }
            }
        });
        button_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try
                {
                    mListener.clickListener(view,bpa_goods);
                } catch (Exception e)
                {
                    T.show(view.getContext(),"商品排序出错:"+e.getMessage());
                }
            }
        });
        button_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try
                {
                    mListener.clickListener(view,bpa_goods);
                } catch (Exception e)
                {
                    T.show(view.getContext(),"商品修改出错:"+e.getMessage());
                }
            }
        });
        button_xf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try
                {
                    mListener.clickListener(view,bpa_goods);
                } catch (Exception e)
                {
                    T.show(view.getContext(),"商品下发出错:"+e.getMessage());
                }
            }
        });
        button_qy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try
                {
                    mListener.clickListener(view,bpa_goods);
                } catch (Exception e)
                {
                    T.show(view.getContext(),"商品启用出错:"+e.getMessage());
                }
            }
        });
        button_jy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try
                {
                    mListener.clickListener(view,bpa_goods);
                } catch (Exception e)
                {
                    T.show(view.getContext(),"商品禁用出错:"+e.getMessage());
                }
            }
        });

        return view;
    }
}