package com.yidian.shenghuoquan.bmanagercenter.personnel.ui

import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.constant.XRouterPathConstants.Companion.PERSONAL_STAFF_MANAGE
import com.yidian.bcommon.base.BaseActivity
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.bcommon.bean.HintOptionBean
import com.yidian.bcommon.extensions.clickAntiShake
import com.yidian.shenghuoquan.bmanagercenter.databinding.ActivityStaffManageBinding
import com.yidian.shenghuoquan.bmanagercenter.personnel.bean.AccountUser
import com.yidian.shenghuoquan.bmanagercenter.personnel.http.PersonalRemoteDataSource
import com.yidian.shenghuoquan.bmanagercenter.personnel.http.callback.IPersonalDissolveCallBack
import com.yidian.bcommon.widget.dialog.HintDialog
import com.yidian.bcommon.utils.SensitiveInfoUtil
import com.yidian.shenghuoquan.bmanagercenter.manager.LifeAccountManager
import com.yidian.xpage.XPageManager

/**
 * 人员管理 —— 员工管理页面
 */
class StaffManageActivity : BaseActivity<ActivityStaffManageBinding>() {

  private var currentUserId: Long? = null

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

  override fun getXPageName(): String {
    return PERSONAL_STAFF_MANAGE
  }

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    initTitle()
    getIntentData(intent)
  }

  private fun initTitle() {
    viewBind.include.tvTitle.text = "员工管理"
    viewBind.include.ivBack.clickAntiShake {
      XPageManager.pop(null)
    }
    viewBind.include.tvMenu.text = "解除关系"
    viewBind.include.tvMenu.setTextColor(ContextCompat.getColor(this, R.color.color_FFFF3A3A))
    viewBind.include.tvMenu.clickAntiShake {
      showConfirmDialog()
    }
  }

  private fun getIntentData(intent: Intent?) {
    val paramsMap = intent?.getSerializableExtra(XRouterPathConstants.ParamsKey)
    if (paramsMap != null) {
      paramsMap as HashMap<*, *>
      val staffInfo = paramsMap["staffInfo"] as AccountUser
      currentUserId = staffInfo.userId
      viewBind.tvUserName.text = staffInfo.nickName
      viewBind.tvLoginAccount.text = staffInfo.mobile?.let {
        SensitiveInfoUtil.mobileEncrypt(it)
      }
      val sb = StringBuilder()
      staffInfo.roleList?.forEachIndexed { index, s ->
        sb.append(s)
        if (index < staffInfo.roleList.size - 1) {
          sb.append("｜")
        }
      }
      viewBind.tvCurrentRole.text = sb
    }
  }


  private fun showConfirmDialog() {
    val dialog = HintDialog(this, resources.getString(R.string.dissolve_relationship),
      arrayListOf(HintOptionBean("关闭"), HintOptionBean("解除关系")),
      object : HintDialog.OnHintOptionCallback {

        override fun onLeftOption() {
        }

        override fun onRightOption() {
          requestDissolve()
        }
      })
    dialog.setRightBtnColor(ContextCompat.getColor(this, R.color.color_FFFF3A3A))
    dialog.show()
  }

  /**
   * 请求解除关系
   */
  private fun requestDissolve() {
    val paramsMap = HashMap<String, String?>()
    paramsMap["delete_user_id"] = currentUserId.toString()
    paramsMap["life_account_id"] = LifeAccountManager.getLifeAccountId()
    PersonalRemoteDataSource.getDissolveRelationship(getDissolveCallBack, paramsMap)
  }


  private val getDissolveCallBack = object : IPersonalDissolveCallBack {

    override fun dissolveSuccess() {
      // 返回员工列表并刷新
      XPageManager.push(XRouterPathConstants.PERSONAL_MY_STAFF, null)
    }
  }
}
