package com.sxyz.rfid.ui.out;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
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.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.utils.StringUtil;

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 OutboundDetailActivity extends BaseActivity<ActivityOutboundDetailBinding> implements IOutBoundView {
    private OutboundModel.RowsBean item;
    private OutBoundDetailAdapter mAdapter;
    private List<OutBoundDetailModel.RowsBean> list = new ArrayList<>();
    private IOutBoundPresenter mPresenter;

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

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

    @Override
    protected void initView() {
        initToolBar("出库详情");
        mPresenter = new OutBoundPresenterImpl(this);
        mPresenter.doOutBoundDetailList(this, item.getOrderId());
        mBinding.rvOutboundDetails.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new OutBoundDetailAdapter(R.layout.item_outbound_detail, list);
        mBinding.rvOutboundDetails.setAdapter(mAdapter);
    }

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

        //出库明细点击事件
        mAdapter.setOnItemClickListener(new OutBoundDetailAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, OutBoundDetailModel.RowsBean item) {
                //出库明细点击事件
                Bundle bundle = new Bundle();
                bundle.putSerializable("item", item);
                startActivity(OutboundDetailListActivity.class, bundle);
            }

            @Override
            public void onDetailClick(int position, OutBoundDetailModel.RowsBean item) {
                //详情点击事件
            }
        });
    }

    @Override
    protected void initData() {
        //出库单号
        mBinding.outboundNumber.setText(item.getOrderNo());
        //出库类型
        switch (item.getOrderType()) {
            case "0":
                mBinding.tvOrderType.setText("计划出库");
                break;
            case "1":
                mBinding.tvOrderType.setText("紧急调拨");
                break;
            case "2":
                mBinding.tvOrderType.setText("报废出库");
                break;
            default:
                mBinding.tvOrderType.setText("-");
                break;
        }
        //出库状态
        switch (item.getOrderStatus()) {
            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;
        }
        //仓库名称
        mBinding.tvWarehouse.setText(!StringUtils.isEmpty(item.getWarehouse().getWarehouseName()) ? item.getWarehouse().getWarehouseName() : "-");
        //客户名称
        mBinding.tvCustomer.setText(!StringUtils.isEmpty(item.getCustomerUser().getCustomerName()) ? item.getCustomerUser().getCustomerName() : "-");
        //总数量
        mBinding.tvTotalQuantity.setText(item.getTotalQuantity() + "个");
        //总金额
        mBinding.tvTotalAmount.setText("¥" + item.getTotalAmount() + "元");
        //出库人
        mBinding.tvOutboundPerson.setText(!StringUtils.isEmpty(item.getApplicantUser().getNickName()) ? item.getApplicantUser().getNickName() : "-");
        //操作时间
        mBinding.tvOutboundTime.setText(!StringUtils.isEmpty(item.getApplyTime()) ? item.getApplyTime() : "-");
        //出库原因
        mBinding.tvOutboundReason.setText(!StringUtils.isEmpty(item.getRemark()) ? item.getRemark() : "-");
    }

    @Override
    public void outBoundDetailSuccess(OutBoundDetailModel model) {
        //出库明细
        list.clear();
//        model.getRows().forEach(item -> {
//            item.setExpanded(false);
//        });
        mAdapter.setList(model.getRows());
    }

    @Override
    public void outBoundSuccess(OutboundModel model) {

    }

    @Override
    public void outBoundLoadMoreSuccess(OutboundModel model) {

    }

    @Override
    public void outBoundFlowingWaterSuccess(OutboundFlowingWaterModel model) {

    }

    @Override
    public void outBoundCancelSuccess() {

    }

    @Override
    public void outBoundDetailCancelSuccess() {

    }
}
