<template>
  <div class="device" ref="device" :style="{ width: systemType==2 ? '420px': '100%'}">
    <div class="device-item" 
      v-for="item in deviceList" :key="item.device_id" 
      @click="chooseItem(item)"
      :class="systemType==2 && selectedDeviceId==item.device_id ? 'active' : ''"
      :style="{minWidth: systemType==2 ? '420px' : '380px'}"
    >
      <div class="item-header">
        <div style="display: flex; width: 50%">
          <div class="icon-bg">
            <span class="iconfont" :class="getIcongont(item)"></span>
          </div>
          <div class="title">
            <span class="number type">{{ item.typename }}</span>
            <span class="name" :title="item.name">{{ item.name }}</span>
            <span class="number"></span>
          </div>
        </div>

        <div class="status">
          <div class="status-item">
            <a-tag :color="item.is_online == 1 ? 'green' : 'red'">{{
              ['离线', '在线'][item.is_online]
            }}</a-tag>
            <!-- <span class="text">在线状态</span> -->
          </div>
          <div class="status-item">
            <a-tag :color="item.is_running == 0 ? 'orange' : 'green'">{{
              ['空闲', '工作'][item.is_running]
            }}</a-tag>
            <!-- <span class="text">工作状态</span> -->
          </div>
          <div class="status-item">
            <a-tag :color="item.is_error == 0 ? 'green' : 'red'">{{
              ['正常', '故障'][item.is_error]
            }}</a-tag>
            <!-- <span class="text">故障状态</span> -->
          </div>

        </div>
      </div>
     
      <div class="item-content" v-if="systemType!=2">
        <div v-for="info in item.params" :key="info.k" class="item-info">
          <span class="text">{{ info.k }}：</span>
          <span class="value">{{ info.v }}</span>
        </div>
      </div>
      <div class="item-button" v-if="systemType!=2">
        <div v-if="item.view == 1">
          <span class="text">实时数据：</span>
          <a-button type="primary" size="small" @click="openModal(item, 1)">查看</a-button>
        </div>
        <div v-if="item.type == 105">
          <span class="text">单体信息：</span>
          <a-button type="primary" size="small" @click="openModal(item, 2)">查看</a-button>
        </div>
      </div>
    </div>
  </div>
  <div style="width: calc(100% - 500px);" v-if="systemType==2">    
     <div class="item-gun" >
        <div class="gun">
          <div class="header">
            <div class="verline"></div>
            <span style="font-size: 16px; font-weight: 1000; color: #5bcdd1">枪：1</span>
          </div>
          <div class="gun-content">
            <div v-for="info in (selectedItem ? selectedItem.params : [])" :key="info.k" class="gun-info">
              <span class="text">{{ info.k }}：</span>
              <span class="value">{{ info.v }}</span>
            </div>
          </div>
        </div>
        <div class="gun">
          <div class="header">
            <div class="verline"></div>
            <span style="font-size: 16px; font-weight: 1000; color: #5bcdd1">枪：2</span>
          </div>
          <div class="gun-content">
            <div v-for="info in (selectedItem ? selectedItem.params1 : [])" :key="info.k" class="gun-info">
              <span class="text">{{ info.k }}：</span>
              <span class="value">{{ info.v }}</span>
            </div>
          </div>
        </div>
      </div>
  </div>
  <a-modal
    v-model:open="modalOpen"
    @ok="handleOk"
    width="60%"
    class="modal-device"
    :get-container="() => $refs.device"
  >
    <div v-if="modalComponent == 1" class="modal-content">
      <div class="item">
        <div class="title">
          电流电压
          <!-- <div>电流电压</div>
          <img src="@/assets/images/titleLine.png" alt="" /> -->
        </div>
        <div class="echarts">
          <predictEcharts
            :chart-options="chartOptions[0]"
            :chart-data="chartData"
            ref="chartRef1"
          />
        </div>
      </div>
      <div class="item">
        <div class="title">功率</div>
        <div class="echarts">
          <predictEcharts
            :chart-options="chartOptions[1]"
            :chart-data="chartData"
            ref="chartRef2"
          />
        </div>
      </div>
    </div>

    <div class="content-table" style="height: 600px" v-else>
      <a-table
        :columns="columns"
        :data-source="tableData"
        size="small"
        :scroll="{ y: 500 }"
        :pagination="false"
        row-class-name="no-hover-row"
        row-key="id"
      >
      </a-table>
    </div>
  </a-modal>
</template>

<script>
import predictEcharts from '@/components/predict/predictEcharts.vue'
import { postReq, getReq } from '@/request/api'
import { deviceTypeList } from '@/utils/config'
export default {
  name: 'DevicePage',
  components: { predictEcharts },
  props: {
    stationId: {
      type: String,
      default: ''
    },
    systemType: {
      type: Number,
      default: 1
    },
    propType: { type: Number, default: 1 }
  },
  data() {
    return {
      modalOpen: false,
      deviceList: [],
      selectedDeviceId: undefined,
      selectedItem: undefined,
      chartOptions: [
        {
          type: 'line',
          smooth: true,
          dataKey: 'chargeDischarge',
          infoKeys: [
            {
              key: 'V',
              label: '电压',
              seriesOptions: {
                symbol: 'circle',
                symbolSize: 8,
                itemStyle: {
                  color: '#00FDF9' // 充电电量线条颜色
                },
                lineStyle: {
                  color: '#00FDF9' // 充电电量线条颜色
                }
              }
            },
            {
              key: 'I',
              label: '电流',
              seriesOptions: {
                symbol: 'circle',
                symbolSize: 8,
                itemStyle: {
                  color: '#3E7EEF' // 充电电量线条颜色
                },
                lineStyle: {
                  color: '#3E7EEF' // 充电电量线条颜色
                }
              }
            }
          ]
        },
        {
          type: 'line',
          smooth: true,
          dataKey: 'chargeDischarge',
          infoKeys: [
            {
              key: 'P',
              label: '功率',
              seriesOptions: {
                symbol: 'circle',
                symbolSize: 8,
                itemStyle: {
                  color: '#00FDF9' // 充电电量线条颜色
                },
                lineStyle: {
                  color: '#00FDF9' // 充电电量线条颜色
                }
              }
            }
          ]
        }
      ],

      chartData: {
        xdata: [],
        ydata: {}
      },
      modalComponent: 1,
      columns: [
        {
          title: '序号',
          dataIndex: 'id',
          key: 'id',
          ellipsis: true,
          fixed: 'left'
        },
        {
          title: '单体SOC（%）',
          dataIndex: 'SOC',
          key: 'SOC'
        },
        {
          title: '单体SOH（%）',
          dataIndex: 'SOH',
          key: 'SOH'
        },
        {
          title: '单体电压（V）',
          dataIndex: 'V',
          key: 'V'
        },
        {
          title: '单体温度（℃）',
          dataIndex: 'T',
          key: 'T'
        },
        {
          title: '单体内阻（Ω）',
          dataIndex: 'R_i',
          key: 'R_i'
        }
      ],
      tableData: [],
      pageOption: {
        // pageSize: 1000,
        // page: 1
      },
      tableOption: {
        scroll: {
          y: 500
        },
        page: false
      },
      timer: null
    }
  },
  watch: {
    // 监听父组件数据变化
    stationId(newVal) {
      if (newVal) {
        this.selectedItem = undefined
        this.selectedDeviceId = undefined
        this.getDeviceList()
      }
    },
    systemType(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.selectedItem = undefined
        this.selectedDeviceId = undefined
        this.deviceList = []
        this.getDeviceList()
      }
    }
  },
  mounted() {
    this.getDeviceList()
    this.timer=setInterval(() => {
      // this.getDeviceList()
    }, 20000)
    
  },
  unmounted() {
    clearInterval(this.timer)

  },
  methods: {
    chooseItem(item) {
      this.selectedItem = item
      this.selectedDeviceId = item.device_id
    },
    handlePagesizeChange(pageOption) {
      this.pageOption.pageSize = pageOption.pageSize
      this.pageOption.page = pageOption.page
      this.getList()
    },
    getIcongont(ele) {
      return deviceTypeList.find((item) => item.value == ele.type).iconfont || ''
    },
    generateTimePoints() {
      const timePoints = []

      for (let hour = 0; hour <= 24; hour++) {
        for (let minute = 0; minute < 60; minute += 10) {
          // 处理24:00的特殊情况
          if (hour === 24 && minute === 0) {
            timePoints.push('24:00')
            break
          }

          // 格式化小时和分钟，确保两位数
          const formattedHour = hour.toString().padStart(2, '0')
          const formattedMinute = minute.toString().padStart(2, '0')
          timePoints.push(`${formattedHour}:${formattedMinute}`)
        }
      }

      return timePoints
    },
    //查看实时数据
    async getDevicCharts(item) {
      try {
        const res = await getReq('/queryDevicCharts', {
          station_id: this.stationId,
          device_id: item.device_id
        })
        this.chartData.ydata = res.data
        this.chartData.xdata = this.generateTimePoints()
        this.$nextTick(() => {
          if (this.$refs.chartRef1) {
            this.$refs.chartRef1.initCharts()
          }
          if (this.$refs.chartRef2) {
            this.$refs.chartRef2.initCharts()
          }
        })
        // this.$refs.chartRef1.initCharts()
        // this.$refs.chartRef2.initCharts()
      } catch (error) {
        console.log(error)
      }
    },
    //查询BCU单体信息
    async getDeviceBCUDetail(item) {
      try {
        const res = await getReq('/queryDeviceBCUDetail', {
          station_id: this.stationId,
          device_id: item.device_id
        })

        this.tableData = res.data.map((item, index) => {
          item.id = index + 1
          return item
        })
      } catch (error) {
        console.log(error)
      }
    },

    //请求运行监控系统设备信息
    async getDeviceList() {
      try {

        const res = await getReq('/queryDevicByCategory', {
          station_id: this.stationId,
          category: this.systemType
        })
        if (this.selectedDeviceId == undefined) {
          if (res.data.length > 0) {
            this.selectedItem = res.data[0]
            this.selectedDeviceId = this.selectedItem.device_id
          }
        }
        this.deviceList = res.data

        this.$emit('updateGatewayData', res.gateway);
      } catch (error) {
        this.deviceList = []

        console.log(error)
      }
    },
    async openModal(item, val) {
      this.modalComponent = val
      this.modalOpen = true
      if (val == 1) {
        await this.getDevicCharts(item)
      } else {
        await this.getDeviceBCUDetail(item)
      }
    },
    handleOk() {
      this.modalOpen = false
    }
  }
}
</script>

<style lang="scss" scoped>
.device {
  // background-color: #08a5ff;
  width: 100%;
  height: 100%;
  margin-left: 20px;
  display: flex;
  flex-wrap: wrap;
  grid-gap: 15px;
  overflow-y: auto;
  align-content: flex-start;
  // grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  .device-item {
    border-radius: 15px;
    background: $bg2-color;
    padding: 15px;
    min-width: 380px;
    max-width: 450px;
    width: calc(25% - 15px);
    // height: 260px;
    // flex: 1;
    cursor: pointer;
    &:hover {
      border: 1px solid #aaa;
    }

    .item-header {
      display: flex;
      align-items: center;
      height: 70px;
      color: #fff;
      justify-content: space-between;
      .icon-bg {
        width: 66px;
        height: 66px;
        text-align: center;
        line-height: 66px;
        border-radius: 6px;
        background: linear-gradient(90deg, #3dfefa 0%, #2a82e4 2.96%, #27a188 100%),
          linear-gradient(90deg, #3dfefa 0%, #01dfef 2.96%, #08a5ff 100%);
        .iconfont {
          font-size: 45px;
        }
      }
      .title {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        margin-left: 15px;
      }
      .number {
        font-size: 12px;
      }
      .name {
        font-size: 14px;
        display: block; /* 或 block，根据需求调整 */
        width: 80px; /* 固定宽度 */
        white-space: nowrap; /* 禁止换行 */
        overflow: hidden; /* 隐藏超出部分 */
        text-overflow: ellipsis;
      }
      .type {
        color: #08a5ff;
      }
      .status {
        display: flex;
        font-size: 14px;
        height: 100%;
        justify-content: space-evenly;
        &-item {
          display: flex;
          flex-direction: column;
          text-align: center;
          justify-content: space-evenly;
          align-items: center;
          min-width: 70px;
          :deep(.ant-tag) {
            margin-inline-end: 0 !important;
          }
          // span:first-child {
          //   font-weight: 700;
          // }
          .text {
            color: $text-color;
          }
        }
      }
    }
    .item-content {
      grid-template-columns: 1fr 1fr;
      color: #fff;
      display: grid;
      margin-top: 15px;
      // margin-bottom: 3px;
      padding: 0 5px;
      height: 120px;
      overflow-y: auto;
      .value {
        font-weight: 700;
        font-size: 13px;
      }
      .item-info {
        height: 24px;
      }
    }
    .item-button {
      display: grid;
      grid-template-columns: 1fr 1fr;
      color: #fff;
      margin-bottom: 3px;
      padding: 0 5px;
      // div {
      //   width: calc(50% - 10px);
      // }
    }
    .text {
      color: $text-color;
      font-size: 13px;
    }
    .video {
      margin-top: 10px;
    }
  }

  .active {
    // background: #012036;
    border: 1px solid #aaa;
  }

  .content-table {
    height: 700px;
    margin-top: 40px;

    :deep(.ant-table-header tr th) {
      background: $table-border !important;
      color: #fff;
    }
    :deep(.ant-table-body tr td, .ant-table-cell) {
      background: $table-bg !important;
      color: #fff;
    }
  }
}

.item-gun {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  width: 100%;
  height: 100%;
  .gun {
    border: 1px solid #37d6d970;
    border-radius: 5px;
    margin: 5px 0;
  }
  .header {
    height: 20px;
    font-size: 12px;
    display: flex;
    align-items: center;
    margin: 5px;
    .verline {
      margin-right: 10px;
      background: linear-gradient(
          180deg,
          rgba(0, 230, 172, 1) 0%,
          rgba(0, 210, 255, 1) 98.78%,
          rgba(0, 210, 255, 1) 100%
        ),
        rgba(1, 223, 239, 1);
      width: 4px;
      height: 15px;
      border-radius: 6px;
    }
  }
  .gun-content {
    display: grid;
    grid-template-columns: 1fr;
    height: 200px;
    padding: 0 5px;
    overflow-y: auto;
    .gun-info {
      height: 30px;

      .text {
        margin-left: 20px;
        color: $text-color;
        font-size: 14px;
      }
      .value {
        font-weight: 700;
        font-size: 16px;
      }
    }
  }
}

.environment {
  width: 200px;
}
.modal-device {
  color: #fff;
  .modal-content {
    height: 700px;
    .item {
      // height: 300px;
      .title {
        color: #fff;
        width: 230px;
        border-bottom: 5px solid transparent;
        border-image: url('@/assets/boxBottom.png') 0 0 100% 0 stretch;
      }

      .echarts {
        height: 300px;
      }
    }
  }
}

:deep(.ant-modal-body) {
  :deep(.ant-table-wrapper .ant-table-row-expand-icon) {
    color: $green !important;
  }
  //表格行悬浮样式
  .no-hover-row:hover > td {
    // background-color: transparent !important;
    background-color: #f0f8ff !important;
  }
}
:deep(.ant-table-cell) {
  &::before {
    width: 0 !important;
  }
}
:deep(.ant-table-thead > tr > td) {
  border-bottom: none !important;
  border-top: none !important;
}
:deep(.ant-table-wrapper .ant-table-tbody > tr > td) {
  border-top: none !important;
}
:deep(.ant-table-wrapper .ant-table-thead > tr > th) {
  border-bottom: none !important;
}
</style>
