<template>
    <div class="h-full flex flex-col screening-first">
        <DocNavBar :title="`${id ? '修改' : '新增'}慢特病随访`" class="shrink-0"
                   :backFunc="onBack" ></DocNavBar>
        <div class="p-4 overflow-y-auto grow" ref="all">
            <archiveCommon :info="baseInfo" v-if="step == 1" ref="archiveCommon"></archiveCommon>
            <FormCont :resident-info="baseInfo" :info="slowSpecialInfo" v-else-if="step == 2" ref="FormCont"/>
        </div>
        <div class="shrink-0" v-if="step !== 3">
            <div class='bottom-small-line'></div>
            <div class='px-5 py-2 grow flex justify-between'>
                <template v-if='step == 1'>
                    <van-button type='primary' block round
                                @click='toNext(2)'>下一步</van-button>
                </template>
                <template v-else>
                    <van-button type='primary' block round
                                @click='toNext(3)'>提交</van-button>
                </template>
            </div>
        </div>
    </div>
</template>

<script>
import DocNavBar from '@/doctor/components/docNavBar/DocNavBar.vue'
import archiveCommon from '@/doctor/components/archiveCommon/archiveCommon.vue'
import FormCont from './FormCont.vue'
import { getChronicResidentsId } from '@/api/doctor/generalFU'
import { fetchDataHandle } from '@/utils/common.js'
import { useStore } from '@/doctor/store'
import { addSlowSpecial, fetchSlowSpecialById, updateSlowSpecial } from '@/api/doctor/slowSpecial'

export default {
    components: {
        DocNavBar,
        archiveCommon,
        FormCont,
    },
    data() {
        return {
            store: useStore(),
            step: 1,
            // 患者基础信息
            baseInfo: {},
            // 慢特病信息
            slowSpecialInfo: {},
        }
    },
    computed: {
        id() {
            return this.$route.query.id
        },
        residentInfoId() {
            return this.$route.query.residentInfoId
        },
        idCard() {
            return this.$route.query.idCard
        },
    },
    created() {
        this.init()
    },
    methods: {
        async init() {
            if (this.id) {
                const res = await fetchSlowSpecialById({id: this.id})
                const result = res.data || {}
                this.slowSpecialInfo = result
                this.baseInfo = this.slowSpecialInfo.residentsRecord
            } else {
                if (this.residentInfoId) {
                    let res = await getChronicResidentsId(this.residentInfoId)
                    this.baseInfo = res.data || {}
                    this.slowSpecialInfo.residentInfoId = this.residentInfoId
                    this.slowSpecialInfo.medicalInsuranceCode = res.data.medicalInsuranceCode
                } else if (this.idCard) {
                    // 新建用户
                    this.baseInfo.idCard = this.idCard
                }
            }
        },
        toNext(val) {
            if (val == 2) {
                // 基础信息
                this.$refs.archiveCommon.onSubmit().then(res => {
                    this.baseInfo = res
                    this.step = val
                    this.$refs.all.scrollTo(0, 0)
                })
            } else if (val == 3) {
                // 筛查信息
                this.$refs.FormCont.submit().then(res => {
                    if (!res) return
                    console.log('FormCont.submit', res)
                    const query = fetchDataHandle(res, {
                    })
                    query.residentsRecord = this.baseInfo
                    console.log(JSON.stringify(query))
                    const func = query.id ? updateSlowSpecial : addSlowSpecial
                    func(query).then(({ code }) => {
                        if (code == 'SUCCESS') {
                            this.store.onRefreshMark()
                                this.$router.replace({
                                    path: '/doctor/patient-detail',
                                    query: {
                                        residentInfoId: this.residentInfoId
                                    }
                                })
                        }
                    })
                })
            } else {
                this.step = val
                this.$refs.all.scrollTo(0, 0)
            }
        },
        onBack() {
            if (this.step == 1) {
                this.$router.back()
            } else {
                this.step--
            }
        }
    }
}
</script>

<style lang="less" scoped>

</style>
