<template>
    <view class="page-body">
        <van-dialog id="van-dialog" />
        <view class="card-tap-area" @tap="onCardTap">
            <view class="body-item-status">
                <text :class="'scroll-view-item-status ' + (admRecord.statusCode === 'M' ? '' : 'noOverTime')">{{ admRecord.status }}</text>

                <text :class="'view-item-baseInfo-surplusTime ' + (admRecord.statusCode === 'R' ? 'noStart' : '')">{{ m1.showTime(admRecord) }}</text>
            </view>
            <view class="scroll-view-item-Info">
                <view><image class="scroll-view-item-image" :src="admRecord.patImage"></image></view>
                <view class="view-item-admInfo">
                    <view class="view-item-baseInfo">
                        <view>
                            <text>{{ admRecord.patName }} {{ admRecord.patNo }}</text>
                        </view>
                        <view class="view-item-baseInfo-sex">
                            <image
                                class="view-item-baseInfo-sex-image"
                                :src="admRecord.patSexDesc == '男' ? imgDomain + '/images/icon/doctor/male.png' : imgDomain + '/images/icon/doctor/female.png'"
                            ></image>
                        </view>
                        <view class="view-item-baseInfo-age">
                            <text class="view-item-baseInfo-ageText">{{ admRecord.patAge }}</text>
                        </view>
                    </view>
                    <view class="scroll-view-item-timeRang">
                        <view>就诊时间: {{ admRecord.admDate }} {{ admRecord.admTime }}</view>
                    </view>
                </view>
            </view>
            <view class="view-item-symptom">
                <!-- <text>{{admRecord.symptom}}</text> -->
                <view class="personDesc">{{ admRecord.symptom }}</view>
            </view>
        </view>
        <block v-if="serviceTypeVisible">
            <view class="view-item-admType">
                <text class="view-item-admType-label">服务类型:</text>
                <text class="view-item-admType-desc">{{ admRecord.resourceTypeDesc }}</text>
            </view>
        </block>

        <!-- <view class="scroll-view-item-admType "></view> -->
        <view class="btn-view">
            <button
                v-if="!m2.isDisbaled(itemBtn, admRecord, serviceTypeCode)"
                @tap="parseEventDynamicCode($event, itemBtn.callFun)"
                :plain="itemBtn.plain"
                :size="dateBtnSize"
                :class="'btn-view-botBtn ' + m3.btnStyle(itemBtn, admRecord)"
                :data-admID="admRecord.admID"
                :data-statusCode="admRecord.statusCode"
                :data-patient="admRecord"
                :data-btnData="itemBtn"
                v-for="(itemBtn, index) in botBtnList"
                :key="index"
            >
                {{ itemBtn.desc }}
            </button>
        </view>
    </view>
</template>
<script module="m1" lang="wxs">
var showTime = function(item){
  var showTimeText="";
  if (item['statusCode']!==""){
    if (item.statusCode==="R"){showTimeText="距离取消："+item.countdown}
    if (item.statusCode==="F"){showTimeText=item.finishDate+" "+item.finishTime}
    if (item.statusCode==="A"){showTimeText=item.receiveDate+" "+item.receiveTime}
    if (item.statusCode==="RR"){showTimeText=item.cancelDate+" "+item.cancelTime}
  }
  return showTimeText
}
module.exports = {
  showTime: showTime
}
</script>
<script module="m2" lang="wxs">
var isDisbaled=function(itemBtn,item,serviceTypeCode){
  var disabledFlag=true;
  var statusCode=item.statusCode;
  var btnCode=itemBtn.code;
  if ((statusCode==="R")&&((btnCode==="reject")||(btnCode==="enterRoom")||(btnCode==="call"))){disabledFlag=false}
  if ((statusCode==="CI")&&((btnCode==="enterRoom")||(btnCode==="call"))){disabledFlag=false}
 if ((statusCode==="A")&&((btnCode==="enterRoom")||(btnCode==="call")||(btnCode==="cancel"))){disabledFlag=false}
  if ((statusCode==="F")&&(btnCode==="detail")){disabledFlag=false}
  if ((serviceTypeCode==="image")&&(statusCode==="F")&&(btnCode==="treatmentrecord")){disabledFlag=false}
  if ((statusCode==="RR")&&(btnCode==="detail")){disabledFlag=false}
  if ((statusCode==="M")&&(btnCode==="detail")){disabledFlag=false}
   if ((statusCode==="F")&&(btnCode==="vediotreatRecord")){disabledFlag=false}
   if ((statusCode==="F")&&(btnCode==="cancelFinish")){disabledFlag=false}
  if (btnCode==="detail"){disabledFlag=false}
  return disabledFlag;
}
module.exports = {
  isDisbaled: isDisbaled
}
</script>
<script module="m3" lang="wxs">
var btnStyle=function(itemBtn,item){
  var className='';
  var btnCode=itemBtn.code;
  if (btnCode==="call"){className="btn-view-botBtn-call"}
  if (btnCode==="enterRoom"){className="btn-view-botBtn-enterRoom"}
  if (btnCode==="reject"){className="btn-view-botBtn-reject"}
  if (btnCode==="detail"){className="btn-view-botBtn-detail"}
  if (btnCode==="cancel"){className="btn-view-botBtn-reject"}
  if (btnCode==="treatmentrecord"){className="btn-view-botBtn-treatmentrecord"}
  if (btnCode==="vediotreatRecord"){className="btn-view-botBtn-treatmentrecord"}
  return className;
}
module.exports = {
  btnStyle: btnStyle
}
</script>
<script>
// pages/myConsultRoom/components/patAdmRecord.js
import { $http } from '../../../config/https.js';
import { httpconfig } from '../../../config/httpconfig';
import messageUtil from '../../../utils/message.js';
import commonUtil from '../../../utils/common.js';
import Util from '../../../utils/util.js';
import Dialog from '../../../utils/dialog';
import Cache from '../../../utils/cache.js';
export default {
    data() {
        return {
            item: {},

            botBtnList: [
                {
                    code: 'detail',
                    plain: true,
                    desc: '查看详情',
                    url: '/pages/myConsultRoom/page/graphicDateil/graphicDateil',
                    callFun: 'acceptAdm',
                    clickState: false
                },
                {
                    code: 'treatmentrecord',
                    plain: true,
                    desc: '咨询记录',
                    url: '/pages/myConsultRoom/page/treatmentrecord/treatmentrecord',
                    callFun: 'acceptAdm',
                    clickState: false
                },
                {
                    code: 'cancel',
                    plain: true,
                    desc: '取消接诊',
                    url: '',
                    callFun: 'handleCancelAdm',
                    clickState: false
                },
                {
                    code: 'reject',
                    plain: true,
                    desc: '拒绝接诊',
                    url: '/pages/myConsultRoom/page/refusal/refusal',
                    callFun: 'handleRejectAdm',
                    clickState: false
                },
                {
                    code: 'enterRoom',
                    plain: false,
                    desc: '开始接诊',
                    url: '/pages/myConsultRoom/page/treatment/treatment',
                    callFun: 'acceptAdm',
                    httpCode: '03100060',
                    clickState: false
                },
                // {
                //   code: "call",
                //   plain: false,
                //   desc: "开始呼叫",
                //   url: "/pages/myConsultRoom/",
                //   callFun: "acceptAdm",
                //   httpCode: "03100058"
                // },
                {
                    code: 'vediotreatRecord',
                    plain: false,
                    desc: '诊疗记录',
                    url: '/pages/myConsultRoom/page/treatment/treatment',
                    callFun: 'acceptAdm',
                    httpCode: '03100060',
                    clickState: false
                },
                {
                    code: 'cancelFinish',
                    plain: false,
                    desc: '取消结束',
                    url: '',
                    callFun: 'cancelFinish',
                    httpCode: '03100125',
                    clickState: false
                }
            ],

            dateBtnSize: 'mini',
            clickState: false,
            imgDomain: httpconfig.imgDomain,
            showRefusal: false
        };
    },
    /**
     * 组件的属性列表
     */
    props: {
        serviceTypeCode: {
            type: String,
            default: 'video'
        },
        serviceTypeVisible: {
            type: Boolean,
            default: true
        },
        admRecord: {
            type: Object,
            // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
            default: () => ({}),
            // 属性初始值（可选），如果未指定则会根据类型选择一个
            observers: function (newVal, oldVal) {
                // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
                // 通常 newVal 就是新设置的数据， oldVal 是旧数据
                this.item = newVal;
            }
        },
        admTypeCode: {
            type: String,
            default: ''
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        // 组件所在页面的生命周期函数
        handlePageShow: function () {},

        handlePageHide: function () {},
        handlePageResize: function () {},

        /**
         * 动态派发按钮点击事件
         */
        parseEventDynamicCode: function (e, callFun) {
            if (typeof this[callFun] === 'function') {
                this[callFun](e);
            }
        },

        /**
         * 点击卡片信息区域，跳转到详情页
         */
        onCardTap: function () {
            try {
                Cache.set('patientData', this.admRecord);
            } catch (error) {
                console.log('onCardTap Cache.set error', error);
            }
            uni.navigateTo({
                url: '/pages/myConsultRoom/page/graphicDateil/graphicDateil'
            });
        },

        // 此处attached的声明会被lifetimes字段中的声明覆盖
        ready: function (options) {
            this;
            // console.log('admrecord:',this.properties.admRecord)
        },

        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () {},

        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () {},

        /** 点击取消接诊 */
        handleCancelAdm: function (event) {
            if (this.clickState) {
                return;
            }
            this.setData({
                clickState: true
            });
            let { admid } = event.target.dataset;
            let userData = Cache.get('userData');
            let data = {
                params: [
                    {
                        // "locID": userData.locID,
                        hospID: userData.hospID,
                        userID: userData.userID,
                        admID: admid
                    }
                ]
            };
            $http.post(
                'urlDeault',
                this,
                {
                    code: '03100107',
                    data: data,
                    success: function (res) {
                        this.setData({
                            clickState: false
                        });
                        let icon = 'none';
                        if (res.errorCode == '0') {
                            icon = 'success';
                            uni.showToast({
                                title: res.errorMessage,
                                icon: icon
                            });
                            this.$emit('cancelAdm', {
                                detail: {}
                            });
                        } else {
                            this.setData({
                                clickState: false
                            });
                            Dialog.alert({
                                title: '提示',
                                message: res.errorMessage,
                                context: this
                            });
                        }
                    }
                },
                {
                    error: 'Y'
                }
            );
        },

        /** * 点击取消接诊 */
        handleRejectAdm: function (event) {
            // console.log("取消接诊", event)
            let myEventDetail = {
                admID: event.currentTarget.dataset.admid,
                patientData: event.currentTarget.dataset.patient
            };
            let myEventOption = {};
            this.$emit(
                'rejectAdm',
                {
                    detail: myEventDetail
                },
                myEventOption
            );
            this.setData({
                showRefusal: true
            });
        },

        colseRefuse: function () {
            this.setData({
                showRefusal: false
            });
        },

        /** * 接诊 */
        /** * 跳转详情 */
        acceptAdm: Util.debounce(function (event) {
            Cache.set('patientData', event.currentTarget.dataset.patient);
            var that = this;
            console.log(event);
            let { admid, statuscode, patient, btndata } = event.target.dataset;
            let admType = '';
            let admTypeCode = patient.admTypeCode;
            let btnCode = btndata.code;
            if (admTypeCode == 'W') {
                admType = 'video';
            }
            let TIMInfo = Cache.get('TIMInfo');
            if (TIMInfo.loginStatus == 'N' && (btnCode == 'enterRoom' || btnCode == 'call')) {
                this.$emit('offLine');
                return;
            }
            if (admid !== '') {
                if (btnCode == 'enterRoom') {
                    let userData = Cache.get('userData');
                    let data = {
                        params: [
                            {
                                locID: userData.locID,
                                hospID: userData.hospID,
                                userID: userData.userID,
                                admID: admid
                            }
                        ]
                    };
                    $http.post('urlDeault', this, {
                        code: btndata.httpCode,
                        data: data,
                        success: function (res) {
                            if (res.errorCode == '0') {
                                uni.navigateTo({
                                    url: btndata.url + '?admtype=' + admType
                                });
                                try {
                                    Cache.set('patientData', patient);
                                } catch (error) {
                                    console.log('CatchClause', error);
                                    console.log('CatchClause', error);
                                    console.log(error);
                                }
                            } else {
                                uni.showToast({
                                    title: res.result.errorMessage
                                });
                                return;
                            }
                        }
                    });
                } else if (btnCode == 'call') {
                    Cache.set('patientData', patient);
                    let path = '/pages/myConsultRoom/page/trtcRoom/trtcRoom';
                    messageUtil.allowToJoin(path);
                } else if (btnCode == 'vediotreatRecord') {
                    uni.navigateTo({
                        url: btndata.url + '?admtype=' + admType + '&record=1'
                    });
                } else {
                    uni.navigateTo({
                        url: btndata.url + '?admtype=' + admType
                    });
                    try {
                        Cache.set('patientData', patient);
                    } catch (error) {
                        console.log('CatchClause', error);
                        console.log('CatchClause', error);
                        console.log(error);
                    }
                }
            }
        }),

        /*取消结束*/
        cancelFinish: function (event) {
            let admID = event.currentTarget.dataset.admid;
            commonUtil.cancelFinish(admID, () => {
                this.$emit('cancelAdm', {
                    detail: {}
                });
            });
        }
    },
    /**
     * 呼叫、接诊
     */
    ChangeAdmSatus: function (code, admID) {
        let returnFlag = false;
        let userData = Cache.get('userData');
        let data = {
            params: [
                {
                    locID: userData.locID,
                    hospID: userData.hospID,
                    userID: userData.userID,
                    admID: admID
                }
            ]
        };
        $http.post('urlDeault', this, {
            code: code,
            data: data,
            success: function (res) {
                if (res.errorCode == '0') {
                    returnFlag = true;
                } else {
                    uni.showToast({
                        title: res.result.errorMessage
                    });
                }
            }
        });
        return returnFlag;
    },
    mounted() {
        // 处理小程序 attached 生命周期
        this.attached();
        // 处理小程序 ready 生命周期
        this.$nextTick(() => this.ready());
    },
    moved: function () {},
    destroyed: function () {},
    created: function () {}
};
</script>
<style>
/* pages/graphicConsult/graphicConsult.wxss */
Page {
    background-color: #f4f7fb;
}
.page-body {
    display: flex;
    flex-direction: column;
    border-radius: 18rpx;
    margin: 18rpx 24rpx;
    background-color: #ffffff;
    box-shadow: 0 8rpx 24rpx rgba(15, 23, 42, 0.06);
    overflow: hidden;
}
.body-item-status {
    display: flex;
    border-bottom: 1rpx solid #edf1f6;
    align-items: center;
    min-height: 88rpx;
    padding: 0 24rpx;
    box-sizing: border-box;
}
.scroll-view-item-status {
    /* align-self: flex-end; */
    justify-content: flex-start;
    font-size: 30rpx;
    margin: 0;
    font-weight: 600;
}
.noOverTime {
    color: #0079fe;
}
.view-item-baseInfo-surplusTime {
    /* align-items: flex-end; */
    font-size: 27rpx;
    margin-top: 0;
    flex: 1;
    text-align: right;
    padding-right: 0;
    color: #3f4a5a;
}
.noStart {
    color: #ff9933;
}

.scroll-view-item-Info {
    display: flex;
    flex-direction: row;
    font-size: 14px;
    padding: 24rpx 32rpx 10rpx;
    align-items: center;
}
.scroll-view-item-image {
    width: 92rpx;
    height: 92rpx;
    border-radius: 50%;
    background: #edf4ff;
}
.view-item-admInfo {
    display: flex;
    flex-direction: column;
    margin-left: 22rpx;
    min-width: 0;
    flex: 1;
}
.view-item-baseInfo {
    font-size: 32rpx;
    display: flex;
    flex-direction: row;
    align-items: center;
    color: #1f2937;
    line-height: 46rpx;
    min-width: 0;
}

.view-item-baseInfo > view:first-child {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.view-item-baseInfo-sex {
    margin-left: 12rpx;
    margin-top: 0;
    flex: 0 0 auto;
}
.view-item-baseInfo-sex-image {
    height: 36rpx;
    width: 36rpx;
}
.view-item-baseInfo-age {
    margin-top: 0;
    flex: 0 0 auto;
}
.view-item-baseInfo-ageText {
    background-color: #13c2c2;
    color: #ffffff;
    font-size: 28rpx;
    margin-left: 12rpx;
    height: auto;
    width: auto;
    min-width: 62rpx;
    padding: 2rpx 8rpx;
    border-radius: 6rpx;
}
.scroll-view-item-timeRang {
    font-size: 28rpx;
    color: #7b8794;
    line-height: 42rpx;
    margin-top: 4rpx;
}
.view-item-symptom {
    font-size: 30rpx;
    background-color: transparent;
    margin: 14rpx 32rpx 0;
    padding-left: 0;
}
.view-item-admType {
    min-height: 78rpx;
    height: auto;
    line-height: 78rpx;
    border-top: 1px solid #edf1f6;
    border-bottom: 1px solid #edf1f6;
    display: flex;
    flex-direction: row;
    font-size: 30rpx;
    margin-top: 18rpx;
}
.view-item-admType-label {
    padding-left: 26rpx;
}
.view-item-admType-desc {
    /* font-weight: Regular;  */
    color: #ff9933;
    /* margin-left: 390rpx; */
    flex: 1;
    text-align: right;
    padding-right: 23rpx;
}

.btn-view {
    margin-top: 18rpx;
    margin-bottom: 24rpx;
    margin-right: 24rpx;
    margin-left: 24rpx;
    align-self: flex-end;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 14rpx;
}
.btn-view-botBtn {
    font-size: 26rpx !important;
    color: #ffffff !important;
    /* border: 1px solid #0079FE !important; */
    margin-left: 0 !important;
    background-color: #0079fe !important;
    padding: 0 25rpx !important;
    border-radius: 999rpx !important;
    min-width: 148rpx !important;
    height: 62rpx !important;
    line-height: 62rpx !important;
    /* margin-left: 34px; */
}
.btn-view-botBtn-detail {
    background-color: #fff !important;
    color: #0079fe !important;
    border: 1px solid #0079fe !important;
    margin-left: 0 !important;
}
.btn-view-botBtn-reject {
    color: #f5222d !important;
    background-color: #ffffff !important;
    border: 1px solid #f5222d !important;
    margin-left: 0 !important;
}
.btn-view-botBtn-enterRoom {
    background-color: #fa8c16 !important;
    color: #ffffff !important;
    margin-left: 0 !important;
}
.btn-view-botBtn-call {
    /* background-color: #FA8C16 !important; */
    color: #ffffff !important;
    margin-left: 9rpx !important;
}
.btn-view-botBtn-treatmentrecord {
    background-color: white !important;
    color: #0079fe !important;
    margin-left: 0 !important;
    border: 1px solid #e9e9e9 !important;
}
.personDesc {
    min-height: 70rpx;
    height: auto;
    /* width: calc(100% - 150rpx); */
    color: #1f2937;
    /* font-size: 30rpx; */
    /* margin: 20rpx 35rpx; */
    background: #f7f9fc;
    white-space: normal;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 44rpx;
    padding: 14rpx 18rpx;
    border-radius: 10rpx;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}
</style>
