package com.zxbw.modulemain.activity

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.zxbw.modulemain.R
import com.zxbw.modulemain.adapter.PhoneVouchersAdapter
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.qy.CouponsEntity
import com.zxhl.cms.utils.JumpUtils
import kotlinx.android.synthetic.main.activity_layout_phone_vouchers.*
import kotlinx.android.synthetic.main.activity_layout_phone_vouchers.id_img_back

/**
 * @author (wangXuewei)
 * @datetime 2022-06-14 18:09 GMT+8
 * @detail :话费券列表
 */
class XymhPhoneVouchersActivity : BaseActivity() {

    private var mAdapter: PhoneVouchersAdapter? = null


    override fun layoutID(): Int = R.layout.activity_layout_phone_vouchers

    override fun onClick(v: View?) {
        when (v) {
            id_img_back -> {
                finish()
            }
        }
    }


    override fun init() {
        id_img_back.setOnClickListener(this)

        mAdapter = PhoneVouchersAdapter(
            this
        ) { view, position, data ->
            JumpUtils.RechargeJump()
            finish()
        }
        id_rl_phone_voucher?.layoutManager = LinearLayoutManager(mActivity)
        id_rl_phone_voucher?.adapter = mAdapter
        getData()
    }


    private fun getData() {
        ApiClient.homeApi.getFetchallCard().compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<CouponsEntity>>() {
                override fun onSuccess(result: List<CouponsEntity>?) {
                    if (result.isNullOrEmpty()) {
                        id_phone_voucher_empty?.visibility = View.VISIBLE
                        id_rl_phone_voucher?.visibility = View.GONE
                    } else {
                        id_phone_voucher_empty?.visibility = View.GONE
                        id_rl_phone_voucher?.visibility = View.VISIBLE
                        mAdapter?.clear()
                        mAdapter?.appendToList(result)
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    id_phone_voucher_empty?.visibility = View.VISIBLE
                    id_rl_phone_voucher?.visibility = View.GONE
                }
            })
    }
}