<template>
  <div class="test1_part">
    <div class="title">调试按压距离</div>
    <div class="form_part">
      <div class="form_item">
        <div class="item_title">增加按压距离：</div>
        <vxe-select
          class="test1_select"
          v-model="form_value.add_value"
          placeholder="请选择"
          v-disableKeybord="disabledComponents">
          <vxe-option v-for="num in 15" :key="num" :value="num" :label="num"></vxe-option>
        </vxe-select>
        <CommonButton size="default" type="info" msg="确定" @click="checkDistance('add')" />
      </div>
      <div class="form_item">
        <div class="item_title">减少按压距离：</div>
        <vxe-select
          class="test1_select"
          v-model="form_value.stop_value"
          placeholder="请选择"
          v-disableKeybord="disabledComponents">
          <vxe-option v-for="num in 15" :key="num" :value="num" :label="num"></vxe-option>
        </vxe-select>
        <CommonButton size="default" type="info" msg="确定" @click="checkDistance('stop')" />
      </div>
      <div class="form_item">
        <div class="item_title">最终按压距离：</div>
        <div class="num">{{ computed_distance }}</div>
        <CommonButton size="default" type="info" msg="确定" @click="checkDistance('check')" />
      </div>
    </div>
    <div class="footer">
      <CommonButton size="default" type="info" msg="返回" @click="router.push('/home')" />
      <CommonButton size="default" type="danger" msg="测试喷射" @click="testShoot" />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, watch } from 'vue';
import { useRouter } from 'vue-router';
import { notify } from '@/utils/message';
import { addDistanceEApi, stopDistanceEApi, DistanceFuWeiApi, startOpenApi } from '@/api/distance';
const router = useRouter();
const disabledComponents = computed(() => {
  return ['disableKeyboard'];
});
let form_value = ref({
  add_value: 1,
  stop_value: 1
});
const computed_distance = computed(() => {
  let distance = form_value.value.add_value - form_value.value.stop_value;
  if (distance < 0) {
    distance = 0;
  }
  if (distance > 22) {
    distance = 22;
  }
  return distance;
});
watch(computed_distance, (newDistance, oldDistance) => {
  if (newDistance !== oldDistance) {
    if (newDistance === 0) {
      notify('最小距离不能小于0');
    } else if (newDistance === 22) {
      notify('最大距离不能大于22');
    }
  }
});
//按压距离
const checkDistance = async (type) => {
  let res;
  if (type == 'add') {
    if (form_value.value.add_value == '') return notify('请选择按压距离');
    res = await addDistanceEApi({ addDistance: form_value.value.add_value });
    let { status } = res;
    if (status == 200) notify('请求成功', 'success');
  } else if (type == 'stop') {
    if (form_value.value.stop_value == '') return notify('请选择按压距离');
    res = await stopDistanceEApi({ stopDistance: form_value.value.stop_value });
    let { status } = res;
    if (status == 200) notify('请求成功', 'success');
  } else {
    res = await DistanceFuWeiApi();
    let { status } = res;
    if (status == 200) notify('请求成功', 'success');
  }
};
const testShoot = async () => {
  let res = await startOpenApi();
  let { status } = res;
  if (status == 200) {
    notify('请求成功', 'success');
  }
};
</script>
<style lang="less" scoped>
@SIZE: 16px;
.test1_part {
  width: 100%;
  padding: 16px;
  background: #f6f6f6;
  box-shadow: 0px 3px 9px 1px rgba(0, 0, 0, 0.16);
  border-radius: 4px 4px 4px 4px;
  border: 1px solid #cccccc;
  .title {
    font-size: @SIZE;
    font-weight: bold;
    margin-bottom: 35px;
  }
  .form_part {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: @SIZE;
    .form_item {
      display: flex;
      height: 44px;
      align-items: center;
      margin-bottom: 20px;
      .test1_select {
        width: 93px;
        height: 44px;
        margin-right: 12px;
        :deep(.vxe-input) {
          height: 44px;
        }
      }
      .num {
        width: 106px;
        color: #cf4a4a;
        font-size: 20px;
      }
    }
    :last-child {
      margin-bottom: 0;
    }
  }
  .footer {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
}
</style>
