<template>
    <div class="department">
        <div class="departmentView">
            <div class="content">
                <div class="list" v-for="(item,index) in departmentLis" @click="goStart(index,item.recordTime)" :key="index">
                    <div class="top">
                        <p>挂号科室：{{item.hospitalDeptName}} <img src="../images/right.png" alt=""></p>
                    </div>
                    <div class="main">
                        <p>预约医院：{{item.sonHospitalName?item.sonHospitalName:item.hospitalName}}</p>
                        <p>预约时间：{{item.recordTime}}</p>
                        <p>姓名：{{item.patientName}}</p>
                        <p>病历号：{{item.recordId}}</p>
                        <p>排队号：{{item.registerNum}}</p>
                        <p v-if="item.dided">您已完成过一次预问诊，是否再次录入</p>
                    </div>
                </div>
            </div>
        </div>
        <Submit v-if="submit" :showType="showType" :fail="failMsg" @showSubmit="showSubmit"></Submit>
        <Tiptoast
            :show="showTip"
            @close="close"
            :data="message"/>
        <StartPage v-if="showStart" :showStart="showStart"/>
    </div>
</template>
<script>
    import {setScroll,compare} from '@utils/tools.js';
    import BScroll from 'better-scroll';
    import Submit from "../common/Submit";
    import Tiptoast from "../common/Tiptoast";
    import StartPage from "../common/HomePage";
    import api from "@utils/api.js";
    export default {
        name: "Department",
        data(){
            return {
                scroll:null,
                departmentLis:[],
                showTip: false,
                showType: "fail",
                failMsg: "",
                message:{
                    title: "",
                    text: ""
                },
                submit: false,
                show: false,
                son:false,//有无子医院
                hospitalNum:'',
                hospitalName:'',
                showStart:false,//显示启动页
            }
        },
        created(){
            let query = this.$route.query;
            let type = query.patientInfoType,name = query.patientName,value = query.patientInfo,code=query.hospitalCode,sonHospitalCode=query.sonHospitalCode,sonHospitalName=query.sonHospitalName,interId=query.interId;
            if(value&&name&&type&&code){//带信息直接跳转列表页面
                this.getSysConfig(code)
                const param = {
                    patientInfo: value,
                    patientInfoType: type,
                    hospitalCode: code,
                    patientName:name,
                    sonHospitalCode: sonHospitalCode||'',
                    sonHospitalName: sonHospitalName||'',
                    interId: interId
                };
                this.signInStr(param);
            }else{
                if(localStorage.getItem("startPage") !== null&&localStorage.getItem("startPage")==1){
                    this.showStart = true
                }
                this.departmentLis = this.sortPart();
            }
        },
        mounted(){
            this.$nextTick(()=>{
                let scroll = setScroll(BScroll,true,'.departmentView')
                this.scroll = scroll
            })
        },
        methods:{
            getSysConfig(code){
                api.getSysConfig({'hospitalCode':code}).then((res)=>{
                    const result = res.data;
                    if(result.code==0){
                        const datas = result.data;
                        this.$store.commit('saveSysConfig',datas)
                        for(let i = 0;i < datas.length;i++){
                            if(datas[i].code == 'homepage_show' && localStorage.getItem("startPage") === null){
                                if(+datas[i].value==1){
                                    localStorage.setItem('startPage',1)
                                    //显示启动页
                                    this.showStart = true
                                }else{
                                    localStorage.setItem('startPage',0)
                                }
                            }
                        }
                    }else{
                        this.defaultWaring(res.data.msg);
                    }
                })
            },
            signInStr(param){
                api.signIn(param).then(res => {
                    let result = res.data;
                    if (result.code == 0) {
                        // result.data.splice(0,1);//测试只有一条数据
                        localStorage.setItem('loginData',JSON.stringify(result.data))//保存登陆信息，扫码进入徐删除该参数
                        if (result.data.length > 1) {
                            this.departmentLis = this.sortPart();
                        } else if (result.data.length == 1) {
                            let msg = result.data[0];
                            let params = {
                                hospitalCode: msg.hospitalCode,
                                hospitalDeptCode: msg.hospitalDeptCode,
                                doctorCode: msg.doctorCode,
                                patientCode: msg.patientCode,
                                recordId: msg.recordId,
                                sonHospitalCode:msg.sonHospitalCode||'',
                                time:msg.recordTime&&(new Date((msg.recordTime).replace(/\-/g, "/"))).getTime()
                            };
                            localStorage.setItem('loginParam',JSON.stringify(params))//保存登陆信息，扫码进入删除该参数
                            this.$router.push({
                                path: "/home",
                                query: params
                            });
                        } else {
                            this.message.title = '温馨提示'
                            this.message.text = '暂无今日挂号信息，可更换登录方式再次尝试。'
                            this.showTip = true
                        }
                    } else {
                        this.defaultWaring(res.data.msg);
                    }
                }).catch(() => {
                    this.defaultWaring("网络异常请稍后重试");
                });
            },
            sortPart(){//按日期排序
                let tmpList = JSON.parse(localStorage.getItem('loginData'));
                let tmpTime = JSON.parse(localStorage.getItem('recordTime'));
                let sortArr = tmpList.sort(compare("recordTime"));
                if(tmpTime){
                    for(let j = 0;j < tmpTime.length;j++){
                        for(let i = 0;i < sortArr.length;i++){
                            if(sortArr[i].recordTime&&tmpTime[j] == (new Date((sortArr[i].recordTime).replace(/\-/g, "/"))).getTime()){
                                sortArr[i].dided = 1
                            }
                        }
                    }
                }
                return sortArr;
            },
            showSubmit(flg){
                this.submit = flg;
            },
            close() {
                this.showTip = false;
            },
            defaultWaring(msg) {
                this.$store.commit("handleToggleShow", false);
                this.showType = "fail";
                this.failMsg = msg;
                this.submit = true;
                let timer = setTimeout(() => {
                    this.submit = false;
                    clearTimeout(timer);
                }, 2000);
            },
            goStart(num,time){
                this.show = !this.show
                let msg = this.departmentLis[num]
                let params = {
                    hospitalCode:msg.hospitalCode,
                    hospitalDeptCode:msg.hospitalDeptCode,
                    doctorCode:msg.doctorCode,
                    patientCode:msg.patientCode,
                    recordId:msg.recordId,
                    sonHospitalCode:msg.sonHospitalCode||'',
                    time:time&&(new Date(time.replace(/\-/g, "/"))).getTime()
                }
                localStorage.setItem('loginParam',JSON.stringify(params))//保存登陆信息，扫码进入删除该参数
                this.$router.push({
                    path:'/home',
                    query:params
                })
            }
        },
        components:{
            Submit,
            Tiptoast,
            StartPage
        }
    }
</script>
<style lang="less" scoped>
@import '../less/base.less';
    .department {
        font-size: .3rem;
        padding: .3rem 0 1rem 0;
        background-color: #fff;
        .departmentView {
            position: absolute;
            width: 100%;
            height: 100%;
            overflow: hidden;
            padding: 0 .3rem 1rem .3rem;
            box-sizing: border-box;
            .content {
                padding-bottom: 1rem;
            }
            .list {
                background:rgba(255,255,255,1);
                box-shadow:0px 3px 20px 0px rgba(192,187,224,0.23);
                border-radius:.1rem;
                overflow: hidden;
                margin-bottom: .3rem;
            }
            .top {
                height: 1rem;
                line-height: 1rem;
                background: url('../images/bg-title.png') 100% 100% no-repeat;
                padding: 0 .3rem;
                box-sizing: border-box;
                p {
                    position: relative;
                    color: #fff;
                    font-size: #font[title];
                    img {
                        position: absolute;
                        right: 0;
                        top: 50%;
                        width: .4rem;
                        height: .4rem;
                        margin-top: -0.2rem;
                    }
                }
            }
            .main {
                font-size: .28rem;
                padding: .3rem;
                p {
                    line-height: .5rem;
                }
            }
        }
    }
</style>


