package com.zswc.creativity.vmodel

import android.app.Application
import android.view.inputmethod.EditorInfo
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.zswc.creativity.BR
import com.zswc.creativity.inte.ChoiceData
import com.zswc.creativity.model.IconData
import com.zswc.creativity.utils.dialog.ChoseTimeDialog
import com.zswc.creativity.utils.net.NetEngine
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import kotlinx.coroutines.launch

/**
 *Created by shihuiyun
 *on 2020/9/25
 */
class AccountAnalysisVModel(application: Application) : BViewModel<BRepository>(application) {

    val info: MutableLiveData<IconData> = MutableLiveData<IconData>()

    val timeData: MutableLiveData<String> = MutableLiveData<String>()

    var start_time=""
    var end_time=""


    @Bindable
    var k: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.k)
        }

    fun  choseTime(){//选择时间

        ChoseTimeDialog.choseTime(repository().context!!,object: ChoiceData {
            override fun onRestuse(time: String?) {
                timeData.value=time
                var ss=time!!.split("至")
                start_time=ss[0]
                end_time=ss[1]
                //刷新
                getInfo()
            }
        })

    }

    fun goSearch(){
        getInfo()
    }





    fun getInfo(){


        viewModelScope.launch {

          var it=  repository().lreq { NetEngine.service.amountLine(start_time,end_time,k)  }
            if(it.ok()){
                info.value=it.data()
            }
        }
    }


    fun onAction(actionId: Int): Boolean {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            getInfo()
        }
        return false;
    }

}
