<!-- 办理住院详情 -->
<template>
    <nav-bar navbar-data="navbarData" />
    <view
        :style="{ paddingTop: height  +'px', height: 'calc(100vh - '+ height +'px)', overflow: 'hidden' }"
        class="admission-details">
        <view class="admission-details-personnel-info">
            <view class="adpi-photo"
                :style="{ background: 'url(' + (userData && userData.patSexDesc === '男' ? genderMale : (userData.patSexDesc === '女' ? genderFemale : unknownGender)) + ') no-repeat', backgroundSize: '100% auto' }">
            </view>
            <view class="adpi-info">
                <view>
                    {{ detailObj.patName }}<text class="adpi-info-sex">{{ detailObj.sexDesc }}</text>
                </view>
                <view class="adpi-info-patNo">{{ detailObj.patNo }}</view>
            </view>
        </view>
        <view class="waiting-progress-card"
            :style="{ height: 'calc(100vh - '+ height +'px - 172rpx)', overflow: 'auto' }">
            <view class="waiting-progress-content">
                <view class="admission-detailss-card-head">
                    <view class="wpcb-label">住院证详情</view>
                    <view class="wpcb-value"></view>
                </view>
                <view class="waiting-progress-card-boby admission-detailss-card-boby">
                    <view>
                        <view class="wpcb-label">住院证编号：</view>
                        <view class="wpcb-value">{{ detailObj.adminNo }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">入院日期：</view>
                        <view class="wpcb-value wpcb-value-btn">
                            <text>{{ startDate }}</text>
                            <van-button plain round type="info" @tap="showPopupStart" v-if="detailObj.admStaCode === 'W'">修改入院日期</van-button>
                        </view>
                    </view>
                    <view>
                        <view class="wpcb-label">入院途径：</view>
                        <view class="wpcb-value FF9933">{{ detailObj.modeDesc }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">入院机构：</view>
                        <view class="wpcb-value">{{ detailObj.hospDesc }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">入院科室：</view>
                        <view class="wpcb-value">{{ detailObj.locDesc }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">入院病区：</view>
                        <view class="wpcb-value">{{ detailObj.wardDesc }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">病区位置：</view>
                        <view class="wpcb-value">{{ detailObj.address }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">联系电话：</view>
                        <view class="wpcb-value">{{ detailObj.phone }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">入院病情：</view>
                        <view class="wpcb-value">{{ detailObj.condtionDesc }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">入院方式：</view>
                        <view class="wpcb-value">{{ detailObj.routeDesc }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">收治原则：</view>
                        <view class="wpcb-value">{{ detailObj.princDesc }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">入院床位：</view>
                        <view class="wpcb-value wpcb-value-btn">
                            <text>{{ bedDesc }}</text>
                            <van-button plain round type="info" @tap="handleBedSelection" v-if="detailObj.admStaCode === 'W'">修改床位</van-button>
                        </view>
                    </view>
                    <view>
                        <view class="wpcb-label">日间手术：</view>
                        <view class="wpcb-value">{{ detailObj.daytimeOP }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">推荐预交金：</view>
                        <view class="wpcb-value">￥ {{ detailObj.inDeposit }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">诊断：</view>
                        <view class="wpcb-value">{{ detailObj.diagDesc }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">申请科室：</view>
                        <view class="wpcb-value">{{ detailObj.locDesc }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">申请医生：</view>
                        <view class="wpcb-value">{{ detailObj.docDesc }}</view>
                    </view>
                    <view>
                        <view class="wpcb-label">申请时间：</view>
                        <view class="wpcb-value">{{ detailObj.adminDate }}</view>
                    </view>
                </view>
            </view>
        </view>
        <van-popup :show="bedVisible" position="bottom" custom-style="height: 728rpx;" @close="hanldeClose" closeable>
            <view>
                <view class="bed-title">选择床位</view>
                <view class="bed-type">
                    <view v-for="(item, index) in roomType" :key="index" @tap="handleChangeBed(item)" :data-page="item"
                        :class="item.key === bedActive ? 'bed-type-item active' : 'bed-type-item'">
                        {{ item.title }}
                    </view>
                </view>
                <picker range="columns" :value="2" @change="handleBenChange"></picker>
                <view class="bed-btn">
                    <van-button block round type="info" @tap="closeBedVisible">确认</van-button>
                </view>
            </view>
        </van-popup>
        <van-popup :show="showStart" @close="onCloseStart" position="bottom">
            <uni-datetime-picker type="date" :value="startTime" @change="onInputStart" />
        </van-popup>
    </view>
</template>

<script>
import $http from "@/utils/https"
import Utils from "@/utils/utils"
import httpConfig from '@/utils/httpConfig'
import NavBar from '@/components/navBar/index'
import VanButton from '@/components/button/index'
const app = getApp()
export default {
    components: {
        'nav-bar': NavBar,
        'van-button': VanButton,
    },
    data() {
        return {
            imgHost: httpConfig && httpConfig.imgHost,
            userData: this.$cache.get('userData'),
            height: app.globalData.height,
            navbarData: {
                isGoHome: true,
                isGoBack: true,
                title: "查看详情"
            },
            genderMale: httpConfig.imgHost + "/images/icon/gender-male.png", // 男
            genderFemale: httpConfig.imgHost + "/images/icon/gender-female.png", // 女
            unknownGender: httpConfig.imgHost + "/images/icon/unknown-gender.png", // 未知性别
            bedVisible: false,
            roomType: [],
            columns: [],
            bedActive: 0,
            bedDesc: "",
            bedID: "",
            startTime: "",
            startDate: "",
            showStart: false,
            detailObj: {},
            bedID: ""
        }
    },
    onLoad: function (options) {
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.on("detailObj", (data) => {
            this.setData({
                detailObj: data.detailObj,
                bedDesc: data.detailObj.bedDesc
            }, () => {
                let startDate = data.detailObj.adminDate
                let arr = startDate.split("-")
                this.setData({
                    startTime: new Date(+arr[0], +arr[1] - 1, +arr[2]).getTime(),
                    startDate: startDate
                })
                this.getRoomType()
                this.getRoomNoList()
            });
        })
    },
    methods: {
        // 关闭选择床位弹窗
        hanldeClose: function () {
            this.setData({
                bedVisible: false
            })
        },

        // 打开选择床位弹窗
        handleBedSelection: function () {
            this.setData({
                bedVisible: true
            })
        },

        //  切换床类型
        handleChangeBed: function (event) {
            let pageItem = event.target && event.target.dataset && event.target.dataset.page ? event.target.dataset.page : {};
            this.setData({
                bedActive: pageItem && pageItem.key ? pageItem.key : 1
            }, () => {
                this.getRoomNoList()
            });
        },

        // 选择床位
        handleBenChange: function (event) {
            this.setData({
                bedDesc: event.detail.value.text,
                bedID: event.detail.value.key,
            }, () => {
                console.log("handleBenChange", this.bedID, this.bedDesc)
            })
        },
        closeBedVisible: function () {
            this.setData({
                bedVisible: false
            }, () => {
                this.UpdateAdmissionTime()
            })
        },
        onInputStart(event) {
            this.setData({
                startTime: event.detail,
                startDate: Utils.timestampToDateFormat(event.detail),
                showStart: false,
            }, () => {
                console.log("UpdateAdmissionTime", this.detailObj)
                this.UpdateAdmissionTime();
            })
        },
        showPopupStart() {
            this.setData({
                showStart: true
            });
        },

        onCloseStart() {
            this.setData({
                showStart: false
            });
        },

        //修改住院预约时间和床位
        UpdateAdmissionTime: function () {
            let {
                detailObj,
                startDate,
                bedID
            } = this
            let data = {
                params: [{
                    adminID: detailObj.adminID,
                    adminDate: startDate || "",
                    bedID: bedID || ""
                }]
            }
            $http.post("urlS", this, {
                code: "03100206",
                data: data,
                namespace: "Y",
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setData({
                            admissionList: res.result
                        })
                    } else {
                        this.setData({
                            admissionList: []
                        })
                    }
                }
            });
        },
        //查询房间类型
        getRoomType: function () {
            let data = {
                params: [{
                }]
            }
            $http.post("urlS", this, {
                code: "03100208",
                data: data,
                namespace: "Y",
                success: function (res) {
                    if (+res.errorCode === 0) {
                        let roomTypeList = res.result
                        for (var i = 0; i < roomTypeList.length; i++) {
                            roomTypeList[i].key = roomTypeList[i].roomTypeID
                            roomTypeList[i].title = roomTypeList[i].roomTypeDesc
                        }
                        this.setData({
                            roomType: roomTypeList
                        })
                    } else {
                        this.setData({
                            roomType: []
                        })
                    }
                }
            });
        },
        //查询床号
        getRoomNoList: function () {
            let userData = this.$cache.get('userData')
            let { detailObj, bedActive } = this
            let roomTypeID = ""
            if (+bedActive === 0) {
                roomTypeID = ""
            } else {
                roomTypeID = bedActive
            }
            let data = {
                params: [{
                    wardID: detailObj.wardID,
                    roomTypeID: roomTypeID,
                    hospCode: userData.hisHospID
                }]
            }
            $http.post("urlS", this, {
                code: "03100207",
                data: data,
                namespace: "Y",
                success: function (res) {
                    if (+res.errorCode === 0) {
                        let bedNoList = res.result
                        for (var i = 0; i < bedNoList.length; i++) {
                            bedNoList[i].key = bedNoList[i].bedID
                            bedNoList[i].text = bedNoList[i].bedDesc
                        }
                        this.setData({
                            columns: bedNoList
                        })
                    } else {
                        this.setData({
                            columns: []
                        })
                    }
                }
            });
        },
    }
}
</script>

<style></style>