<template>
    <div>
        <van-nav-bar :title="routerDetail.id ? '修改通用随访': '新增通用随访'" left-text='' left-arrow
                     @click-left='toBack'></van-nav-bar>
        <div class='p-4 h-overflow' ref='all'>
            <base-info :info='info'
                       :modeEnumList='modeEnumList'
                       v-show='step == 1'
                       ref='baseInfo'
            ></base-info>
            <general-f-u-form :info='info'
                              :first-form='firstForm'
                              :modeEnumList='modeEnumList'
                              v-show='step == 2'
                              ref='generalFUForm'
            ></general-f-u-form>
            <common-bottom :info='info'
                           :first-form='firstForm'
                           :modeEnumList='modeEnumList'
                           v-show='step == 3'
                           ref='commonBottom'
            ></common-bottom>
        </div>
        <div class='pt-2 pb-2'>
            <div class='px-5  grow flex flex-col justify-end' v-if='step == 1'>
                <van-button type='primary' block round
                            @click='toNext(2)'>下一步
                </van-button>
            </div>

            <div class='px-5  flex align-center justify-around' v-if='step == 2'>
                <van-button type='primary' round plain style='width: 44%'
                            @click='toNext(1)'>上一步
                </van-button>
                <van-button type='primary' round style='width: 44%'
                            @click='toNext(3)'>下一步
                </van-button>
            </div>

            <div class='px-5  grow flex flex-col justify-end' v-if='step == 3'>
                <van-button type='primary' block round
                            @click='onsubmit'>提交
                </van-button>
            </div>
        </div>
    </div>
</template>

<script>
import BaseInfo from '@/doctor/followUp/generalFU/form/BaseInfo'
import {
    addCurrency,
    fetchCurrencyById,
    getChronicResidentsId, getResidentWX,
    updateCurrency,
    upLoadMultifile
} from '@/api/doctor/generalFU'
import GeneralFUForm from '@/doctor/followUp/generalFU/form/GeneralFUForm'
import CommonBottom from '@/doctor/followUp/generalFU/form/CommonBottom'
const getModeEnum = (patientInfo = {}) => {
    return {
        mode1: [
            {
                value: '1', name: `门诊`,
                children: [
                    { value: 1, name: `复检`, disabled: false },
                    { value: 2, name: `指导`, disabled: false },
                    { value: 3, name: `宣教`, disabled: false },
                    { value: 4, name: `催检`, disabled: true }
                ]
            },
            {
                value: '2', name: `住院`,
                children: [
                    { value: 1, name: `复检`, disabled: false },
                    { value: 2, name: `指导`, disabled: false },
                    { value: 3, name: `宣教`, disabled: false },
                    { value: 4, name: `催检`, disabled: true }
                ]
            },
            {
                value: '3', name: `入户`,
                children: [
                    { value: 1, name: `复检`, disabled: false },
                    { value: 2, name: `指导`, disabled: false },
                    { value: 3, name: `宣教`, disabled: false },
                    { value: 4, name: `催检`, disabled: true }
                ]
            },
            {
                value: '4', name: `电话`,
                children: [
                    { value: 1, name: `复检`, disabled: false },
                    { value: 2, name: `指导`, disabled: false },
                    { value: 3, name: `宣教`, disabled: true },
                    { value: 4, name: `催检`, disabled: false }
                ]
            },
            {
                value: '5', name: `短信`,
                children: [
                    { value: 1, name: `复检`, disabled: true },
                    { value: 2, name: `指导`, disabled: false },
                    { value: 3, name: `宣教`, disabled: false },
                    { value: 4, name: `催检`, disabled: false }
                ]
            },
            {
                value: '6', name: `微信`,
                children: [
                    { value: 1, name: `复检`, disabled: true },
                    { value: 2, name: `指导`, disabled: false },
                    { value: 3, name: `宣教`, disabled: false },
                    { value: 4, name: `催检`, disabled: false }
                ]
            }
        ],
        tel: `（本人电话：${patientInfo.telephone || ''}）`,
        weixi: `（绑定电话：${patientInfo.weixin || '未绑定'}）`,
        telephone: patientInfo.telephone,
        weixinTel: patientInfo.weixin,
        //是否下次随访日期必填
        nextVisitDateReq: true,
        //催检内容是否显示患者姓名
        urgeResidentShow: true
    }
}
export default {
    name: 'Index',
    components: { CommonBottom, GeneralFUForm, BaseInfo },
    data() {
        return {
            info: {},
            resident: {},
            step: 1,
            //第一步提交的表单
            firstForm: {},
            //居民信息
            residentInfo: {},
            modeEnumList: getModeEnum({})
        }
    },
    created() {
        this.init()
    },
    computed: {
        routerDetail() {
            return this.$route.query
        }
    },
    methods: {
        async init() {
            this.info = {}
            if (this.routerDetail.id) {
                const res = await fetchCurrencyById({ id: this.routerDetail.id })
                let result = res.data || {}
                const { residentsRecord = {} } = result
                const { id, ...others } = residentsRecord
                this.info = {
                    ...others,
                    personId: id,
                    ...result
                }
            } else {
                const res = await getChronicResidentsId(this.routerDetail.residentInfoId)
                const weixinInfo = await getResidentWX({residentInfoId: this.routerDetail.residentInfoId})
                const {
                    id,
                    createDate,
                    createDoctorId,
                    createDoctorName,
                    createOfficeId,
                    createOfficeName,
                    createUnitId,
                    createUnitName,
                    updated,
                    ...others
                } = res.data
                this.info = {
                    personId: id,
                    ...others,
                    sendNumber: weixinInfo?.data?.telephone
                }
                this.info.diseaseType = this.routerDetail.diseaseType
            }
            this.modeEnumList = getModeEnum(this.info)
        },
        async toNext(val) {
            this.$refs.all.scrollTo(0, 0)
            if (val == 2) {
                this.firstForm = await this.$refs.baseInfo.onSubmit()
            }
            if (val == 3) {
                await this.$refs.generalFUForm.onSubmit()
            }
            this.step = val
        },
        //图片上传
        async upload(imgList = []) {
            let list = []
            if (imgList.length) {
                let fileId = Math.random().toString(16).substring(2, 8)
                let data = new FormData()
                data.append('parentId', fileId)
                imgList.forEach(item => {
                    data.append('files', item)
                })
                let res = await upLoadMultifile(data)
                let result = res.data || []
                result.forEach(item => {
                    list.push(item.id)
                })
            }
            return list
        },
        //原图片数据处理
        baseImgHandle(imgList = []) {
            let list = []
            imgList.forEach(item => {
                list.push(item.id)
            })
            return list
        },
        async onsubmit() {
            let baseInfo = await this.$refs.baseInfo.onSubmit()
            let generalFUForm = await this.$refs.generalFUForm.onSubmit()
            let commonBottom = await this.$refs.commonBottom.onSubmit()
            let params = {
                ...generalFUForm,
                ...commonBottom,
                ...baseInfo,
                source: 2
            }
            if (this.info.id) {
                params.visitRecordId = this.info.visitRecordId
            }
            let fun = this.info.id ? updateCurrency : addCurrency
            fun(params, true).then(({ code }) => {
                if (code == 'SUCCESS') {
                    this.$router.back()
                }
            })
        },
        toBack() {
            if (this.step != 1) {
                this.step--
                return
            }
            this.$router.back()
        }
    }
}
</script>

<style scoped lang='less'>
:deep(.van-nav-bar .van-icon) {
    color: #000000;
}

.h-overflow {
    height: calc(100vh - 110px);
    overflow-y: auto;
}
</style>