package com.bbitcn.silk.ui.fragment;


import android.app.Application;

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

import com.bbitcn.silk.base.BasePresenter;
import com.bbitcn.silk.model.net.QueryList;
import com.bbitcn.silk.utils.global.Global;
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 org.xutils.http.RequestParams;
import org.xutils.x;

public class QueryFragmentPresenter extends BasePresenter {
    private MutableLiveData<QueryList> setListLiveData = new MutableLiveData<>();
    private MutableLiveData<QueryList> addListLiveData = new MutableLiveData<>();

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

    public MutableLiveData<QueryList> getAddListLiveData() {
        return addListLiveData;
    }

    public MutableLiveData<QueryList> getSetListLiveData() {
        return setListLiveData;
    }

    public void query(int type, String startDate, String endDate, String key, int page, int limit) {
        showLoadingDialog("正在查询中");
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.queryList, true, true, page, limit);
        params.addParameter("type", type);
        params.addParameter("dtstart", startDate);
        params.addParameter("dtend", endDate);
        params.addParameter("like", key);
        params.addParameter("depsysid", Global.getDepSysId());
        x.http().post(params, new XHttpShorthand<QueryList>() {
            @Override
            public void success(QueryList model) {
                if (model.getResult()) {
                    MyLog.app("查询成功");
                    if (page == 1) {
                        setListLiveData.setValue(model);
                    } else {
                        addListLiveData.setValue(model);
                    }
                } else {
                    MyLog.appError("查询失败");
                }
            }

            @Override
            public void error(Throwable e) {
                MyLog.appError("查询失败");
            }

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