package com.zswc.creativity.fragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.zswc.creativity.R
import com.zswc.creativity.adapter.BillAdapter
import com.zswc.creativity.base.MBRFragment
import com.zswc.creativity.config.MIntentAction
import com.zswc.creativity.databinding.FragmentAccountBillBinding
import com.zswc.creativity.databinding.ItemAccountHeadBinding
import com.zswc.creativity.vmodel.AccountBillVModel

class AccountBillFragment : MBRFragment<AccountBillVModel, BillAdapter, FragmentAccountBillBinding>() {

    var headBinding: ItemAccountHeadBinding? =null
    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        setHead()
        vm.fragment = this
        vm.getAmountCount()

        vm.info.observe(this, Observer {
            if (null == it) {
                return@Observer
            }
            headBinding!!.vm = vm
        })
    }

    private fun setHead() {
        //套餐详情
        //val headerView: View = layoutInflater.inflate(R.layout.item_account_head, null)
         headBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_account_head, null, false)
        val headerView: View = headBinding!!.root
        adapter().setHeaderView(headerView)
    }


    override fun initAdapter(): BillAdapter {
        return BillAdapter()
    }

    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentAccountBillBinding {
        return FragmentAccountBillBinding.inflate(layoutInflater, container, false)
    }

    override fun title(): String? {
        return null
    }

    override fun vmClass(): Class<AccountBillVModel> {
        return AccountBillVModel::class.java
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (MIntentAction.REQUEST_CODE_ONE == requestCode && resultCode == Activity.RESULT_OK) {
            //刷新
            vm.getAmountCount()
            onRefresh()
        }
    }

    override fun onRefresh() {
        super.onRefresh()
        vm.getAmountCount()
    }
}
