package com.sxyz.rfid.ui.out;

import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.StringUtils;
import com.sxyz.rfid.R;
import com.sxyz.rfid.base.BaseActivity;
import com.sxyz.rfid.databinding.ActivityOutboundDetailBinding;
import com.sxyz.rfid.databinding.ActivityOutboundDetailListBinding;
import com.sxyz.rfid.presenter.IOutBoundPresenter;
import com.sxyz.rfid.presenter.imp.OutBoundPresenterImpl;
import com.sxyz.rfid.presenter.model.OutBoundDetailModel;
import com.sxyz.rfid.presenter.model.OutboundFlowingWaterModel;
import com.sxyz.rfid.presenter.model.OutboundModel;
import com.sxyz.rfid.presenter.view.IOutBoundView;
import com.sxyz.rfid.ui.out.adapter.OutBoundDetailAdapter;
import com.sxyz.rfid.ui.out.adapter.OutBoundDetailListAdapter;
import com.sxyz.rfid.utils.StringUtil;
import com.sxyz.rfid.utils.ToastUtil;

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

/**
 * 主要功能:  出库明细列表
 *
 * @Description: 出库详情
 * @Project: rfid-app
 * @Package: com.sxyz.rfid.ui.out
 * @author: chunyuanwu
 * @date: 2025年12月29日 18:18
 * @version: 1.0.0
 */
public class OutboundDetailListActivity extends BaseActivity<ActivityOutboundDetailListBinding> implements IOutBoundView {
    private OutBoundDetailModel.RowsBean item;
    private OutBoundDetailListAdapter mAdapter;
    private List<OutboundFlowingWaterModel.RowsBean> list = new ArrayList<>();
    private IOutBoundPresenter mPresenter;

    @Override
    public void initParms(Bundle mBundle) {
        item = (OutBoundDetailModel.RowsBean) mBundle.getSerializable("item");
    }

    @Override
    protected ActivityOutboundDetailListBinding getViewBinding() {
        return ActivityOutboundDetailListBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        initToolBar(item.getMaterial().getMaterialName() + " - 批次出库详情");
        mPresenter = new OutBoundPresenterImpl(this);
        mPresenter.doOutBoundFlowingWaterList(this, item.getOrderId());
        mBinding.rvOutboundDetails.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new OutBoundDetailListAdapter(R.layout.item_outbound_detail_list, list);
        mBinding.rvOutboundDetails.setAdapter(mAdapter);
    }

    @Override
    protected void setListener() {
        //复制按钮点击事件
        mBinding.btnCopy.setOnClickListener(v -> {
            StringUtil.copyToClipboard(this, mBinding.outboundNumber.getText().toString());
        });

        //出库明细点击事件
        mAdapter.setOnItemClickListener((position, item) -> {
//            弹窗
            new AlertDialog.Builder(this).setTitle("确认撤销吗？").setPositiveButton("确认", (dialog, which) -> {
                //出库明细点击事件
                mPresenter.doOutBoundCancel(this, item.getRelationId());
            }).setNegativeButton("取消", null).show();
        });
    }

    @Override
    protected void initData() {
        //出库状态
        switch (item.getPickingStatus()) {
            case "0":
                mBinding.outboundStatus.setText("未出库");
                mBinding.outboundStatus.setBackground(ContextCompat.getDrawable(this, R.drawable.status_gray_bg));
                mBinding.outboundStatus.setTextColor(ContextCompat.getColor(this, R.color.c_6666));
                break;
            case "1":
                mBinding.outboundStatus.setText("拣货中");
                mBinding.outboundStatus.setBackground(ContextCompat.getDrawable(this, R.drawable.status_red_bg));
                mBinding.outboundStatus.setTextColor(ContextCompat.getColor(this, R.color.red));
                break;
            case "2":
                mBinding.outboundStatus.setText("已出库");
                mBinding.outboundStatus.setBackground(ContextCompat.getDrawable(this, R.drawable.status_green_bg));
                mBinding.outboundStatus.setTextColor(ContextCompat.getColor(this, R.color.green));
                break;
            default:
                mBinding.outboundStatus.setText("-");
                mBinding.outboundStatus.setBackground(ContextCompat.getDrawable(this, R.drawable.status_gray_bg));
                mBinding.outboundStatus.setTextColor(ContextCompat.getColor(this, R.color.c_6666));
                break;
        }
        if (item.getMaterial() != null) {
            //出库单号
            mBinding.outboundNumber.setText(item.getMaterial().getMaterialCode());
            //物资名称
            mBinding.tvMaterialName.setText(!StringUtils.isEmpty(item.getMaterial().getMaterialName()) ? item.getMaterial().getMaterialName() : "-");
            //客户名称
            mBinding.tvSpecificationModel.setText(!StringUtils.isEmpty(item.getMaterial().getSpecification()) ? item.getMaterial().getSpecification() : "-");
        } else {
            //出库单号
            mBinding.outboundNumber.setText("-");
            //物资名称
            mBinding.tvMaterialName.setText("-");
            //客户名称
            mBinding.tvSpecificationModel.setText("-");
        }
        //计划出库数量
        mBinding.tvPlanOutboundQuantity.setText(item.getPlanQuantity() + "");
        //实际出库数量
        mBinding.tvActualOutboundQuantity.setText(item.getActualQuantity() + "");
    }

    @Override
    public void outBoundDetailSuccess(OutBoundDetailModel model) {
    }

    @Override
    public void outBoundSuccess(OutboundModel model) {

    }

    @Override
    public void outBoundLoadMoreSuccess(OutboundModel model) {

    }

    @Override
    public void outBoundFlowingWaterSuccess(OutboundFlowingWaterModel model) {
        //出库流水
        list.clear();
        mAdapter.setList(model.getRows());
    }

    @Override
    public void outBoundCancelSuccess() {
        ToastUtil.showShort("撤销成功");
        mPresenter.doOutBoundFlowingWaterList(this, item.getOrderId());
    }

    @Override
    public void outBoundDetailCancelSuccess() {

    }
}
