<template>
  <div>
    <div class="top-nav flex-vertical-between">
      <div class="title flex-center">
        <!-- <img src="../assets/logo-white.png" alt="" /> -->
        中医智能辅助诊疗系统
      </div>
      <el-menu
        :default-active="active"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        background-color="#5386F6"
        text-color="#fff"
        active-text-color="#fff"
        active-background-color="#fff"
      >
        <el-menu-item v-for="(item,index) in navlist" :key="index" :index="item.index">{{item.name}}</el-menu-item>
      </el-menu>
      <div class="admin">
        <span class="name">{{getuserinfo.username}}</span>
        <img src="../assets/down-header.png" alt class="icon" />

        <div class="hover-show">
          <!-- <div class="flex-center">

                        <span>{{getuserinfo.username}}</span>
          </div>-->
          <div class="flex-center" @click="$router.push({path:'/index/help'})">
            <span>修改密码</span>
          </div>
          <div class="flex-center" @click="$router.push({path:'/index/help'})">
            <span>帮助</span>
          </div>
          <div class="flex-center" @click="logOut">
            <span>退出登录</span>
          </div>
        </div>
      </div>
    </div>
    <div class="header-bottom flex-center">
      <!-- <div class="left-nav-tip">主页/{{activeName}}</div> -->
      <div class="left-nav-tip">{{navlist[active].name}}/{{$route.meta.title}}</div>
      <!-- <div class="left-nav-tip">navlist[active].name/{{navlist[active].child}}</div> -->
      <div class="tab-bar flex-center">
        <div
          v-for="(item,index) in navlist[active].child"
          :key="index"
          :class="item.id==activeID?'is-active':''"
          @click="choose(item.id,item.path,item.name)"
        >{{item.name}}</div>
      </div>
      <!-- 用户信息 -->
      <div class="user-info flex-column flex-center" v-if="getPatiensInfo!=null || getPatiensInfo ">
        <div @click="patientIn" class="flex-vertical-center-r">
          <img src="../assets/h-avatar.png" alt />
          <div>
            <span style="margin-right:27px;">
              {{getPatiensInfo.patientidSelsourceName}}&nbsp;&nbsp;
              {{getPatiensInfo.sex}}&nbsp;&nbsp;
              {{getPatiensInfo.basisPatient.age}}岁&nbsp;&nbsp;
              {{getPatiensInfo.pregnancy==2?'妊娠期':''}}&nbsp;&nbsp;
              {{getPatiensInfo.breastfeeding==1?'哺乳期':''}}
            </span>
            <div style="font-size:14px;">{{getPatiensInfo.code}}</div>
          </div>
        </div>
        <!-- <div class="flex-vertical-center-r">
                    <img src="../assets/h-guahao.png" alt="">
                    <span>{{getPatiensInfo.code}}</span>
        </div>-->

        <!-- 就诊记录列表 -->
        <div class="seeRecord" v-if="patiensMsg">
          <div class="seeRecord-header">就诊记录</div>
          <div class="list">
            <div
              class="seeRecord-item"
              @click="handleRecord(item)"
              v-for="(item,index) in list"
              :key="index"
            >
              {{item.timeStr}}
              <span
                v-if="item.illnessName"
              >{{item.illnessName}}-{{item.symptomType}}</span>
            </div>
          </div>
          <div class="block flex flex-col-center flex-row-center">
            <el-pagination layout="prev, pager, next" :total="total" @current-change="pageChange"></el-pagination>
          </div>
        </div>
      </div>
    </div>

    <pageProup
      @cancle="closeCase"
      :showDialog="showCase"
      :showBtns="false"
      title
      width="70%"
      distanceTop="5vh"
    >
      <div slot="body">
        <outpatientRecord :info="caseItem"></outpatientRecord>
      </div>
    </pageProup>
  </div>
</template>
<script>
import { mapState, mapGetters, mapActions, mapMutations } from "vuex";
import { menus } from "@/utils/menu.js";
import { getMenuPermiss } from "@/api/system.js";
import { getPatiensBasisM, getRecordDetail } from "@/api/diagnosis";
import { getPatiensVisitList } from "@/api/patients.js";
import pageProup from "@/components/Propup";
import outpatientRecord from "@/components/ui/outpatientRecords";
export default {
  components: {
    pageProup,
    outpatientRecord
  },
  data() {
    return {
      activeName: "",
      active: "0",
      // 修改一级目录名称时 要同步修改 router 里面的 值
      navlist: menus,
      activeID: "1-1", // 默认值 从缓存取
      patiensMsg: null, // 病人信息
      showCase: false,
      caseItem: null,

      page: 1,
      list: [],
      total: 0
    };
  },
  created() {
    this.activeID = this.getActiveID;
    this.active = this.getActive;

    this.getMenuPermiss();
  },
  methods: {
    // 分页改变
    pageChange(e) {
      this.page = e;
      this._getPatiensVisitList();
    },
    // 鼠标进入
    patientIn() {
      this._getPatiensBasisM();
    },
    // 就诊记录点击
    handleRecord(item) {
      // this.showCase = true

      this._getRecordDetail(item.id);
    },
    // 关闭就诊 详情
    closeCase() {
      this.showCase = false;
    },
    handleSelect(key, keyPath) {
      // console.log(key, keyPath);
      this.active = key;
      this.setActive(key);
      this.activeID = this.navlist[key].child[0].id;
      this.setActiveID(this.navlist[key].child[0].id);
      this.activeName = this.navlist[key].child[0].name;
      if (this.navlist[key].child[0].path == this.$route.path) return;
      this.$router.push({
        path: this.navlist[key].child[0].path
      });
    },
    // 二级分类选中
    choose(id, path, name) {
      this.activeID = id;
      this.activeName = name;
      this.setActiveID(id);
      if (path == this.$route.path) return;
      this.$router.push({
        path: path
      });
    },
    // 退出登录
    logOut() {
      this.setActive("0");
      this.setActiveID("1-1");
      this.setPatiensInfo({});
      this.setUserinfo({});

      localStorage.clear();
      sessionStorage.clear();

      this.$router.replace({
        path: "/"
      });
    },
    async getMenuPermiss() {
      let res = await getMenuPermiss();
      if (res.ResultCode == 0) {
        let arr = [];
        if (res.Data.length == 0) return;

        this.navlist = res.Data;
        return;
        res.Data.forEach((item, index) => {
          let obj = {
            index: String(index),
            name: item.title,
            child: []
          };

          item.list.forEach((item1, index1) => {
            let obj1 = {
              id: item1.id,
              name: item1.title,
              path: item1.jump
            };

            obj.child.push(obj1);
          });

          arr.push(obj);
        });

        this.navlist = arr;
      }
    },
    // 获取患者己本信息
    async _getPatiensBasisM() {
      let res = await getPatiensBasisM({
        patientId: this.getPatiensInfo.pid
      });
      if (res.ResultCode == 0) {
        // res.Data.recordLength = res.Data.patientRecord.length;

        this.patiensMsg = res.Data;
        this._getPatiensVisitList(this.getPatiensInfo.basisPatient.pid);
      }
    },
    // 获取就诊详情
    async _getRecordDetail(id) {
      let res = await getRecordDetail({
        id: id
        // id: 'e68d293d-f1b7-4f6d-bed4-42b1aba6973e'
      });
      if (res.ResultCode == 0) {
        this.caseItem = res.Data;
        this.showCase = true;
      } else {
        this.$message.error(res.ResultInfo);
      }
    },
    // 获取就诊记录
    async _getPatiensVisitList(id) {
      let res = await getPatiensVisitList({
        patientId: id,
        page: this.page,
        limit: 11
      });
      if (res.ResultCode == 0) {
        this.list = res.Data.Items;
        this.total = res.Data.TotalRecordCount;
      }
    },
    ...mapMutations({
      setActiveID: "nav/setActiveID",
      setActive: "nav/setActive",
      setPatiensInfo: "user/setPatiensInfo",
      setUserinfo: "user/setUserinfo"
    })
  },
  watch: {
    $route: {
      handler: function(to, from) {
        this.navlist.forEach((item, index) => {
          // if (to.meta.pftitle == item.name) {

          item.child.forEach(item1 => {
            if (to.path.indexOf(item1.path) != -1) {
              this.activeID = item1.id;
              this.activeName = item1.name;
              this.active = String(index);
              this.setActive(this.active);
              this.setActiveID(this.activeID);
            }
          });
          // }

          if (to.path == "/") {
            this.logOut();
          }

          // return
          // if (to.path == '/index/physical' && this.getPatiensInfo.patientidSelsourceName &&
          //     from
          //     .name != 'physicald') {
          //     this.$router.push({
          //         path: '/index/physicald?pid=' + this.getPatiensInfo.basisPatient.pid
          //     })

          // }
        });
      },
      immediate: true,
      deep: true
    },

    getPatiensInfo: {
      deep: true,
      immediate: true,
      handler: function() {
        this._getPatiensBasisM();
      }
    }
  },
  computed: {
    ...mapGetters(["getuserinfo", "getPatiensInfo", "getActiveID", "getActive"])
  }
};
</script>
<style lang="scss" scoped>
@import "../style/common.scss";

.top-nav {
  background: #5386f6;
  padding: 0 24px 0 0;
  // padding: 0px;
  // height: 60px;
  height: 50px;
  // position: relative;
  box-sizing: border-box;

  .title {
    background: #5386f6;
    // height: 60px;
    height: 50px;
    padding: 0 32px;
    font-size: 20px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #ffffff;
    cursor: pointer;
    // position: absolute;
    // left: 0;
    // top: 0;
    // bottom: 0;

    img {
      width: 40px;
      margin-right: 20px;
    }
  }

  .admin {
    width: 292px;
    // background: red;
    font-size: 14px;
    height: 32px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #ffffff;
    cursor: pointer;
    text-align: right;
    // position: absolute;
    line-height: 32px;
    // right: 0px;
    // top: 18px;
    // bottom: 0;

    .name {
      margin-right: 10px;
    }

    .icon {
      width: 12px;
    }

    .hover-show {
      display: none;
      position: absolute;
      top: 27px;
      right: 0px;
      z-index: 999;
      width: 120px;
      // height: 300px;

      box-sizing: border-box;
      background: #ffffff;
      padding: 10px 0;
      border-radius: 8px;
      box-shadow: 2px 2px 15px rgba($color: #000000, $alpha: 0.2);

      div {
        // padding: 0px 20px 0;
        height: 40px;
        // line-height: 50px;
      }

      span {
        color: #000;
      }
    }
  }

  .admin:hover {
    .hover-show {
      display: block;
    }
  }

  .hover-show:hover {
    display: block;
  }
}

.header-bottom {
  background: #fff;
  // height: 50px;
  height: 40px;
  position: relative;

  .left-nav-tip {
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #333333;
    position: absolute;
    left: 5px;
  }

  .user-info {
    position: absolute;
    right: 15px;
    cursor: pointer;

    img {
      width: 25px;
      margin-right: 8px;
    }

    span {
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #333333;
    }
  }

  .user-info:hover .seeRecord {
    display: block;
  }

  .tab-bar {
    div {
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #666666;
      margin-right: 25px;
      cursor: pointer;
    }

    .is-active {
      color: #5386f6;
    }
  }
}

.seeRecord {
  display: none;
  width: 350px;
  height: 400px;
  position: absolute;
  top: 35px;
  right: -18px;
  background: #fff;
  z-index: 9999;
  border: 1px solid #ebeef5;
  // overflow-y: auto;
  overflow: hidden;

  .list {
    height: 320px;
    overflow-y: auto;
    // background: red;
  }

  &-header {
    height: 40px;
    background: #5386f6;
    line-height: 40px;
    font-size: 18px;
    color: #fff;
    text-align: center;
  }

  &-item {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    color: #000;
    font-size: 16px;
    margin-top: 10px;
    cursor: pointer;
    padding: 0 10px;
  }
}

.el-menu--horizontal > .el-menu-item {
  border-bottom: 0 !important;
}

.el-menu--horizontal > .el-menu-item.is-active {
  background: #2bab9b !important;
}

.el-menu-demo > ul > li {
  font-size: 16px !important;
}

.top-nav ::v-deep .el-menu-item {
  font-size: 16px;
  // font-weight: bold;
}

.top-nav ::v-deep .el-menu-item {
  padding: 0 5px;
}
</style>