package com.yidian.shenghuoquan.newscontent.ui.store

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.yidian.bcommon.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.newscontent.adapter.StoreAdapter
import com.yidian.shenghuoquan.newscontent.databinding.ActivityStoreManagementBinding
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.utils.StorageUtil

/**
 * 门店管理
 */
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"] = StorageUtil.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)
    }
  }
}
