package com.stm.asset.adapter;

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

import com.stm.asset.R;
import com.stm.asset.model.InventoryInfo;
import com.stm.asset.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 类名称：TaskAdapter
 * 类描述：盘点任务适配器
 * 创建人：story
 * 创建时间：2021-12-07 15:27
 */
public class TaskAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<InventoryInfo> dataList;

    public TaskAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return dataList == null ? 0 : dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.view_check_task_item, null);
            viewHolder.nameLayout = convertView.findViewById(R.id.task_item_name_layout);
            viewHolder.nameTxt = convertView.findViewById(R.id.task_item_name_txt);
            viewHolder.numberTxt = convertView.findViewById(R.id.task_item_number_txt);
//            viewHolder.userTxt = convertView.findViewById(R.id.task_item_user_txt);
//            viewHolder.deptTxt = convertView.findViewById(R.id.task_item_dept_txt);
            viewHolder.statusTxt = convertView.findViewById(R.id.task_item_status_txt);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        InventoryInfo taskInfo = dataList.get(position);
        viewHolder.nameTxt.setText(taskInfo.getName());
        viewHolder.numberTxt.setText(taskInfo.getNumber());
        if (StringUtils.isEmptyOrNull(taskInfo.getName())) {
            viewHolder.nameLayout.setVisibility(View.GONE);
        } else {
            viewHolder.nameLayout.setVisibility(View.VISIBLE);
        }
//        viewHolder.userTxt.setText(taskInfo.getUserName());
//        viewHolder.deptTxt.setText(taskInfo.getUserDept());
        String statusName = "";
        if ("0".equals(taskInfo.getItem01())) {
            statusName = "未盘点";
        } else if ("1".equals(taskInfo.getItem01())) {
            statusName = "相符";
        } else if ("2".equals(taskInfo.getItem01())) {
            statusName = "盘亏";
        } else if ("3".equals(taskInfo.getItem01())) {
            statusName = "盘盈";
        }
        viewHolder.statusTxt.setText(statusName);
        return convertView;
    }

    public void setData(List<InventoryInfo> data) {
        if (dataList == null) {
            dataList = new ArrayList<>();
        }
        dataList.clear();
        if (data != null && data.size() > 0) {
            dataList.addAll(data);
        }
        notifyDataSetChanged();
    }

    private class ViewHolder {
//        private TextView nameTxt, numberTxt, userTxt, deptTxt, statusTxt;
        private TextView nameTxt, numberTxt, statusTxt;
        private LinearLayout nameLayout;
    }

}
