package com.bbitcn.silk.ui.activity;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.bbitcn.silk.base.BasePresenter;
import com.bbitcn.silk.model.net.CommonResponse;
import com.bbitcn.silk.model.net.ToTareSubmit;
import com.bbitcn.silk.utils.DialogUtil;
import com.bbitcn.silk.utils.network.Url;
import com.bbitcn.silk.utils.network.XHttpManager;
import com.bbitcn.silk.utils.network.XHttpShorthand;

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

/**
 * @Author DuanKaiji
 * @CreateTime 2024年3月28日 10:06:01
 */
public class TareActivityPresenter extends BasePresenter {
    MutableLiveData<Integer> notice = new MutableLiveData<>();

    public TareActivityPresenter(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<Integer> getNotice() {
        return notice;
    }

    public void tareSubmit(ToTareSubmit data) {
        showLoadingDialog("正在提交中");
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.tareSubmit);
        params.addParameter("czsysid", data.getCzsysid());
        params.addParameter("items", data.getItems());
        x.http().post(params, new XHttpShorthand<CommonResponse>() {
            @Override
            public void success(CommonResponse model) {
                if (model.getResult()) {
                    successTips("提交成功");
                    notice.setValue(0);
                } else {
                    errorTips(model.getMsg());
                }
            }

            @Override
            public void error(Throwable e) {
                errorTips(e.getMessage());
            }

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

    /**
     * 弃售
     */
    public void cancel(String id) {
        showLoadingDialog("正在提交弃售中");
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.configOrCancel);
        params.addQueryStringParameter("sgBillstate", -1);
        params.addQueryStringParameter("czsysid", id);
        x.http().put(params, new XHttpShorthand<CommonResponse>() {
            @Override
            public void success(CommonResponse model) {
                if (model.getResult()) {
                    successTips("提交成功");
                    notice.setValue(0);
                } else {
                    errorTips(model.getMsg());
                }
            }

            @Override
            public void error(Throwable e) {
                errorTips(e.getMessage());
            }

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