<template>
  <div class="MoveMain">
    <header class="headerMove">
      <img
        @click="handelJump(`/`)"
        src="../assets/mobile/login/LOGO.svg"
        alt="logo"
      />
      <!-- 页面名称 -->
      <span style="font-size: 0.9rem">{{ $t(key) }}</span>

      <!-- 菜单 -->
      <el-dropdown trigger="click" :hide-on-click="false">
        <span
          class="el-dropdown-link"
          style="font-size: 0.9rem; color: rgba(0, 0, 0, 1)"
        >
          <img src="../assets/mobile/login/menu.svg" alt="menu" />
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item style="font-size: 0.8rem; color: rgba(0, 0, 0, 1)">
            <div class="menuItem" @click="handelJump(`/`)">
              <img src="../assets/mobile/home/homeMenu.svg" alt="home" />
              <span> {{ $t(`home.home`) }}</span>
            </div>
          </el-dropdown-item>
          <!-- 最新报块 -->
          <el-dropdown-item
            style="font-size: 0.8rem; color: rgba(0, 0, 0, 1)"
            @click.native="handelJump(`/reportBlock`)"
          >
            <div class="menuItem">
              <img
                src="../assets/mobile/home/reportBlock.svg"
                alt="reportBlock"
              />
              <span> {{ $t(`home.reportBlock`) }}</span>
            </div>
          </el-dropdown-item>
          <el-dropdown-item style="font-size: 0.8rem; color: rgba(0, 0, 0, 1)">
            <el-collapse v-model="activeNames">
              <el-collapse-item name="1">
                <!-- 挖矿账户 -->
                <template slot="title">
                  <div class="menuItem2">
                    <img src="../assets/mobile/home/mining.svg" alt="account" />
                    <span>{{ $t(`home.accountCenter`) }}</span>
                  </div>
                </template>
                <div
                  class="accountBox"
                  @click="handelJumpAccount(item)"
                  v-for="item in newMiningAccountList"
                  :key="item.id"
                >
                  <div class="coinBox">
                    <img :src="item.img" alt="coin" />
                    <span class="coin">{{ item.coin }}</span>
                  </div>

                  <span class="account">{{ item.account }}</span>
                </div>
              </el-collapse-item>
            </el-collapse>
          </el-dropdown-item>
          <!-- 个人中心 -->
          <el-dropdown-item
            @click.native="handelJump(`/personalCenter`)"
            style="font-size: 0.8rem; color: rgba(0, 0, 0, 1)"
          >
            <div class="menuItem">
              <img
                src="../assets/mobile/home/personal.svg"
                alt="personalCenter"
              />
              <span> {{ $t(`home.personalCenter`) }}</span>
            </div>
          </el-dropdown-item>
          <!-- 工单记录 -->
          <el-dropdown-item
            @click.native="handelJump(`/workOrderRecords`)"
            style="font-size: 0.8rem; color: rgba(0, 0, 0, 1)"
          >
            <div class="menuItem">
              <img
                src="../assets/mobile/home/workRecord.svg"
                alt="workRecord"
              />
              <span> {{ $t(`personal.workOrderRecord`) }}</span>
            </div>
          </el-dropdown-item>
          <!-- 工单管理 -->
          <el-dropdown-item
            v-show="ManagementShow"
            @click.native="handelJump(`/workOrderBackend`)"
            style="font-size: 0.8rem; color: rgba(0, 0, 0, 1)"
          >
            <div class="menuItem">
              <img
                src="../assets/mobile/home/workAdministration.svg"
                alt="Work Order Management"
              />
              <span> {{ $t(`work.WorkOrderManagement`) }}</span>
            </div>
          </el-dropdown-item>

          <el-dropdown-item
            @click.native="handelSignOut"
            style="font-size: 0.8rem; color: rgba(0, 0, 0, 1)"
          >
            <div class="menuItem">
              <img src="../assets/mobile/home/lgout.svg" alt="sign out" />
              <span> {{ $t(`user.signOut`) }}</span>
            </div>
          </el-dropdown-item>
          <el-dropdown-item style="font-size: 0.8rem; color: rgba(0, 0, 0, 1)">
            <div class="langBox">
              <el-radio
                fill="red"
                @input="handelRadio"
                v-model="radio"
                label="zh"
                >简体中文</el-radio
              >
              <el-radio
                fill="#fff"
                @input="handelRadio"
                v-model="radio"
                label="en"
                >English</el-radio
              >
            </div>
          </el-dropdown-item>
          <el-dropdown-item
            style="font-size: 0.8rem; color: rgba(0, 0, 0, 1)"
            v-show="!isLogin"
          >
            <div class="menuLogin">
              <el-button class="lgBTH" @click="handelJump(`/login`)">{{
                $t(`home.MLogin`)
              }}</el-button>
              <el-button class="reBTH" @click="handelJump(`/register`)">{{
                $t(`home.MRegister`)
              }}</el-button>
            </div>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </header>
  </div>
</template>

<script>
import { getLogout, getAccountGradeList } from "../api/login";
export default {
  data() {
    return {
      radio: "en",
      data: [
        {
          label: "一级 1",
          children: [
            {
              label: "二级 1-1",
              children: [
                {
                  label: "三级 1-1-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 2",
          children: [
            {
              label: "二级 2-1",
              children: [
                {
                  label: "三级 2-1-1",
                },
              ],
            },
            {
              label: "二级 2-2",
              children: [
                {
                  label: "三级 2-2-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 3",
          children: [
            {
              label: "二级 3-1",
              children: [
                {
                  label: "三级 3-1-1",
                },
              ],
            },
            {
              label: "二级 3-2",
              children: [
                {
                  label: "三级 3-2-1",
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
      currencyList: [],
      miningAccountList: [],
      newMiningAccountList: [],
      activeNames: "",
      titleList: [
        {
          path: "/",
          label: "home.home",
        },
        {
          path: "/reportBlock",
          label: "home.reportBlock",
        },

        {
          //挖矿账户
          path: "/miningAccount",
          label: "home.accountCenter",
        },

        {
          //只读页面
          path: "/readOnlyDisplay",
          label: "personal.readOnlyPage",
        },
        {
          //个人中心
          path: "/personalCenter",
          label: "home.personalCenter",
        },
        {
          //挖矿账户设置
          path: "/personalCenter/personalMining",
          label: "home.accountSettings",
        },
        {
          //只读页面
          path: "/personalCenter/readOnly",
          label: "personal.readOnlyPage",
        },
        {
          //安全设置
          path: "/personalCenter/securitySetting",
          label: "personal.securitySetting",
        },
        {
          //API
          path: "/personalCenter/personalAPI",
          label: "home.API",
        },
      ],
      token: "",
      isLogin: false,
      jurisdiction: {
        roleKey: "",
      },
      ManagementShow: false,
    };
  },
  computed: {
    key() {
      let obj = this.titleList.find((item) => item.path == this.$route.path);
      if (obj) {
        return obj.label;
      } else {
        return "";
      }
    },
  },
  watch: {
    token: {
      handler(val) {
        if (val) {
          this.isLogin = true;
        } else {
          this.isLogin = false;
        }
      },
      immediate: true,
      deep: true,
    },
  },
  mounted() {
    this.radio = localStorage.getItem("lang")
      ? localStorage.getItem("lang")
      : "en";
    this.currencyList = JSON.parse(localStorage.getItem("currencyList"));
    let miningAccountList = localStorage.getItem("miningAccountList");
    this.miningAccountList = JSON.parse(miningAccountList);
    let valueTaking = localStorage.getItem("token");
    this.token = JSON.parse(valueTaking);

    let jurisdiction = localStorage.getItem("jurisdiction");
    this.jurisdiction = JSON.parse(jurisdiction);
    window.addEventListener("setItem", () => {
      this.currencyList = JSON.parse(localStorage.getItem("currencyList"));
      let miningAccountList = localStorage.getItem("miningAccountList");
      this.miningAccountList = JSON.parse(miningAccountList);
      let valueTaking = localStorage.getItem("token");
      this.token = JSON.parse(valueTaking);
      if (this.miningAccountList[0]) {
        this.newMiningAccountList = this.flattenArray(this.miningAccountList);
      }
      let jurisdiction = localStorage.getItem("jurisdiction");
      this.jurisdiction = JSON.parse(jurisdiction);

      if (this.jurisdiction && this.jurisdiction.roleKey == `admin`) {
        console.log(565656);
        this.ManagementShow = true;
      } else {
        this.ManagementShow = false;
      }
    });
    if (this.miningAccountList[0]) {
      this.newMiningAccountList = this.flattenArray(this.miningAccountList);
    }

    if (this.jurisdiction && this.jurisdiction.roleKey == `admin`) {
      this.ManagementShow = true;
    } else {
      this.ManagementShow = false;
    }
  },

  methods: {
    handelJump(url) {
      console.log(url, "及附件");

      try {
        const lang = this.$i18n.locale;
        // 移除开头的斜杠
        const cleanPath = url.startsWith("/") ? url.slice(1) : url;
        // 处理根路径特殊情况
        const path = cleanPath === "" ? `/${lang}` : `/${lang}/${cleanPath}`;
        this.$router.push(path).catch((err) => {
          if (err.name !== "NavigationDuplicated") {
            console.error("Navigation failed:", err);
          }
        });
      } catch (error) {
        console.error("Navigation error:", error);
      }
    },
    toggleDropdown() {
      this.showDropdown = !this.showDropdown;
    },
    handelRadio(val) {
      const oldLang = this.$i18n.locale;
      this.$i18n.locale = val;
      localStorage.setItem("lang", val);
      // 更新当前路径的语言部分
      const currentPath = this.$route.path;
      const newPath = currentPath.replace(`/${oldLang}`, `/${val}`);
      const query = this.$route.query;
      this.$router
        .push({
          path: newPath,
          query: query,
        })
        .catch((err) => {
          if (err.name !== "NavigationDuplicated") {
            console.error("Navigation failed:", err);
          }
        });
    },
    selectItem(item) {
      this.selectedItem = item;
      this.showDropdown = false;
      // 更新菜单项的高亮状态
      this.menuItems.forEach((menuItem) => {
        menuItem.isHighlighted = menuItem.text === item;
      });
    },
    handelJumpAccount(item) {
      const lang = this.$i18n.locale;
      let obj = {
        ma: item.account,
        coin: item.coin,
        id: item.id,
        img: item.img,
      };

      this.$addStorageEvent(1, "accountItem", JSON.stringify(obj));
      this.$router.push({
        path: `/${lang}/miningAccount`,
        query: { ma: item.account + item.coin },
      });
    },
    async fetchSignOut() {
      const lang = this.$i18n.locale;
      const data = await getLogout();
      if (data && data.code == 200) {
        this.$router.push(`/${lang}/login`);
      }
    },

    handelSignOut() {
      this.fetchSignOut();
      localStorage.removeItem(`token`);
      localStorage.removeItem("username");
      this.$addStorageEvent(1, `miningAccountList`, JSON.stringify(""));
      this.isLogin = false;
      this.isDropdownVisible = false;
    },

    //扁平化数组币种数组
    flattenArray(arr) {
      console.log(arr, "进来的数组");

      if (arr[0]) {
        return arr.reduce((flat, item) => {
          return flat.concat(
            item.children.map((child) => ({
              ...child,
              coin: item.coin,
              img: item.img,
              title: item.title,
            }))
          );
        }, []);
      }
    },
  },
};
</script>

<style scoped lang="scss">
.MoveMain {
  width: 100%;
  height: 100%;
  position: relative;
  z-index: 9999;
}

.headerMove {
  width: 100%;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0px 20px;

  z-index: 999;

  img {
    width: 30px;
  }

  .title {
    font-weight: 600;
  }

  .menu {
    width: 15%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: right;
  }
}

.menuItem {
  background: #d2c4e8;
  // padding: 0% 8%;
  padding-left: 5%;
  border-radius: 5px;
  margin-top: 20px;
  display: flex;
  align-items: center;
  justify-content: left;

  img {
    width: 15px;
    margin-right: 5px;
  }
}
.menuItem2 {
  background: #d2c4e8;
  padding-left: 5%;
  border-radius: 5px;
  display: flex;
  align-items: center;

  img {
    width: 15px;
    margin-right: 5px;
  }
}
.menuLogin {
  display: flex;
  margin-top: 10px;
  justify-content: space-around;
  margin-bottom: 20px;
}
.langBox {
  width: 100%;
  display: flex;
  font-size: 0.6em;
  margin-top: 18px;
  justify-content: space-around;
}
::v-deep .el-collapse-item__content {
  max-height: 300px;
  overflow-y: auto;
}

.el-radio {
  margin: 0 !important;
  margin-left: 2px !important;
  font-size: 0.6em !important;
}
.lgBTH {
  background: #651efe;
  color: #fff;
  padding: 8px 23px;
}
.reBTH {
  padding: 8px 23px;
  color: #fff;
  background: #ff4181;
}

::v-deep.el-dropdown-menu {
  left: 40% !important;
  top: 48px !important;
  padding-bottom: 30px;
}
.el-popper[x-placement^="bottom"] {
  min-width: 60% !important;
}

::v-deep .el-collapse-item__header {
  border: none !important;
  height: 36px !important;
  line-height: 36px !important;
  background: #d2c4e8 !important;
  margin-top: 20px;
  border-radius: 5px;
}
::v-deep.el-dropdown-menu__item:focus,
.el-dropdown-menu__item:not(.is-disabled):hover {
  padding: 0;
}

::v-deep.el-collapse {
  border: none !important;
}

::v-deep .el-collapse-item__wrap {
  border: none !important;
}

.currencyBox {
  margin: 0;
  padding: 0;
  width: 88%;
  display: flex;
  min-height: 200px;
  flex-wrap: wrap;
  padding: 10px 20px;
  margin: 0 auto;
  li {
    list-style: none;
    //  width: calc(100% / 5);
    display: flex;
    flex-direction: column;
    align-items: center !important;
    margin-left: 10px;
    // background: #D2C4E8;
    // align-items: center;
    // overflow: hidden;
    padding: 5px 5px;
    box-sizing: border-box;
    // background: palegoldenrod;

    img {
      width: 25px;
    }
    p {
      min-width: 50px;
      margin-top: 8px;
      font-size: 0.85rem;
      word-wrap: break-word;
      word-break: break-all;
      text-align: center;
      text-transform: capitalize;
    }
  }
}

.accountBox {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  font-size: 0.8rem;
  justify-content: space-between;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);

  .coinBox {
    display: flex;
    align-items: center;

    img {
      width: 20px;
    }
    .coin {
      margin-left: 5px;
    }
  }
  .coin {
    text-transform: capitalize;
  }
}

.el-menu--horizontal > .el-submenu .el-submenu__title {
  color: #000;
}
.el-menu--horizontal {
  background: transparent;
}
.el-submenu:hover {
  background-color: transparent !important; /* 例如，更改背景颜色 */
}
.el-submenu.is-active:after {
  border-bottom: none !important;
  border-bottom-color: transparent !important;
  border: none !important;
  outline: none !important;
  background: transparent;
}
</style>
<style lang="scss">
.el-submenu__title:hover {
  background-color: transparent !important;
  background: transparent !important;
  color: #6e3edb !important;
}
.el-menu-item.is-active:not(.is-index) {
  border-bottom: none !important;
}
.el-submenu__title {
  border: none !important;
  border-bottom: none !important;
  border-bottom-color: transparent !important;
}

.el-submenu.is-active {
  border: none !important;
  border-bottom: none !important;
  border-bottom-color: transparent !important;
}
.el-menu--horizontal > .el-submenu.is-active .el-submenu__title {
  border-bottom: 0px !important;
}
.el-submenu.is-active .el-submenu__title {
  border-bottom-color: transparent !important;
}
.el-menu.el-menu--horizontal {
  border-bottom: 0px !important;
}
.el-dropdown-menu__item,
.el-menu-item {
  padding: 0px 10px !important;
}
</style>