package com.yidianling.medical.archives

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.utils.StatusBarUtils
import com.yidianling.common.tools.ToastUtil
import com.yidianling.im.R
import com.yidianling.uikit.custom.http.ServiceImpl
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.medical_add_new_archives_activity.*


/**
 * 新增问诊人
 * */
class MedicalAddNewArchivesActivity : BaseActivity() {


    companion object {
        var mMarried = -1
        var mRelation = -1

        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, MedicalAddNewArchivesActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun layoutResId(): Int {
        return R.layout.medical_add_new_archives_activity
    }

    override fun initDataAndEvent() {
        initStatus()
        group_merried.setOnCheckedChangeListener { group, checkedId ->
            //婚姻状态
            when (checkedId) {
                R.id.married -> {
                    //已婚  1
                    mMarried = 1

                }
                R.id.unmarried -> {
                    //未婚  0
                    mMarried = 0
                }

                R.id.widowed -> {
                    //丧偶  2
                    mMarried = 2
                }
                R.id.divorce -> {
                    //离异  3
                    mMarried = 3
                }
                else -> {

                    mMarried = -1
                }
            }
        }
        group_relation.setOnCheckedChangeListener { group, checkedId ->
            //与本人关系
            when (checkedId) {
                R.id.me -> {
                    //本人  0
                    mRelation = 1

                }
                R.id.parent -> {
                    //父母  0
                    mRelation = 1
                }

                R.id.spouse -> {
                    //配偶  2
                    mRelation = 2
                }
                R.id.children -> {
                    //子女  3
                    mRelation = 3
                }
                R.id.other -> {
                    //其他  4
                    mRelation = 4
                }
                else -> {

                    mRelation = -1
                }
            }
        }


        add_archives_save.setOnClickListener {
            //保存新增问诊人
            checkInfo()
        }

        iv_back.setOnClickListener { finish() }


    }

    @SuppressLint("CheckResult")
    private fun checkInfo() {
        if (et_name.text.isNullOrEmpty()) {
            ToastUtil.toastLong(this@MedicalAddNewArchivesActivity, "请填写姓名")
            return
        }
        if (et_idcard.text.isNullOrEmpty()) {
            ToastUtil.toastLong(this@MedicalAddNewArchivesActivity, "请填写身份证号")
            return
        }

        if (et_idcard.text.length < 15 || et_idcard.text.length > 18) {
            ToastUtil.toastLong(this@MedicalAddNewArchivesActivity, "请填写有效身份证号")
            return
        }
        if (mMarried == -1) {
            ToastUtil.toastLong(this@MedicalAddNewArchivesActivity, "请选择婚姻状况")
            return
        }
        if (mRelation == -1) {
            ToastUtil.toastLong(this@MedicalAddNewArchivesActivity, "请选择与就诊人的关系")
            return
        }

        ServiceImpl.instance.createArchives(
            et_name.text.toString(),
            et_idcard.text.toString(),
            mMarried,
            mRelation
        )
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                if ("200" == it.code) {
                    finish()
                    ToastUtil.toastShort("保存成功")
                } else {
                    ToastUtil.toastShort(it.msg)
                }
            }, {
                it.printStackTrace()
                ToastUtil.toastShort("网络错误")
            })


    }

    private fun initStatus() {
        StatusBarUtils.setTransparentForImageView(this@MedicalAddNewArchivesActivity, null)
        StatusBarUtils.statusBarLightMode(this@MedicalAddNewArchivesActivity)
    }
}