<template>
  <div class="home">
    <nav-bar />

    <!-- 科室索引列表 -->
    <van-index-bar
      :index-list="indexList"
      class="custom-index-bar"
    >
      <div v-for="letter in indexList" :key="letter">
        <van-index-anchor :index="letter" />
        <van-cell
          v-for="item in groupedDepartments[letter]"
          :key="item.DEPARTCODE"
          :title="item.DEPARTNAME"
          is-link
          @click="handleGuke(item)"
        />
      </div>
    </van-index-bar>
  </div>
</template>

<script>
// 脚本部分保持不变
import { pinyin } from 'pinyin-pro';
import { apiGetAppointDeptMark, getUserCategory, getMedicalNotice } from "@/request/api.js";
import { Dialog, Toast, IndexBar, IndexAnchor, Cell } from "vant";

export default {
  components: {
    [IndexBar.name]: IndexBar,
    [IndexAnchor.name]: IndexAnchor,
    [Cell.name]: Cell
  },
  data() {
    return {
      list: [],
      yuyue: {
        t: "",
        k: "",
        e: "",
        z: 2,
      },
      groupedDepartments: {},
      indexList: [],
      currentLetter: '',
      showNotice: false, // 修改默认值为false
      noticeContent: ""
    };
  },
  mounted() {
    // 删除loadMedicalNotice调用
    // this.getUserCategoryToHis();
    this.getList(); // 直接调用getList()
  },
  methods: {
    // 删除loadMedicalNotice方法
    // 删除getDefaultNotice方法
    // 删除handleConfirmNotice方法
    // 删除handleBeforeClose方法
    
    getUserCategoryToHis() {
      let card = sessionStorage.getItem("card");
      if (!card) {
        setTimeout(() => {
          Toast("未查询到用户身份，请先绑定就诊人！！");
          this.$router.push({ path: "/Member_jzr" });
        }, 500);
        return;
      }
      let cardJson = JSON.parse(card);
      getUserCategory({
        queryCode: cardJson.patientId,
        queryType: 0,
      }).then(res => {
        cardJson.chargetype = res.data;
        sessionStorage.setItem("card", JSON.stringify(cardJson));
      }).catch(err => {
        Toast(err.message);
      });
    },
    getList() {
      let params = {
        searchType: '2',
        departCode: '',
        departLevel: "",
        departType: '0',
      };
      apiGetAppointDeptMark(params).then(res => {
        this.list = [];
        try {
          let jsonObj = this.$x2js.xml2js(res.data);
          let jsonObj2 = this.$x2js.xml2js(
            jsonObj.Envelope.Body.MOP_OutpRegisterDeptQueryResponse
              .MOP_OutpRegisterDeptQueryResult
          );
          let tempList = jsonObj2.response.data.data_row || [];
          tempList.forEach(e => {
            if (e.DEPARTLEVEL === '1') this.list.push(e);
          });
          this.groupDepartments();
        } catch (error) {
          Toast("解析科室数据失败");
        }
      }).catch(err => {
        Toast("获取科室列表失败");
      });
    },
    groupDepartments() {
      const groups = {};
      this.list.forEach(item => {
        // 过滤掉包含"军人"和"小儿科门诊"的科室
        if (item.DEPARTNAME && (item.DEPARTNAME.includes('军人') || item.DEPARTNAME.includes('小儿科门诊'))) {
          return; // 跳过这些科室
        }
        
        let firstLetter = '';
        if (item.DEPARTNAME) {
          firstLetter = pinyin(item.DEPARTNAME, {
            pattern: 'first',
            toneType: 'none'
          }).charAt(0).toUpperCase();
        }
        if (!/^[A-Z]$/.test(firstLetter)) firstLetter = '#';
        if (!groups[firstLetter]) groups[firstLetter] = [];
        groups[firstLetter].push(item);
      });
      const sortedLetters = Object.keys(groups).sort((a, b) => {
        if (a === '#') return 1;
        if (b === '#') return -1;
        return a.localeCompare(b);
      });
      sortedLetters.forEach(letter => {
        groups[letter].sort((a, b) => a.DEPARTNAME.localeCompare(b.DEPARTNAME));
      });
      this.groupedDepartments = groups;
      this.indexList = sortedLetters;
    },
    handleGuke(val) {
      this.yuyue.DeptCode = val.DEPARTCODE;
      this.yuyue.DeptName = val.DEPARTNAME;
      sessionStorage.setItem("yuyue", JSON.stringify(this.yuyue));
      // this.$router.push({ path: "/xzmz2" });
      this.$router.push({ path: "/Guke_pbcx" });
    }
  }
};
</script>

<style scoped lang="scss">
.home {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f5f5;
  overflow: hidden;
}

// 删除所有与notice-dialog相关的样式

// 索引栏样式（不变）
::v-deep .custom-index-bar {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;

  .van-index-bar__index-list {
    right: 4px;
    padding: 10px 0;
    background-color: rgba(212, 212, 212, 0.806);
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }

  .van-index-bar__index {
    width: 0.5rem;
    height: 0.5rem;
    line-height: 0.5rem;
    font-size: 0.4rem;
    color: #666;
    margin: 0.2rem 0;
    text-align: center;

    &.van-index-bar__index--active {
      color: #46c3b0;
      font-weight: bold;
      transform: scale(1.1);
    }
  }

  .van-index-anchor {
    height: 0.8rem;
    line-height: 0.8rem;
    padding: 0 0.3rem;
    background-color: #f0f0f0;
    color: #888;
    font-size: 0.28rem;
    font-weight: 500;
  }

  .van-cell {
    margin: 0;
    border-bottom: 1px solid #eee;
    background-color: #fff;
    .van-cell__title {
      font-size: 0.32rem;
    }
    .van-cell__right-icon {
      display: none !important;
    }
    &:last-child {
      border-bottom: none;
    }
  }
}
</style>