<template>

  <div class="dashboard-container">
    <!-- 顶部导航栏 -->
    <header class="dashboard-header">
      <div class="logo">
        <div class="logo_header">
          <i class="icon-dashboard">
            <img src="@/assets/images/headers/config_header.png" alt="" style="width: 20px; height: 20px;">
          </i>
          <span class="logo_header_content">大屏配置</span>
        </div>
        <div class="logo_header_right">
          <span class="logo_header_right_content">[区域一特事故]</span>
        </div>
      </div>

      <div class="logo logo_right">
        <hr class="divider">
      </div>
      <div class="header-controls">
        <button class="close-btn" @click="emits('handleScreenConfigeClose')">×</button>
      </div>
    </header>

    <!-- 主体内容区 -->
    <main class="dashboard-main">
      <!-- 标签页导航 -->
      <div class="tabs-container">
        <div class="tab-item" :class="{ 'active': activeTab === 'equipmentCount' }"
          @click="activeTab = 'equipmentCount'">
          对象选取
        </div>
        <div class="tab-item" :class="{ 'active': activeTab === 'failureStatus' }" @click="activeTab = 'failureStatus'">
          模板列表
        </div>
        <div class="tab-indicator" :style="indicatorStyle"></div>
      </div>

      <!-- 标签页内容区域 -->
      <div class="tab-content">
        <!-- 根据选中的标签显示不同内容 -->
        <div v-if="activeTab === 'equipmentCount'" class="tab-panel">
          <!-- 地面站设备数量内容 -->
          <div class="equipment-content">
            <!-- 下拉选择器区域 -->
            <div class="selector-container">

              <div class="selector-item">
                <label class="selector-label">{{ keliSelector.label }}</label>
                <div class="custom-select">
                  <!-- 粒度 -->
                  <el-select v-model="keliSelector.value" placeholder="" style="width: 100%;" @change="keliOnChange">
                    <el-option v-for="item in keliSelector.options" :key="item.value" :label="item.label"
                      :value="item.value" />
                  </el-select>
                </div>
              </div>

              <div class="selector-item" v-if="!isGlobal">
                <label class="selector-label" style="visibility: hidden;">占位符</label>
                <div class="custom-select">
                  <el-select v-model="childrenSelector.value" placeholder="" style="width: 100%;" :disabled="isGlobal">
                    <el-option v-for="item in childrenSelector.options" :key="item.value" :label="item.label"
                      :value="item.value" />
                  </el-select>
                </div>
              </div>


              <div class="selector-item">
                <label class="selector-label">模板列表</label>
                <div class="custom-select">
                  <!-- 粒度 -->
                  <el-select v-model="templateSelector" placeholder="请选择模板列表" @change="templateOnChange"
                    style="width: 100%;">
                    <el-option v-for="item in templateSelectorOptions" :key="item.value" :label="item.label"
                      :value="item.value" />
                  </el-select>
                </div>
              </div>
            </div>
            <div class="content-item">
              <KeepAlive>
                <component :is="currentComponent1"></component>
              </KeepAlive>

              <KeepAlive>
                <component :is="currentComponent2"></component>
              </KeepAlive>

              <KeepAlive>
                <component :is="currentComponent3"></component>
              </KeepAlive>

            </div>
            <div>
              <el-button type="primary" class="apply-btn apply-btn-right">应用</el-button>
            </div>
          </div>
        </div>

        <div v-if="activeTab === 'failureStatus'" class="tab-panel">
          <!-- 模板案例情况内容 -->
          <div class="failure-content">
            <div class="templateCase_left templateCase">
              <div class="templateCase_left_header">
                <div class="templateCase_left_header_item_title">模板列表</div>
                <div class="templateCase_left_header_item_tool">
                  <el-icon @click="handleDelete" title="删除" style="cursor: pointer">
                    <Delete />
                  </el-icon>
                  <el-icon @click="handleAdd" title="添加" style="cursor: pointer">
                    <Plus />
                  </el-icon>

                </div>
              </div>
              <div class="templateCase_left_content">
                <div class="templateCase_left_content_item" v-for="(item, index) in templateCaseList"
                  :key="item.templateId" @click="templateCaseClickFn(item, index)"
                  :class="{ 'CaseClickActive': activeTemplateIndex === index }">
                  {{ item.templateName }}
                </div>
              </div>
            </div>
            <div class="templateCase_right templateCase">
              <screenConfigCase ref="screenConfigCaseRef" :settingConfig="currentSettingConfig"></screenConfigCase>
            </div>

          </div>
          <el-button type="primary" class="apply-btn" @click="handleApply">应用</el-button>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup>

import { ref, computed, onMounted, watch, markRaw } from 'vue';
import hotMapComponent from '@/pages/all/components/hotMapComponents/index.vue';
import cdf from '@/pages/all/components/cdf/index.vue';
import instant from '@/pages/all/components/instant/index.vue';
import screenConfigCase from './screenConfigCase.vue';
import { getCapacityAndTraffic, getAvailability, getTemplateList, templateAdd, templateDel, templateDetail, templateuUpdate } from '@/api/Zodiac';
import useAppStore from '@/store/module/app'
import { ElMessage, ElMessageBox } from 'element-plus'

let currentComponent1 = markRaw(hotMapComponent)  //容量流量  热力图
let currentComponent2 = markRaw(cdf)  //可用性 PDF曲线
let currentComponent3 = markRaw(instant)  //let  瞬时值曲线组件   //前面那个人写的好乱更屎一样，我服了，在这里偷偷吐槽一下嘿嘿
const screenConfigCaseRef = ref()

const currentSettingConfig = ref()
const templateCaseList = ref([])


const appStore = useAppStore();

const emits = defineEmits(['handleScreenConfigeClose']);
// 标签页状态管理
const activeTab = ref('equipmentCount'); // 默认选中"地面站设备数量"



const keliSelector = ref(
  {
    label: '地面粒度',
    value: 'global',
    options: [
      {
        value: 'global',
        label: '全球',
      },
      {
        value: 'region',
        label: '区域',
      },
      {
        value: 'station',
        label: '信关站',
      },
      {
        value: 'grid',
        label: '格网',
      }
    ]
  });

//根据前选择器决定后选择器
const childrenSelector = ref(
  {
    label: '',
    value: '',
    options: []
  }
)

const isGlobal = computed(() => keliSelector.value.value === 'global');

const templateSelector = ref()
const templateSelectorOptions = ref([])

watch(() => templateCaseList.value, () => {
  console.log('templateCaseList', templateCaseList.value)
  templateSelectorOptions.value = templateCaseList.value.map(item => ({
    label: item.templateName,
    value: item.templateId
  }))
})


const dropdownVisible = ref([false, false, false]);
const handleDelete = () => {
  if (!currentSettingConfig.value.templateId) {
    ElMessage({
      type: 'error',
      message: '请先选择一个模板',
    })
    return
  }
  ElMessageBox.confirm(
    `您确定删除 [${currentSettingConfig.value.templateName}] ?`,
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
      const ret = await templateDel({
        templateId: currentSettingConfig.value.templateId // 模板id 
      })
      if (ret.code == 200) {
        await handleList() // 刷新列表获取最新数据
        currentSettingConfig.value = templateCaseList.value[0] || {} // 删除后默认选中第一个模板
        activeTemplateIndex.value = 0;
      }
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
    })
};
const handleAdd = () => {
  // console.log('新增')
  // const ret = await templateAdd({
  //   name: '模板1',
  //   settingConfig: {}
  // })
  const templateId = Date.now();
  templateCaseList.value.push({
    //新增模板
    templateName: '新增模板', //模板名
    templateId,   // 模板id，后端生成的应该是uuid，这里先用时间戳模拟
  })

  currentSettingConfig.value = {
    templateName: '新增模板',
    templateAdd: true,
    templateId,
  }

  activeTemplateIndex.value = templateCaseList.value.length - 1; // 设置当前选中为新增的模板

};
// 初始化选中值
onMounted(async () => {
  await handleList();
  // await getCapacityAndTrafficData()
  // await getAvailabilityData();
});

// 切换下拉菜单显示
const toggleDropdown = (index) => {
  // 关闭其他下拉菜单
  dropdownVisible.value = dropdownVisible.value.map((val, i) => i === index ? !val : false);
};


const getCapacityAndTrafficData = async () => {
  const res = await getCapacityAndTraffic({
    granularity: keliSelector.value.value,
    startTime: '2025-11-03 00:00:00',
    endTime: '2025-11-06 00:00:00',
    regionName: '',
    signalSiteCode: '',
    gridCodes: ''

  })
  console.log(res)
}

const getAvailabilityData = async () => {
  const res = await getAvailability({
    granularity: keliSelector.value.value,
    startTime: '2025-11-03 00:00:00',
    endTime: '2025-11-04 00:00:00',
    regionName: '',
    signalSiteCode: '',
    gridCodes: ''
  })
  console.log('可用性', res)
}

const activeTemplateIndex = ref(0);
const templateCaseClickFn = (item, index) => {
  //console.log('templateCaseClickFn', index);
  activeTemplateIndex.value = index;
  currentSettingConfig.value = item;
};
// 点击外部关闭下拉菜单
document.addEventListener('click', (e) => {
  if (!e.target.closest('.custom-select')) {
    dropdownVisible.value = [false, false, false];
  }
});

// 计算指示器位置
const indicatorStyle = computed(() => {
  return activeTab.value === 'equipmentCount'
    ? { left: '0%', width: '50%' }
    : { left: '50%', width: '50%' };
});

const keliOnChange = () => {
  childrenSelector.value.value = ''
  switch (keliSelector.value.value) {
    case 'region':
      childrenSelector.value.options = appStore.regionPanelData
      break;
    case 'grid':
      childrenSelector.value.options = appStore.gridPanelData
      break;
    case 'station':
      childrenSelector.value.options = appStore.signalSitePanelData
      break;
  }
}
watch(() => keliSelector.value.value, () => keliOnChange)

const handleApply = async () => {
  let ret = null
  if (currentSettingConfig.value.templateAdd) { // 如果是新增的模板，先调用新增接口，再调用修改接口
    ret = await templateAdd({
      // templateName: currentSettingConfig.value.templateName, // 模板名
      ...screenConfigCaseRef.value.configData() // 获取当前配置
    })
  } else {

    ret = await templateuUpdate({
      templateId: currentSettingConfig.value.templateId, // 模板id
      ...screenConfigCaseRef.value.configData() // 获取当前配置
    })
  }

  if (ret.code == 200) {
    await handleList() // 刷新列表获取最新数据
    ElMessage({
      message: '模板设置成功',
      type: 'success',
    })
  }


}

const handleList = async () => {
  const ret = await getTemplateList()
  if (ret.code == 200) {
    templateCaseList.value = ret.data;
    currentSettingConfig.value = ret.data[activeTemplateIndex.value]
  }
}

const templateOnChange = (val) => {
  console.log('val', val)
  const selectedTemplate = templateCaseList.value.find(item => item.templateId === val)
  console.log('selectedTemplate', selectedTemplate)
  // availabilityCurveType
  // capacityCurveType
  // latencyCurveType
  const obj = {
    'PDF曲线': markRaw(cdf),
    '热力图': markRaw(hotMapComponent),
    '瞬时值曲线': markRaw(instant)
  }

  currentComponent1 = obj[selectedTemplate.capacityCurveType]
  currentComponent2 = obj[selectedTemplate.availabilityCurveType]
  currentComponent3 = obj[selectedTemplate.latencyCurveType]


  // if()


}






</script>

<style scoped>
/* 基础样式设置 */
.dashboard-container {
  /* width: 900px; */

  background-color: #000000;
  color: #080606;
  /* display: flex;
  flex-direction: column; */




  position: fixed;
  /* 改为fixed定位 */
  left: 10%;
  top: 10%;

  width: 1550px;

  z-index: 1001;
  overflow: hidden;






}

/* 头部样式 */
.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  padding: 0rem 1rem;
}

.logo {
  display: flex;
  align-items: center;
  height: 100%;
  width: 200px;
  justify-content: space-between;
  color: #ffffff;
}

.logo_header {
  width: 90px;
  height: 100%;
  align-items: center;
  justify-content: space-between;
  display: flex;
}

.icon-dashboard {
  width: 10px;
  height: 10px;
  display: flex;
  align-items: center;
}

.logo_header_content {
  font-weight: 900;
  font-size: 16px;
  /* font-family: 'Consolas', monospace;  */
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.logo_header_right {
  display: flex;
  padding-left: 10px;
  align-items: center;
  width: 100px;
  height: 100%;
}

.logo_header_right_content {
  color: #000000;
  padding: 2px;
  font-size: 12px;
  border-radius: 5px;
  font-weight: 200;
  background-color: #6ffcba;
}

.logo_right {
  width: 70%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.divider {
  width: 100%;
  border: none;
  border-top: 1px solid #3e3f3f;
  margin: 0;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.close-btn {
  background: none;
  border: none;
  color: #8890a6;
  cursor: pointer;
  font-size: 1.2rem;
  transition: color 0.2s;
}

.close-btn:hover {
  color: #ffffff;
}

/* 主内容区域 */
.dashboard-main {
  flex: 1;
  padding: 1rem;
  overflow-y: auto;
}

/* 标签页样式 */
.tabs-container {
  display: flex;
  width: 300px;
  background-color: #0f172a;
  border-radius: 4px;
  overflow: hidden;
  position: relative;
  /* margin-bottom: 5px; */
}

.tab-item {
  flex: 1;
  padding: 8px 0;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #cbd5e1;
  font-size: 14px;
  position: relative;
  z-index: 1;
}

.tab-item.active {
  color: #ffffff;
  font-weight: 500;
}

/* 标签页激活指示器 */
.tab-indicator {
  position: absolute;
  height: 100%;
  background: linear-gradient(to bottom, #2c78f5, #000000);
  border-bottom: 1px solid #2c78f5;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 标签页内容区域 */
.tab-content {
  background-color: rgba(15, 23, 42, 0.5);
  border-radius: 8px;
  padding: 5px 15px;
}

.equipment-content {
  height: 600px;
}

.tab-panel {
  min-height: 600px;
  /* max-height: 700px; */
  color: #e2e8f0;
}

.failure-content {
  display: flex;
  flex-direction: row;
  font-size: 15px;
  width: 1500px;
  /* gap: 20px; 添加间距 */
  /* height: 500px; */
  /* background-color: rgba(1, 73, 241, 0.5); */
  /* padding: 15px; 添加内边距 */
  border-radius: 8px;
  /* 添加圆角 */
}

.templateCase {
  flex: 1;
  height: 700px;
  /* width: 500px; */
  /* background-color: rgba(241, 1, 81, 0.5); */
  /* border-radius: 6px; 添加圆角 */
  /* border: 1px solid rgba(255, 255, 255, 0.1); 添加边框 */
  /* padding: 10px; 添加内边距 */
  overflow: hidden;
  /* 防止内容溢出 */
}

/* 可以为左右两侧添加不同样式 */
.templateCase_left {
  flex: 0.151;
  height: 100%;

  /* background-color: rgba(241, 1, 81, 0.5); */
  /* border-radius: 6px; */
  border-right: 1px solid rgba(255, 255, 255, 0.1);
  /* padding: 10px; */
  overflow: hidden;
}

.templateCase_right {
  flex: 1;
  height: 100%;
  background-color: rgba(2, 2, 2, 0.5);
  /* border-radius: 6px; */
  /* border: 1px solid rgba(255, 255, 255, 0.1); */
  /* padding: 10px; */
  /* overflow: hidden; */
}


.templateCase_right_content {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  /* background-color: #2b70df; */
  /* padding: 10px 10px; */
}

.templateCase_right_content_header {
  display: flex;
  flex-direction: column;
  height: 8%;
  width: 100%;
  padding: 10px 10px;
  /* background-color: #df2b34; */
}

.templateCase_right_content_body {
  height: 92%;
  width: 100%;
  background-color: #73df2b;
}

/* 新增样式 */
.horizontal-sections {
  display: flex;
  height: 100%;
  /* gap: 10px; 可选：段落之间的间距 */
}

.section-item {
  flex: 1;
  height: 100%;
  background-color: #1e293b;
  /* 您可以选择合适的背景色 */
  border: 1px solid rgba(255, 255, 255, 0.1);
  /* 可选：边框 */
  padding: 10px 10px;
  /* border-radius: 4px; 可选：圆角 */
}

.templateCase_right_content_header_item {

  height: 100%;
  width: 8%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* background-color: #6ffcba; */

}

.templateCase_left_header {
  display: flex;
  padding: 10px 0;
}

.templateCase_left_header_item_title {
  flex: 0.61;
  padding-left: 10px;
  /* background-color: #2c60b4; */
}

.templateCase_left_header_item_tool {
  display: flex;
  justify-content: flex-end;
  flex: 0.39;
  gap: 15px;
  padding-right: 10px;
  align-items: center;
  /* 垂直居中对齐 */
}




.templateCase_left_content {
  /* background-color: #6ffcba; */
  width: 100%;
  height: 100%;
  overflow-y: auto;
}

.templateCase_left_content_item {
  height: 40px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: left;
  padding: 10px 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  font-size: 12px;
  /* background-color: #2c78f5; */
}

.CaseClickActive {
  background: linear-gradient(to right, #193d77 0, rgba(0, 0, 0, 1) 100%);
}

.templateCase_left_content_item:hover {
  background: linear-gradient(to right, #2b70df 0, rgba(0, 0, 0, 1) 100%);
}

/* 下拉选择器样式 */
.selector-container {
  display: flex;
  gap: 20px;
  /* padding: 10px 0; */

}

.selector-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding-top: 15px;
}

.selector-label {
  font-size: 14px;
  color: #94a3b8;
}

.custom-select {
  position: relative;
  width: 160px;
  /* color: #ffffff; */
  border: 1px solid #000000;
}

:deep(.custom-select .el-select__wrapper) {
  height: 12.5px;
}

:deep(.custom-select .el-select) {
  background: #000000;
  border: 1px solid #000000;
  /* color: #ffffff; */
}

:deep(.custom-select .el-select .el-select__wrapper) {
  background: #000000;
  border: 1px solid #000000;
  box-shadow: 0 0 2px rgba(131, 131, 131, 0.95);
  /* color: #ffffff; */
}

:deep(.custom-select.el-range-input) {
  color: #ffffff;
  /* font-size: 10px; */
}

:deep(.el-popper) {
  background-color: #000000 !important;
}

:deep(.el-select-dropdown) {
  background: #000000 !important;
}

:deep(.el-select-dropdown) {
  background-color: #000000 !important;
}

.select-trigger {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background-color: #91380f;
  border: 1px solid #334155;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  color: #e2e8f0;
}

.select-value {
  flex: 1;
  text-align: left;
}

.select-arrow {
  font-size: 10px;
  color: #94a3b8;
  transition: transform 0.2s;
}

.custom-select:hover .select-arrow {
  color: #e2e8f0;
}

.select-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 2px;
  background-color: #1e293b;
  border: 1px solid #334155;
  border-radius: 4px;
  z-index: 10;
  max-height: 150px;
  overflow-y: auto;
}

.dropdown-option {
  padding: 6px 12px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s;
}

.dropdown-option:hover {
  background-color: #334155;
  color: #ffffff;
}

.content-item {
  display: flex;
  flex-direction: row;
  gap: 20px;
  padding: 10px 5px;
  /* background-color: #1c5abe; */
}

.content-item>* {
  flex: 1;
  min-width: 0;
}

.content-item>*:not(:last-child) {
  padding-right: 10px;
  border-right: 1px solid #1b1b1b;
  /* 右边框样式 */
}

.apply-btn-right {
  position: absolute;
  bottom: 16px;
}

:deep(.apply-btn) {
  width: 100px;
  height: 30px;
  /* margin-top: 10%; */
  border: none;
  border-radius: 0px;
  background: linear-gradient(to bottom, #2c78f5 0, rgba(0, 0, 0, 1));
  color: #ffffff;
  font-size: 14px;
  cursor: pointer;
  border-bottom: 1px solid #2c78f5;
}

/* 滚动条样式 */
.select-dropdown::-webkit-scrollbar {
  width: 6px;
}

.select-dropdown::-webkit-scrollbar-track {
  background: #1e293b;
}

.select-dropdown::-webkit-scrollbar-thumb {
  background: #475569;
  border-radius: 3px;
}

.select-dropdown::-webkit-scrollbar-thumb:hover {
  background: #64748b;
}
</style>