package com.zj365.mime.act

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseListAct
import com.xty.common.TimeSelect
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.util.CommonToastUtils
import com.zj365.mime.R
import com.zj365.mime.adapter.LnvitationMessageAdapter
import com.zj365.mime.databinding.ActLnvitationMessageBinding
import com.zj365.mime.vm.LnvitationMessageVm
import com.xty.network.model.SettingBean

/**
 * 邀请记录页面
 */
@Route(path = ARouterUrl.LNVITATION_MESSAGE)
class LnvitationMessageAct : BaseListAct<LnvitationMessageVm>() {

    private val binding by lazy { ActLnvitationMessageBinding.inflate(layoutInflater) }
    override fun setLayout(): View = binding.root

    private val mAdapter by lazy { LnvitationMessageAdapter() }

    private var user: SettingBean.User? = null
    private var orderByType: Int = 1
    private var year = ""
    private var month = ""
    private var isRegisterTime = true
    private var isActivateTime = true

    private val timSelect by lazy {
        TimeSelect(this) {
            val toString = binding.tvAll.text.toString()
            toString.let {
                val split = it.split("-")
                year = split[0]
                month = split[1]
            }
            page = 1
            loadData()
        }.apply {
            chooseMany = false
            showYear = true
            showMonth = true
            showDay = false
            showHour = false
            showMin = false
            showSec = false
        }
    }

    override fun initData() {
        super.initData()
        isDarkBar = true
        val userBean = intent.extras!!.getSerializable("user")
        if (userBean != null) {
            user = userBean as SettingBean.User
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "邀请记录"
        binding.title.mTvRight.apply {
            visibility = View.VISIBLE
            text = "去邀请"
            setTextColor(resources.getColor(R.color.col_02c))
            setOnClickListener {
                bundle.clear()
                bundle.putSerializable("user", user)
                RouteManager.goAct(ARouterUrl.INVITE_FRIENDS, bundle)
            }
        }
        binding.title.mIvBack.setOnClickListener { finish() }

        //点击选择时间
        binding.tvAll.setOnClickListener {
            timSelect.selectTimeWithYearMonth(it, "yyyy-MM")
        }

        //注册时间
        binding.tvRegisterTime.setOnClickListener {
            page = 1
            binding.ivActivateTime.setImageResource(R.mipmap.paixu)
            binding.ivRegisterTime.setImageResource(R.mipmap.paixu_2)
            if (isRegisterTime) {
                isRegisterTime = false
                orderByType = 1
            } else {
                isRegisterTime = true
                orderByType = 3
            }
            loadData()
        }
        //激活时间
        binding.tvActivateTime.setOnClickListener {
            page = 1
            binding.ivActivateTime.setImageResource(R.mipmap.paixu_2)
            binding.ivRegisterTime.setImageResource(R.mipmap.paixu)
            if (isActivateTime) {
                isActivateTime = false
                orderByType = 2
            } else {
                isActivateTime = true
                orderByType = 4
            }
            loadData()
        }
    }

    override fun initAdapter() {
        setRecycleRefresh(binding.mRecycle, binding.mRefresh, true)
        binding.mRecycle.layoutManager = LinearLayoutManager(this)
        binding.mRecycle.adapter = mAdapter
    }

    override fun loadData() {
        mViewModel.getInviteRecordRequest(page, user!!.id, orderByType, year, month)
    }

    @SuppressLint("SetTextI18n")
    override fun liveObserver() {
        mViewModel.inviteRecordData.observe(this) {
            if (it.code == 0) {
                val data = it.data
                binding.tvNumAll.text = "共${data.total}条"

                setDate(mAdapter, data.records)
            } else {
                CommonToastUtils.showToast("获取邀请记录列表失败！！")
            }
        }
    }
}