package com.yidian.shenghuoquan.newscontent.personnel.ui

import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import com.orhanobut.hawk.Hawk
import com.yidian.common.HawkConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.common.XRouterPathConstants.Companion.PERSONAL_STAFF_MANAGE
import com.yidian.common.base.BaseActivity
import com.yidian.common.extensions.clickAntiShake
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.bean.HintOptionBean
import com.yidian.shenghuoquan.newscontent.databinding.ActivityStaffManageBinding
import com.yidian.shenghuoquan.newscontent.personnel.bean.AccountUser
import com.yidian.shenghuoquan.newscontent.personnel.http.PersonalRemoteDataSource
import com.yidian.shenghuoquan.newscontent.personnel.http.callback.IPersonalDissolveCallBack
import com.yidian.shenghuoquan.newscontent.ui.dialog.HintDialog
import com.yidian.shenghuoquan.newscontent.utils.SensitiveInfoUtil
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"] = Hawk.get(HawkConfig.LifeAccountId, "")
    PersonalRemoteDataSource.getDissolveRelationship(getDissolveCallBack, paramsMap)
  }


  private val getDissolveCallBack = object : IPersonalDissolveCallBack {

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