<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <div class="header-logo">
          <div class="left-box">
            <img src="@/assets/logo.svg" />

            <el-tabs
              v-model="activeName"
              class="demo-tabs"
              @tab-click="handleClick"
            >
              <el-tab-pane label="巡检" name="first"></el-tab-pane>
              <el-tab-pane label="报表" name="second"></el-tab-pane>
              <el-tab-pane label="配置" name="third"></el-tab-pane>
            </el-tabs>
          </div>
          <div class="right-box">
            <div class="task">
              <el-icon><Document /></el-icon>
              <span class="title">任务</span>
            </div>
            <div class="message">
              <el-icon><Message /></el-icon>
              <span class="title">消息</span>
            </div>
            <div class="divider"></div>
            <el-popover  placement="top"  trigger="hover"  :width="304">
              <div class="info">
                <img :src="userInfo.headImg" alt="" class="head-img" />
                <div class="info-text">
                  <div class="name">{{ userInfo.name }}</div>
                  <div class="company">{{ userInfo.companyName }}</div>
                </div>
              </div>
              <div class="change_account" @click="changeCompany">
              <div class="change_account_text">
                <img src="@/assets/images/qhqy.svg" class="change_account_icon">
                切换企业</div>
              <el-icon><ArrowRight /></el-icon>
              </div>
              <div class="change_account grsz_border" @click="settingUserInfo">
                <div class="change_account_text ">
                <img src="@/assets/images/grsz.svg" class="change_account_icon">
                个人设置</div>
              
              </div>
              <div class="change_account" @click="logoutFn">
                <div class="change_account_text">
                <img src="@/assets/images/tcdl.svg" class="change_account_icon">
                退出登录</div>
           
              </div>
              <template #reference>
                <div class="user" >
                  <img :src="userInfo.headImg" alt="" class="avatar" />
                  <span class="title">{{ userInfo.name }}</span>
                </div>
              </template>
            </el-popover>
          </div>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            :collapse="isCollapse"
            @open="handleOpen"
            @close="handleClose"
          >
            <Mymenu :data="menuArr"></Mymenu>
          </el-menu>
          <el-radio-group
            v-model="isCollapse"
            style="margin-bottom: 20px"
          >
            <el-radio-button :value="false" v-if="isCollapse">
              <!-- <el-icon class="location_icon"><Expand /></el-icon> -->
              <img src="@/assets/icons/zkdh.svg" alt="" class="location_icon_1">
            </el-radio-button>
            <el-radio-button class="location_button" :value="true" v-else>
              <!-- <el-icon class="location_icon_1"><Fold /></el-icon> -->
              <img src="@/assets/icons/sqdh.svg" alt="" class="location_icon_1">
              收起导航
            </el-radio-button>
          </el-radio-group>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>


  <ChangeCompany  ref="child" ></ChangeCompany>
  </div>
</template>
<script setup>
import { ref,onMounted, onUnmounted } from 'vue'
import { useRouter} from 'vue-router'
import { ElMessageBox } from 'element-plus'
import Mymenu from '../components/Mymenu.vue'
import { dynamicRoutes } from '@/router/menuArr'
import { getInfo,logout, } from '@/api/login'
import { useUserStore } from '@/store/modules/user'
import ChangeCompany from '@/components/changeCompany.vue'
// import {addDynamicRoutes} from '@/router'
const router=useRouter()
const userStore = useUserStore()

const userInfo = ref({})
const child = ref(null);
 
const callChildMethod = () => {
  if (child.value) {
    console.log('调用子组件的方法,child.value')
    child.value.changeCompany(); // 调用子组件的方法
  }
};


getInfo()
  .then(res => {
    if (res.data.code == 200) {
      console.log(res.data.data.userInfo)
      userInfo.value = res.data.data.userInfo
      userStore.userInfo = res.data.data.userInfo
      console.log(userStore.userInfo)
    }
  })
  .catch(() => {
    console.error('Info接口调用失败')
  })

const menuArr = ref([])
menuArr.value = dynamicRoutes('first')
const activeName = ref('first')
const handleClick = (tab) => {
  console.log(tab.props.name)
  menuArr.value = dynamicRoutes(tab.props.name)
  console.log("menuArr.value",menuArr.value)
  localStorage.setItem('menuArr', JSON.stringify(menuArr.value))

}

const isCollapse = ref(true)
const handleOpen = (key, keyPath) => {
  console.log(key, keyPath)
}
const handleClose = (key, keyPath) => {
  console.log(key, keyPath)
}
const settingUserInfo=()=>{
 router.push('/userInfo')
}
//切换企业弹框
const changeCompany=()=>{
  callChildMethod()
}


//退出登录
const logoutFn = () => {
  ElMessageBox.confirm(
    '确定退出登录？',
    '',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      logout().then(res => {
        if (res.data.code == 200) {
          console.log('退出成功')
          router.push('/login')
        }
      })
    })
    .catch(() => {
    })
}




//适配屏幕尺寸
const screenWidth = ref(window.innerWidth);

const handleResize = () => {
  screenWidth.value = window.innerWidth;
  console.log(screenWidth.value);
  if (screenWidth.value < 1280) {
    isCollapse.value = true;
  }
};

onMounted(() => {
  window.addEventListener('resize', handleResize);
});

onUnmounted(() => {
  window.removeEventListener('resize', handleResize);
});
</script>
<style scoped lang="scss">
.common-layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: url('@/assets/bg.png') no-repeat center center;
  background-size: cover;
}
.el-header {
  height: 66px;
  padding-bottom: 16px;
  .header-logo {
    display: flex;
    justify-content: space-between;
    .left-box {
      width: 366px;
      display: flex;
      justify-content: space-between;
    }
    .right-box {
      width: 260px;
      display: flex;
      justify-content: space-between;
      line-height: 50px;
      height: 50px;
      padding-top: 10px;
      padding-right: 20px;
      .divider {
        width: 2px;
        height: 20px;
        margin-top: 15px;
        background-color: #bfc1c5;
      }
      .task,
      .message,
      .user {
        display: flex;
        align-items: center;
        color: #323335;
        font-size: 14px;
        font-weight: 400;
        .title {
          margin-left: 10px;
        }
      }
      .avatar {
        margin-left: 10px;
        width: 30px;
        height: 30px;
        border-radius: 50%;
      }
    }
  }
}
.el-main {
  background-color: white;
  border-radius: 8px 8px 0px 0px;
}
.el-container {
  .el-aside {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .el-menu {
      flex: 1;
      border-right: none;
      color: white;
      background-color: rgba(255, 255, 255, 0);
      padding-left: 20px;
    }
  }
}
:deep(.el-aside) {
  width: auto;
  padding-right: 20px;
}
:deep(.el-tabs__nav-wrap:after) {
  height: 0;
}
.el-radio-button {
  .location_icon {
    margin-left: 5px;
    font-size: 20px;
  }
}

.location_icon_1 {
  font-size: 20px;
  margin-left: 5px;
  margin-right: 8px;
}
.location_button {
  display: inline-block;
  width: 150px;
  transition: transform 0.5s ease;
}
:deep(.el-radio-button__inner) {
  display: flex;
  width: 100%;
  background-color: rgba(255, 255, 255, 0);
  border: none;
}
.el-tabs {
  padding-top: 10px;
}
:deep(.el-tabs__item) {
  font-weight: bold;
}
.info {
  width: 280px;
  height: 80px;
  border-bottom: 1px #dededf solid;
  display: flex;
  align-items: center;
  .head-img {
    width: 48px;
    height: 48px;
    border-radius: 100px 100px 100px 100px;
  }
  .info-text {
    margin-left: 10px;
    .name {
      font-size: 16px;
      font-weight: bold;
      color: #202328;
      margin-bottom: 5px;
    }
    .company {
      font-size: 12px;
      color: #656A72;
    }
  }
}
.change_account{
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  .change_account_text{
    display: flex;
    align-items: center;
.change_account_icon{
  margin-right: 10px;
}
  }
}
.grsz_border
{
  border-bottom: 1px #dededf solid;
}


</style>
