package com.sxyz.rfid.ui.out.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sxyz.rfid.R;
import com.sxyz.rfid.presenter.model.OutboundFlowingWaterModel;
import com.sxyz.rfid.utils.StringUtil;
import com.sxyz.rfid.utils.TimeUtils;

import java.util.List;

/**
 * 出库流水 适配器
 *
 * @author chunyuanwu
 * @version 1.0.0
 * @date 2025年12月27日 16:40
 */
public class OutBoundDetailsChildAdapter extends BaseQuickAdapter<OutboundFlowingWaterModel.RowsBean, BaseViewHolder> implements LoadMoreModule {

    public OutBoundDetailsChildAdapter(int layoutResId, List<OutboundFlowingWaterModel.RowsBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, OutboundFlowingWaterModel.RowsBean dataBean) {
        //RFID标签
        baseViewHolder.setText(R.id.tv_material_code, !StringUtils.isEmpty(dataBean.getRfidCode()) ? dataBean.getRfidCode() : "-");
        //批次号
        baseViewHolder.setText(R.id.tv_batch_code, !StringUtils.isEmpty(dataBean.getBatchNo()) ? dataBean.getBatchNo() : "-");

        String warehouseName = "-";
        if (dataBean.getWarehouse() != null && !StringUtils.isEmpty(dataBean.getWarehouse().getWarehouseName())) {
            warehouseName = dataBean.getWarehouse().getWarehouseName();
        }
        if (dataBean.getArea() != null && !StringUtils.isEmpty(dataBean.getArea().getAreaName())) {
            warehouseName += "/" + dataBean.getArea().getAreaName();
        }
        if (dataBean.getLocation() != null && !StringUtils.isEmpty(dataBean.getLocation().getLocationName())) {
            warehouseName += "/" + dataBean.getLocation().getLocationName();
        }

        //仓库/库区/货架
        baseViewHolder.setText(R.id.tv_warehouse, warehouseName);
        //数量
        baseViewHolder.setText(R.id.tv_quantity, !StringUtils.isEmpty(dataBean.getQuantity() + "") ? dataBean.getQuantity() + "" : "-");
        //单价
        baseViewHolder.setText(R.id.tv_in_price, !StringUtils.isEmpty(dataBean.getUnitCost() + "") ? "¥" + dataBean.getUnitCost() : "-");
        if (!StringUtils.isEmpty(dataBean.getOutboundTime())) {
            String outboundTime = TimeUtils.toChineseFormat(dataBean.getOutboundTime(), "yyyy-MM-dd");
            //出库时间
            baseViewHolder.setText(R.id.tv_outbound_time, outboundTime);
        } else {
            //出库时间
            baseViewHolder.setText(R.id.tv_outbound_time, "-");
        }
        if (!StringUtils.isEmpty(dataBean.getCancelTime())) {
            String cancelTime = TimeUtils.toChineseFormat(dataBean.getCancelTime(), "yyyy-MM-dd'T'HH:mm:ss");
            cancelTime = cancelTime.replace("T", " ");
            //撤销时间
            baseViewHolder.setText(R.id.tv_cancel_time, cancelTime);
        } else {
            //撤销时间
            baseViewHolder.setText(R.id.tv_cancel_time, "-");
        }
        //拣货状态
        TextView status = baseViewHolder.getView(R.id.tv_status);
        switch (dataBean.getIsCanceled()) {
            case "0":
                status.setText("未撤销");
//                status.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                status.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
                break;
            case "1":
                status.setText("已撤销");
//                status.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_red_bg));
                status.setTextColor(ContextCompat.getColor(getContext(), R.color.red));
                break;
            default:
                status.setText("-");
//                status.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.status_gray_bg));
                status.setTextColor(ContextCompat.getColor(getContext(), R.color.c_6666));
                break;
        }
        if (dataBean.getIsCanceled().equals("1")) {
            //已撤销  隐藏撤销按钮
            baseViewHolder.getView(R.id.btn_cancel).setVisibility(View.GONE);
        } else {
            //未撤销  显示撤销按钮
            baseViewHolder.getView(R.id.btn_cancel).setVisibility(View.VISIBLE);
        }
        //撤销按钮
        baseViewHolder.getView(R.id.btn_cancel).setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(baseViewHolder.getLayoutPosition(), dataBean);
            }
        });
        //点击RFID标签  复制
        baseViewHolder.getView(R.id.tv_material_code).setOnClickListener(v -> {
            StringUtil.copyToClipboard(getContext(), dataBean.getRfidCode());
        });
        //点击批次号  复制
        baseViewHolder.getView(R.id.tv_batch_code).setOnClickListener(v -> {
            StringUtil.copyToClipboard(getContext(), dataBean.getBatchNo());
        });
    }

    public interface OnItemClickListener {
        void onItemClick(int position, OutboundFlowingWaterModel.RowsBean item);
    }

    private OnItemClickListener mItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }
}