package com.zj365.health.act

import android.view.View
import android.widget.EditText
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.databinding.ActOnlineMakeBinding
import com.zj365.health.vm.OnLineMakeVm
import com.zj365.health.weight.TimeAppointmentDialog
import com.xty.network.model.KeeperBean
import com.xty.network.model.OnLineTime

/**
 * 线上预约
 */
@Route(path = ARouterUrl.ONLINE_MAKE)
class OnLineMakeAct: BaseVmAct<OnLineMakeVm>() {
    val binding by lazy { ActOnlineMakeBinding.inflate(layoutInflater)}
    var ymd=""
    var timeSelect=""
    var week=-1
    var hourPerson=2
    val timeDialog by lazy { TimeAppointmentDialog(this,{
        mViewModel.getNowDataEnable(bean!!.id,it)
    }){ymd,time,week->
        this.week=week
        this.ymd=ymd
        timeSelect=time
        binding.mTime.text="$ymd $time"

    } }
    var timeBean:OnLineTime?=null
    var bean :KeeperBean?=null
    override fun setLayout()=binding.root
   override fun initData() {
        super.initData()
        isDarkBar=true
        bean=intent.extras!!.getSerializable("bean") as KeeperBean

       binding.mName.setText(bean?.realname)
       binding.mPhone.setText(bean?.phone)

    }

    override fun liveObserver() {
        mViewModel.timeData.observe(this){
            timeBean=it.data
        }
        mViewModel.submitData.observe(this){
            CommonToastUtils.showSucceedToast("预约成功")
            finish()
        }
        mViewModel.timeLive.observe(this){
            if(timeDialog.isShowing){
                timeDialog.refresh(it.data)
            }
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        (binding.title.mTvTitle.parent as View).setBackgroundColor(ContextCompat.getColor(this,R.color.white))
        binding.title.mTvTitle.text="线上预约${bean!!.realname}"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }

        binding.mTime.setOnClickListener {

               timeDialog.showInfoDialog(hourPerson)
        }
        binding.mBtn.setOnClickListener {
            var name=binding.mName.text.toString().trim()
            var phone=binding.mPhone.text.toString().trim()
            var remark=binding.mEditRemark.text.toString().trim()
            if (phone.isEmpty()){
                CommonToastUtils.showToast("请输入手机号")
                return@setOnClickListener
            }
            if (name.isEmpty()){
                CommonToastUtils.showToast("请输入姓名")
                return@setOnClickListener
            }
            if (timeSelect.isEmpty()){
                CommonToastUtils.showToast("请选择时间")
                return@setOnClickListener
            }
            if (remark.isEmpty()){
                CommonToastUtils.showToast("请输入备注")
                return@setOnClickListener
            }
            mViewModel.addReservation(bean!!.id,ymd,timeSelect,week,name,phone,remark)

        }
        changeEdit(binding.mEditRemark)
        changeEdit(binding.mPhone)
        changeEdit(binding.mName)
        mViewModel.getTimeReservation(bean!!.id)
    }

    fun changeEdit(edit:EditText){
        edit.setOnFocusChangeListener { v, hasFocus ->
            v.isSelected=hasFocus
        }
    }
}