package com.yidian.shenghuoquan.bmanagercenter.ui.store

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.yidian.bcommon.constant.XRouterPathConstants.Companion.STORE_MANAGEMENT
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.extensions.hide
import com.yidian.bcommon.extensions.show
import com.yidian.shenghuoquan.bmanagercenter.adapter.StoreAdapter
import com.yidian.shenghuoquan.bmanagercenter.databinding.ActivityStoreManagementBinding
import com.yidian.shenghuoquan.bmanagercenter.http.ApiService
import com.yidian.shenghuoquan.bmanagercenter.manager.LifeAccountManager

/**
 * 门店管理
 */
class StoreManagementActivity : BaseActivity<ActivityStoreManagementBinding>() {

  private lateinit var adapter: StoreAdapter

  override fun createViewBinding(): ActivityStoreManagementBinding {
    return ActivityStoreManagementBinding.inflate(layoutInflater)
  }

  override fun getXPageName(): String {
    return STORE_MANAGEMENT
  }

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    val layoutManager = LinearLayoutManager(this)
    layoutManager.orientation = LinearLayoutManager.VERTICAL
    viewBind.storeList.layoutManager = layoutManager

    adapter = StoreAdapter(this)
    viewBind.storeList.adapter = adapter

    viewBind.addStoreBtn.setOnClickListener {
      val intent = Intent(this, EditStoreActivity::class.java)
      intent.putExtra("from", EditStoreActivity.fromAddStore)
      startActivity(intent)
    }
  }

  override fun onResume() {
    super.onResume()
    val requestParams = HashMap<String, String?>()
    requestParams["life_account_id"] = LifeAccountManager.getLifeAccountId()
    ApiService.getShopList(requestParams) {
      if (it.isNullOrEmpty()) {
        viewBind.storeList.hide()
        viewBind.pvEmptyPage.show()
        return@getShopList
      } else {
        viewBind.storeList.show()
        viewBind.pvEmptyPage.hide()
      }
      adapter.setData(it)
    }
  }
}
