<template>
  <div>
    <div class="top-nav flex-vertical-between">
      <div class="title flex-center" v-if="innerWidth>768">
        <img v-if="logo" :src="logo" alt />
        {{title}}
      </div>
      <div class="title flex-center" v-else>
        <img v-if="logo" :src="logo" alt />
        {{titleSplit[0]}}<br />{{titleSplit[1]}}
      </div>
      <div v-if="ai_url">
        <AIModal :url="ai_url">
          <template #default="{ handle }">
            <svg @click="handle()" class="ai" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="32" height="32">
              <path d="M501.824 32C303.552 32 141.504 176.992 141.504 357.76c0 23.712 2.816 47.104 8.32 69.856l-51.008 114.208a32 32 0 0 0 24.704 44.736c54.272 7.744 76.672 31.168 76.672 77.312v111.552a64 64 0 0 0 64 64h20.704a64 64 0 0 1 64 64V960a32 32 0 0 0 32 32h345.6a32 32 0 0 0 0-64h-313.6v-24.608a128 128 0 0 0-128-128h-20.736v-111.552c0-65.664-32.192-110.688-91.2-131.136l39.872-89.28a31.968 31.968 0 0 0 1.568-21.792 233.088 233.088 0 0 1-8.896-63.904c0-143.712 131.936-261.76 296.32-261.76s296.32 118.016 296.32 261.76a32 32 0 0 0 64 0C862.144 176.992 700.064 32 501.824 32zM904 448a32 32 0 0 0-32 32v360a32 32 0 0 0 64 0V480a32 32 0 0 0-32-32z" fill="#ffffff"></path>
              <path d="M673.888 466.656c-11.744-25.568-48.416-24.64-58.816 1.536l-132.8 333.76a32 32 0 0 0 59.488 23.68l32.608-81.92c0.576 0.032 1.088 0.32 1.664 0.32h154.848l38.176 83.104a31.968 31.968 0 1 0 58.144-26.72l-153.312-333.76zM599.68 680l47.264-118.72 54.528 118.72H599.68z" fill="#ffffff"></path>
            </svg>
          </template>
        </AIModal>
      </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.id">{{item.title}}
                </el-menu-item>

      </el-menu>-->
      <div class="main-menu flex flex-row-center flex-col-center" v-if="navlist.length>0">
        <div
          :class="['menu-item',active==index?'active':'']"
          @click="handleSelect1(index)"
          v-for="(item,index) in navlist"
          :key="index"
        >{{item.title}}</div>
      </div>
      <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" v-if="navlist.length>0">
      <!-- <div class="left-nav-tip">主页/{{activeName}}</div> -->
      <div class="left-nav-tip">{{navlist[active].title}}/{{$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].list"
          :key="index"
          :class="item.id==activeID?'is-active':''"
          @click="choose(item.id,item.jump,item.title)"
        >{{item.title}}</div>
      </div>
      <!-- 用户信息 -->
      <div class="user-info flex-column flex-center" v-if="getPatiensInfo!=null  ">
        <div @click="patientIn" class="flex-vertical-center-r">
          <img src="../assets/h-avatar.png" alt />
          <div v-if=" Object.keys(getPatiensInfo).length>0">
            <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"
              :page-size="11"
              :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";
import AIModal from '@/components/AIModal.vue';
export default {
  components: {
    AIModal,
    pageProup,
    outpatientRecord
  },
  data() {
    return {
      title: window.SIX_config.title,
      logo: window.SIX_config.logo,
      ai_url: window.SIX_config.AI_URL,
      activeName: "",
      active: "0",
      // 修改一级目录名称时 要同步修改 router 里面的 值
      navlist: [],
      activeID: "1-1", // 默认值 从缓存取
      patiensMsg: null, // 病人信息
      showCase: false,
      caseItem: null,

      page: 1,
      list: [],
      total: 0,
      innerWidth: 1920
    };
  },
  created() {
    this.activeID = this.getActiveID;
    this.active = this.getActive;
    this.navlist = [];
    this.getMenuPermiss();
    this.innerWidth = window.innerWidth;
  },
  methods: {
    // 分页改变
    pageChange(e) {
      this.page = e;
      this._getPatiensVisitList(this.getPatiensInfo.basisPatient.pid);
    },
    // 鼠标进入
    patientIn() {
      this._getPatiensBasisM();
    },
    // 就诊记录点击
    handleRecord(item) {
      // this.showCase = true

      this._getRecordDetail(item.id);
    },
    // 关闭就诊 详情
    closeCase() {
      this.showCase = false;
    },
    handleSelect1(index) {
      this.active = index;
      this.setActive(index);
      this.activeID = this.navlist[index].list[0].id;
      this.setActiveID(this.navlist[index].list[0].id);
      this.activeName = this.navlist[index].list[0].title;
      if (this.navlist[index].list[0].jump == this.$route.path) return;
      this.$router.push({
        path: this.navlist[index].list[0].jump
      });
    },

    // 二级分类选中
    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;
        // res.Data[5].list.push({
        //     id: "6-6",
        //     jump: "/index/patientData",
        //     name: "patientData",
        //     title: "患者分析",
        //     type: "page",
        // })
        // res.Data[5].list.push({
        //     id: "6-7",
        //     jump: "/index/dosage",
        //     name: "dosage",
        //     title: "中药用量分析",
        //     type: "page",
        // })

        this.navlist = res.Data;
        this.navlist.forEach((item, index) => {
          item.list.forEach(item1 => {
            if (this.$route.path == item1.jump) {
              this.activeID = item1.id;
              this.activeName = item1.title;
              this.active = String(index);
              this.setActive(this.active);
              this.setActiveID(this.activeID);
            }
          });
        });
        // console.log(this.navlist, '导航')
        this.$forceUpdate();
        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: {
      deep: true,
      immediate: true,
      handler: function(to, from) {
        this.navlist.forEach((item, index) => {
          item.list.forEach(item1 => {
            if (to.path == item1.jump) {
              this.activeID = item1.id;
              this.activeName = item1.title;
              this.active = String(index);
              this.setActive(this.active);
              this.setActiveID(this.activeID);
            }
          });

          if (to.path == "/") {
            this.logOut();
          }
        });
      },
      immediate: true,
      deep: true
    },

    getPatiensInfo: {
      deep: true,
      immediate: true,
      handler: function() {
        if (!this.getPatiensInfo) return;
        if (Object.keys(this.getPatiensInfo).length > 0) {
          this._getPatiensBasisM();
        }
      }
    },
    getActiveID: {
      deep: true,
      immediate: true,
      handler: function() {
        this.activeID = this.getActiveID;
      }
    }
  },
  computed: {
    titleSplit() {
      const suffix = `辅助诊疗系统`
      const values = this.title.split(suffix);
      values[1] = values.length === 2 ? suffix + values[1] : '';
      return values;
    },
    ...mapGetters(["getuserinfo", "getPatiensInfo", "getActiveID", "getActive"])
  }
};
</script>
<style lang="scss" scoped>
@import "../style/common.scss";

.main-menu {
  flex: 1;
  // background: red;
  height: 100%;
  margin: 0 10px;

  .menu-item {
    height: 100%;
    display: flex;
    align-items: center;
    padding: 0 10px;
    cursor: pointer;
    color: #fff;
  }

  .active {
    background: rgba($color: #ffffff, $alpha: 0.2);
  }
}

.top-nav {
  svg.ai {
    cursor: pointer;
    &:hover {
      path {
        fill: #f56c6c;
      }
    }
  }
  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: 0px 32px;
    font-size: 18px;
    box-sizing: border-box;
    font-family: PingFang SC;
    font-weight: bold;
    color: #ffffff;
    // padding: 5px 0;
    cursor: pointer;
    // position: absolute;
    // left: 0;
    // top: 0;
    // bottom: 0;

    img {
      width: 45px;
      margin-right: 10px;
    }
  }

  .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>
<style scoped lang="scss">
@media screen and (max-width: 1280px) {
  .main-menu {
    .menu-item {
      padding: 0 5px;
      font-size: 12px;
    }
  }
}

@media screen and (max-width: 768px) {
  .main-menu {
    .menu-item {
      padding: 0 5px;
    }
  }

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

    .title {
      background: #5386f6;
      // height: 60px;
      height: 50px;
      padding: 0 6px;
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: bold;
      color: #ffffff;
      cursor: pointer;

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

    .admin {
      width: 94px;
    }
  }

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

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

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

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

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

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

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

      .is-active {
        color: #5386f6;
      }
    }
  }
}
</style>