package com.bbitcn.silk.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.bbitcn.silk.base.BaseRecyclerAdapter;
import com.bbitcn.silk.databinding.ItemInfoBinding;
import com.bbitcn.silk.databinding.ItemInfoContentBinding;
import com.bbitcn.silk.model.InfoContent;
import com.bbitcn.silk.model.net.CommonResponse;
import com.bbitcn.silk.model.net.QueryList;
import com.bbitcn.silk.ui.activity.ConvertActivity;
import com.bbitcn.silk.ui.activity.TareActivity;
import com.bbitcn.silk.ui.dialog.ConfirmDialog;
import com.bbitcn.silk.ui.dialog.TipsDialog;
import com.bbitcn.silk.utils.DialogUtil;
import com.bbitcn.silk.utils.log.MyLog;
import com.bbitcn.silk.utils.network.Url;
import com.bbitcn.silk.utils.network.XHttpManager;
import com.bbitcn.silk.utils.network.XHttpShorthand;
import com.blankj.utilcode.util.GsonUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

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

/**
 * @Description 信息项
 * @Author DuanKaiji
 * @CreateTime 2024年04月01日 09:30:28
 */
public class ItemInfoAdapter extends BaseRecyclerAdapter<QueryList.DataDTO, ItemInfoBinding> {
    /**
     * 使用类型
     * 0磅单查询 1扣皮定价 2茧别转换
     */
    int type;
    View.OnClickListener listener;
    Context mContext;

    public ItemInfoAdapter(Context context, List<QueryList.DataDTO> dataList, Class<ItemInfoBinding> bindingClass, int type) {
        super(dataList, bindingClass);
        this.type = type;
        this.mContext = context;
    }

    @Override
    protected void bindData(ItemInfoBinding binding, QueryList.DataDTO data) {
        if (type != 0) {
            if (type == 3) {
                //弃售/确认售
                binding.llExertFunc.setVisibility(View.VISIBLE);
                binding.tvCancel.setOnClickListener(v -> new ConfirmDialog(mContext, "弃售", "确定要弃售" + data.getCzCode() + "吗",
                        () -> requestNet(-1, data.getCzSysid())).show());
                binding.tvConfirm.setOnClickListener(v -> new ConfirmDialog(mContext, "确认售", "确定要确认售" + data.getCzCode() + "吗",
                        () -> requestNet(2, data.getCzSysid())).show());
            } else {
                Intent intent = new Intent();
                intent.putExtra("data", GsonUtils.toJson(data));
                binding.llExertFunc.setVisibility(View.GONE);
                if (type == 1) {
                    //扣皮定价
                    intent.setClass(mContext, TareActivity.class);
                } else if (type == 2) {
                    //茧别转换
                    intent.setClass(mContext, ConvertActivity.class);
                }
                listener = v -> binding.clRoot.getContext().startActivity(intent);
                binding.clRoot.setOnClickListener(listener);
            }
        }
        binding.tvState.setText(data.getStatus());
        binding.tvName.setText(data.getNhName());
        binding.tvId.setText(data.getCzCode());
        binding.recycleView.setLayoutManager(new LinearLayoutManager(mContext));
        List<InfoContent> infoContents = new ArrayList<>();
        for (
                QueryList.DataDTO.ItemsDTO itemsDTO : data.getItems()) {
            infoContents.add(new InfoContent(itemsDTO.getSgTypeName(), itemsDTO.getBaoshu(), itemsDTO.getMaozhong(), itemsDTO.getPrice()));
        }
        binding.recycleView.setAdapter(new ItemContentAdapter(infoContents, ItemInfoContentBinding.class, listener));
    }

    /**
     * @param type -1 弃售 2 确认销售
     * @param id   称重sysid
     */
    private void requestNet(int type, String id) {
        DialogUtil.showLoadingDialog(mContext, "正在提交中");
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.configOrCancel);
        params.addQueryStringParameter("sgBillstate", type);
        params.addQueryStringParameter("czsysid", id);
        x.http().put(params, new XHttpShorthand<CommonResponse>() {
            @Override
            public void success(CommonResponse model) {
                if (model.getResult()) {
                    MyLog.app("操作成功");
                    new TipsDialog(mContext, "提交成功").show();
                    // 删除这行数据
                    for (int i = 0; i < dataList.size(); i++) {
                        if (dataList.get(i).getCzSysid().equals(id)) {
                            dataList.remove(i);
                            notifyItemRemoved(i);
                            break;
                        }
                    }
                } else {
                    new TipsDialog(mContext, model.getMsg()).show();
                    MyLog.appError("查询失败");
                }
            }

            @Override
            public void error(Throwable e) {
                new TipsDialog(mContext, e.getMessage()).show();
            }

            @Override
            public void myFinish() {
                super.myFinish();
                DialogUtil.hideLoadingDialog();
            }
        });
    }


}
