<template>
    <div class="doctor-info">
        <div class="doc-form-label" required>随访单位</div>
        <van-field is-link
            v-model='form.visitUnitName'
            readonly
            placeholder='请选择'
            name="visitUnitId"
            @click="showUnit = true"
            :rules="[{ required: true, message: '请选择' }]"
        />
        <DocUnit v-model:show="showUnit"
            v-model:value="form.visitUnitId"
            :valueName="form?.visitUnitName"
            @change="unitChange"
        />

        <div class="doc-form-label" required>随访科室</div>
        <van-field is-link
            v-model='form.visitOfficeName'
            readonly
            placeholder='请选择'
            name="visitOfficeId"
            @click="showOffice = true"
            :rules="[{ required: true, message: '请选择' }]"
        />
        <DocOffice v-model:show="showOffice"
            v-model:value="form.visitOfficeId"
            :unitId="form.visitUnitId"
            @change="officeChange"
        />

        <div class="doc-form-label" required>随访医生</div>
        <van-field is-link
            v-model='form.visitDoctorName'
            readonly
            placeholder='请选择'
            name="visitDoctorId"
            @click="showDoctor = true"
            :rules="[{ required: true, message: '请选择' }]"
        />
        <DocOfficeDoctor v-model:show="showDoctor"
            :allowClear="false"
            v-model:value="form.visitDoctorId"
            :unitId="form.visitUnitId"
            :officeId="form.visitOfficeId"
            @change="(option) => { form.visitDoctorName = option.staffName }"
        />
    </div>
</template>

<script>
import { useStore } from '@/doctor/store'
import dayjs from 'dayjs'
import DocUnit from '@/doctor/components/docUnit/DocUnit.vue'
import DocOffice from '@/doctor/components/docOffice/DocOffice.vue'
import DocOfficeDoctor from '@/doctor/components/docOfficeDoctor/DocOfficeDoctor.vue'

const defaultForm = (info = {}) => {
    const form = {
        createDate: undefined,
        // 随访单位
        visitUnitId: undefined,
        visitUnitName: undefined,
        // 随访科室
        visitOfficeId: undefined,
        visitOfficeName: undefined,
        // 随访医生
        visitDoctorId: undefined,
        visitDoctorName: undefined,
        // 录入单位
        createUnitId: undefined,
        createUnitName: undefined,
        // 录入科室
        createOfficeId: undefined,
        createOfficeName: undefined,
        // 录入医生
        createDoctorId: undefined,
        createDoctorName: undefined
    }
    Reflect.ownKeys(form).forEach(key => {
        if (info[key] != undefined) {
            form[key] = info[key]
        }
    })
    return form
}
export default {
    components: {
        DocUnit,
        DocOffice,
        DocOfficeDoctor
    },
    props: {
        info: { default: () => ({}) }
    },
    data() {
        return {
            form: {},
            store: useStore(),
            showUnit: false,
            showOffice: false,
            showDoctor: false
        }
    },
    computed: {
        authInfo() {
            return this.store.authInfo
        }
    },
    methods: {
        initForm() {
            this.form.createDate = dayjs().format('YYYY-MM-DD')
            // 随访单位
            this.form.visitUnitId = this.authInfo.unitId
            this.form.visitUnitName = this.authInfo.unitName
            // 随访科室
            this.form.visitOfficeId = this.authInfo.officeId
            this.form.visitOfficeName = this.authInfo.officeName
            // 随访医生
            this.form.visitDoctorId = this.authInfo.relationId
            this.form.visitDoctorName = this.authInfo.nickName
            // 录入单位
            this.form.createUnitId = this.authInfo.unitId
            this.form.createUnitName = this.authInfo.unitName
            // 录入科室
            this.form.createOfficeId = this.authInfo.officeId
            this.form.createOfficeName = this.authInfo.officeName
            // 录入医生
            this.form.createDoctorId = this.authInfo.relationId
            this.form.createDoctorName = this.authInfo.nickName
        },
        unitChange(option = {}) {
            this.form.visitUnitName = option.unitName
            this.form.visitDoctorId = undefined
            this.form.visitDoctorName = undefined
            this.form.visitOfficeId = undefined
            this.form.visitOfficeName = undefined
        },

        officeChange(option = {}) {
            this.form.visitOfficeName = option.officeName
            this.form.visitDoctorId = undefined
            this.form.visitDoctorName = undefined
        },
        submit() {
            return this.form
        }
    },
    watch: {
        info: {
            handler(info) {
                this.form = defaultForm(info)
                if (!this.info.id) {
                    this.initForm()
                }
            },
            immediate: true
        }
    }
}
</script>

<style lang="less" scoped></style>
