package com.yidianling.medical.archives

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.*
import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.utils.StatusBarUtils
import com.ydl.ydlcommon.view.dialog.NormalDialog
import com.yidianling.common.tools.ToastUtil
import com.yidianling.im.R
import com.yidianling.medical.utils.EditTextUtils
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() {
        mMarried = -1
        mRelation = -1
        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()
        }

        EditTextUtils.disableCopyAndPaste(et_name)

        iv_back.setOnClickListener { finish() }

        showDialogTip()
    }

    private fun showDialogTip() {
        val builder = NormalDialog.Builder(this@MedicalAddNewArchivesActivity, "center", false)
        builder.setTitle("")
        var strs = arrayOf("就诊人的身份证号码", "就诊人的授权同意", "您需要在提供前征得该儿童监护人的同意")
        builder.setMessageAndBolds(
            "\n为向您添加的就诊人提供问诊服务，您需要向我们提供该就诊人的身份证号码，并确保在提供前已取得该就诊人的授权同意；若该就诊人为儿童的，您需要在提供前征得该儿童监护人的同意。\n",
            strs
        )
        builder.setPositiveButton(
            "同意"
        ) { dialog, which ->
            dialog.dismiss()
        }.setNegativeButton(
            "不同意"
        ) { dialog, which ->
            //关闭页面
            finish()
            dialog.dismiss()
        }
        builder.setRight_color("#3464EC")
        builder.setLeft_color("#8595A9")
        builder.setContent_color("#10233A")

        builder.create().show()

    }

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

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

}