<!-- BluetoothHeader.vue -->
<template>
  <view class="header">
    <u-row>
      <u-col span="2">
        <view class="status-btn" :class="{ active_btn: linkStatus }">Link</view>
      </u-col>
      <u-col span="2">
        <view class="status-btn" :class="{ active_btn: bleConnected }">blue</view>
      </u-col>
      <u-col span="4" textAlign="left">
        <text class="title">长沙亿旭智能</text>
      </u-col>
      <u-col span="4">
        <view class="text-lable">2025.12.02 V1.0</view>
        <view class="typeSelect" @click="toggleAgreementType">
          {{ agreementTypeName }}
          <u-icon name="arrow-down-fill" color="#fff" size="16"></u-icon>
        </view>
      </u-col>
    </u-row>

    <u-action-sheet
        :show="showAgreementType"
        :actions="typeOptions"
        title="请选择协议类型"
        @close="closeAgreementType"
        @select="selectAgreementType"
    >
    </u-action-sheet>
  </view>
</template>

<script>
import {setAgreement,getAgreement} from '@/utils/modbus.js';
export default {
  data() {
    return {
      communicationTimer: null,
      linkStatus:false,
      showAgreementType: false,
      agreementTypeName: "",
      typeOptions: [
        {name: 'Lora', value: 'Lora'},
        {name: 'zigbee', value: 'zigbee'}
      ],
    }
  },
  computed: {
    bleConnected() {
      return this.$store.getters['ble/connected']
    },
    bleData() {
      return this.$store.getters['ble/data']
    }
  },
  watch: {
    bleData: {
      handler(newData, oldData) {
        if (newData) {
          console.log('接收到蓝牙数据:------------------')
          // 清除之前的定时器
          if (this.communicationTimer) {
            clearTimeout(this.communicationTimer)
            this.communicationTimer = null
          }
          if (newData.device !== '255' && newData.device !== ''){
              // 设置通信链接状态为 true
              this.linkStatus = true
          }
          // 将数据传递给父组件
          this.$emit('ble-data-received', newData);

          this.communicationTimer = setTimeout(() => {
            this.linkStatus = false
            this.communicationTimer = null
          }, 5000)
        }
      },
      deep: true, // 深度监听，确保嵌套对象变化时也能触发
      immediate: true // 立即执行一次
    }
  },
  mounted() {
    this.agreementTypeName = getAgreement()
  },
  methods: {
    toggleAgreementType() {
      this.showAgreementType = true;
    },
    closeAgreementType() {
      this.showAgreementType = false;
    },
    selectAgreementType(e) {
      setAgreement(e.name);
      this.agreementTypeName = getAgreement()
    }
  }
}
</script>

<style lang="scss">
// 头部样式
.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20rpx;

  .status-btn {
    background-color: #ff4757;
    border: none;
    margin-left: 0px;
    padding: 0px;
    width: 40px;
    text-align: center;
    color: white;
    border-radius: 4px;
    height: 20px;
    line-height: 20px;
  }
  .active_btn {
    background-color: yellowgreen !important;
  }



  .title {
    color: #fff;
    font-size: 36rpx;
    font-weight: bold;
  }
}
.typeSelect{
  font-size: 16px;
  color: #fff;
  font-weight: 600;
  display: flex;
  justify-content: flex-end;
}
.text-lable{
  color: #fff;
  font-size: 12px;
  text-align: right;
}
</style>
