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

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

        <div class="doc-form-label" required>筛查医生</div>
        <van-field is-link
            v-model='form.screenDoctorName'
            readonly
            placeholder='请选择'
            name="screenDoctorId"
            @click="showDoctor = true"
            :rules="[{ required: true, message: '请选择' }]"
        />
        <DocOfficeDoctor v-model:show="showDoctor"
            :allowClear="false"
            v-model:value="form.screenDoctorId"
            :unitId="form.screenUnitId"
            :officeId="form.screenOfficeId"
            @change="(option) => { form.screenDoctorName = 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,
        // 随访单位
        screenUnitId: undefined,
        screenUnitName: undefined,
        // 随访科室
        screenOfficeId: undefined,
        screenOfficeName: undefined,
        // 随访医生
        screenDoctorId: undefined,
        screenDoctorName: 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.screenUnitId = this.authInfo.unitId
            this.form.screenUnitName = this.authInfo.unitName
            // 随访科室
            this.form.screenOfficeId = this.authInfo.officeId
            this.form.screenOfficeName = this.authInfo.officeName
            // 随访医生
            this.form.screenDoctorId = this.authInfo.relationId
            this.form.screenDoctorName = 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.screenUnitName = option.unitName
            this.form.screenDoctorId = undefined
            this.form.screenDoctorName = undefined
            this.form.screenOfficeId = undefined
            this.form.screenOfficeName = undefined
        },

        officeChange(option = {}) {
            this.form.screenOfficeName = option.officeName
            this.form.screenDoctorId = undefined
            this.form.screenDoctorName = 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>
