<script lang="ts" setup>
import { executeCommandAPI } from '@/api/centerControl'

const props = defineProps({
  bikeCode: {
    type: String,
    default: '',
  },
})

const bikecode = ref('')
const ecucode = ref('')
const btnConfig = reactive({
  width: '150rpx',
  heigth: '60rpx',
})

// 测试项列表
const list = reactive({
  unlocking: {
    msg: '',
    name: '开锁',
    key: 'UNLOCK',
    status: 'none',
    tip: '',
    btnText: '开锁',
    btnLoadingText: '执行',
    btnLoading: false,
  },
  Locking: {
    msg: '',
    name: '关锁',
    key: 'LOCK',
    status: 'none',
    tip: '',
    btnText: '关锁',
    btnLoadingText: '执行',
    btnLoading: false,
  },
  openLock: {
    msg: '',
    name: '开电池仓',
    key: 'OPEN_BATTERY_LOCK',
    status: 'none',
    tip: '',
    btnText: '开电池仓',
    btnLoadingText: '执行',
    btnLoading: false,
  },
  openHelmet: {
    msg: '',
    name: '开头盔锁',
    key: 'OPEN_HELMET',
    status: 'none',
    tip: '',
    btnText: '开头盔锁',
    btnLoadingText: '执行',
    btnLoading: false,
  },
})

//  确定按钮点击事件
async function handleBtnClick(value: any, key: string, index: number) {
  if (!bikecode.value) {
    return uni.showToast({
      title: '无车辆编号',
      icon: 'none',
    })
  }
  list[key].btnLoading = true
  try {
    const result: boolean = await executeCommandAPI({
      ecuSn: '',
      bikeCode: bikecode.value,
      commandCode: value.key,
    })
    if (result) {
      uni.showToast({
        title: '执行成功',
        icon: 'success',
      })
      list[key].btnLoading = false
    }
    else {
      uni.showToast({
        title: '执行失败',
        icon: 'error',
      })
      list[key].btnLoading = false
    }
  }
  catch (e) {
    uni.showToast({
      title: '执行异常',
      icon: 'error',
    })
    console.error('中控测试报错------>', e)
  }
}

watch(() => props.bikeCode, (val) => {
  console.log(val, '2222')
  bikecode.value = val
})
</script>

<template>
  <view class="cotainer">
    <uv-button
      v-for="(value, key, index) in list"
      :key="key"
      :loading="value.btnLoading"
      :loading-text="value.btnLoadingText"
      type="primary"
      :text="value.btnText"
      :custom-text-style="{
        fontSize: '28rpx',
      }"
      :custom-style="btnConfig"
      @click="handleBtnClick(value, key, index)"
    />
  </view>
</template>

<style lang="scss" scoped>
.cotainer {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
