<template>


  <headerBar
    title="初始设备调频设置"
  />

  <div class="page">
  <el-row>
    <el-col :span="24"><h3>相机设置</h3></el-col>
  </el-row>
  <el-row>
    <el-col :span="6">相机高度mm：</el-col>
    <el-col :span="12"><el-input
        @change="changeNum('相机电机','move_deviation','camera_high_motor_deviation',0, 50)"
        :min="0" :max="50"
        :step="1"
        v-model="editRowData.camera_high_motor_deviation" type="number"/>
         <div class="error-msg">最小0，最大50</div>
    </el-col>
    <el-col :span="6"><el-button
        @click="changeNum('相机电机','set_deviation','camera_high_motor_deviation',0, 50)"
    >设定</el-button></el-col>
  </el-row>
  <el-row class="mar-top-10">
    <el-col :span="6">相机角度（度）：</el-col>

    <el-col :span="12"><el-input
        @change="changeNum('相机舵机','move_deviation','camera_steering_deviation',-90, 90)"
        :min="-90" :max="90"
        :step="0.1"
        v-model="editRowData.camera_steering_deviation" type="number"/>
      <div class="error-msg">最小-90，最大90</div>
    </el-col>
    <el-col :span="6"><el-button @click="changeNum('相机舵机','set_deviation','camera_steering_deviation',-90, 90)">设定</el-button></el-col>
  </el-row>



  <el-row>
    <el-col :span="24"><h3>转盘设置</h3></el-col>
  </el-row>
  <el-row>
    <el-col :span="6">角度偏移 (度)：</el-col>
    <el-col :span="12"><el-input
        @change="changeNum('转盘舵机','move_deviation','turntable_steering_deviation',-720, 720)"
        :min="-720" :max="720"
        :step="1"
        v-model="editRowData.turntable_steering_deviation" type="number"/>
      <div class="error-msg">最小-720，最大720</div>
    </el-col>
    <el-col :span="6"><el-button  @click="changeNum('转盘舵机','set_deviation','turntable_steering_deviation',-720, 720)">设定</el-button></el-col>
  </el-row>
  <el-row class="mar-top-10">
    <el-col :span="6">前后偏移：</el-col>

    <el-col :span="12"><el-input
        @change="changeNum('转盘前后电机','move_deviation','turntable_front_end_deviation',0, 950)"
        :min="0" :max="950"
        :step="1"
        v-model="editRowData.turntable_front_end_deviation" type="number"/>
      <div class="error-msg">最小0，最大950</div>
    </el-col>
    <el-col :span="6"><!--<el-button @click="changeNum('转盘前后电机','set_deviation','turntable_front_end_deviation',0, 950)">设定</el-button>--></el-col>
  </el-row>



  <el-row>
    <el-col :span="24"><h3>翻版舵机</h3></el-col>
  </el-row>
  <el-row>
    <el-col :span="6">中位：</el-col>

    <el-col :span="12"><el-input
        @change="changeNum('翻板舵机中位','move_deviation','overturn_steering_middle',0, 180)"
        :min="0" :max="180"
        :step="0.5"
        v-model="editRowData.overturn_steering_middle" type="number"/>
      <div class="error-msg">最小0，最大180</div>
    </el-col>
    <el-col :span="6"><el-button @click="changeNum('翻板舵机中位','set_deviation','overturn_steering_middle',0, 180)">设定</el-button></el-col>
  </el-row>
  <el-row class="mar-top-10">
    <el-col :span="6">高位：</el-col>
    <el-col :span="12"><el-input
        @change="changeNum('翻板舵机高位','move_deviation','overturn_steering_high',0, 180)"
        :min="0" :max="180"
        :step="0.5"
        v-model="editRowData.overturn_steering_high" type="number"/>
      <div class="error-msg">最小0，最大180</div>
    </el-col>
    <el-col :span="6"><el-button  @click="changeNum('翻板舵机高位','set_deviation','overturn_steering_high',0, 180)">设定</el-button></el-col>
  </el-row>
  <el-row class="mar-top-10">
    <el-col :span="6">上升速度：</el-col>
    <el-col :span="12"><el-input
        @change="changeNum('翻板舵机上升速度','move_deviation','overturn_steering_up_speed',0, 10)"
        :min="0" :max="10"
        :step="1"
        v-model="editRowData.overturn_steering_up_speed" type="number"/>
      <div class="error-msg">最小0，最大10</div>
    </el-col>
    <el-col :span="6"><el-button @click="changeNum('翻板舵机上升速度','set_deviation','overturn_steering_up_speed',0, 10)">设定</el-button></el-col>
  </el-row>
  <el-row class="mar-top-10">
    <el-col :span="6">下降速度：</el-col>
    <el-col :span="12"><el-input
        @change="changeNum('翻板舵机下降速度','move_deviation','overturn_steering_down_speed',0, 10)"
        :min="0" :max="10"
        :step="1"
        v-model="editRowData.overturn_steering_down_speed" type="number"/>
      <div class="error-msg">最小0，最大10</div>
    </el-col>
    <el-col :span="6"><el-button  @click="changeNum('翻板舵机下降速度','set_deviation','overturn_steering_down_speed',0, 10)">设定</el-button></el-col>
  </el-row>


  <el-row align="middle" justify="middle" class="mar-top-20 bottom-wrap">
    <el-col :span="24">
      <el-button type="primary" @click="connect_mcu__init">设备初始化</el-button>
      <el-button type="primary" @click="get_deviation">读取偏移量并运行</el-button>
      <el-button type="primary" @click="AllChangeNum">整体设定</el-button>
    </el-col>
  </el-row>
  </div>



</template>

<script setup lang="ts">

import {ref,reactive,onMounted} from "vue";

import client from "@/stores/modules/client";
import  icpList from '@/utils/ipc'
import socket from "@/stores/modules/socket";
import {ElMessage} from "element-plus";
const clientStore = client();
const socketStore = socket()


const editRowData = ref({
  "camera_high_motor_deviation": '',
  "camera_steering_deviation": '',
  "turntable_steering_deviation": '',
  "turntable_front_end_deviation":'',
  "overturn_steering_middle": '',
  "overturn_steering_high": '',
  "overturn_steering_up_speed": '',
  "overturn_steering_down_speed": ''
}); // 当前编辑行的数据

onMounted(()=>{
  get_deviation()
})

//获取配置
async function  get_deviation(){
  if(clientStore.isClient){
    socketStore.sendMessage({
      type: 'get_deviation',
      data:"get_deviation"
    })


    clientStore.ipc.on(icpList.socket.message+'_get_deviation_data', (event, result) => {
      console.log('_get_deviation_data')
      console.log(result)
      if(result.code === 0){
        editRowData.value.camera_high_motor_deviation = result.data.camera_high_motor_deviation
        editRowData.value.camera_steering_deviation = result.data.camera_steering_deviation
        editRowData.value.turntable_steering_deviation = result.data.turntable_steering_deviation
        editRowData.value.turntable_front_end_deviation = result.data.turntable_front_end_deviation

        editRowData.value.overturn_steering_middle = result.data.overturn_steering_middle
        editRowData.value.overturn_steering_high = result.data.overturn_steering_high
        editRowData.value.overturn_steering_up_speed = result.data.overturn_steering_up_speed
        editRowData.value.overturn_steering_down_speed = result.data.overturn_steering_down_speed



        changeNum('相机电机','move_deviation','camera_high_motor_deviation',0, 50)
        changeNum('相机舵机','move_deviation','camera_steering_deviation',-90, 90)
        changeNum('转盘舵机','move_deviation','turntable_steering_deviation',-720, 720)
        changeNum('转盘前后电机','move_deviation','turntable_front_end_deviation',0, 950)
        changeNum('翻板舵机中位','move_deviation','overturn_steering_middle',0, 180)
    //    changeNum('翻板舵机高位','move_deviation','overturn_steering_high',0, 180)
    //    changeNum('翻板舵机上升速度','move_deviation','overturn_steering_up_speed',0, 10)
     //   changeNum('翻板舵机下降速度','move_deviation','overturn_steering_down_speed',0, 10)
      }else if(result.msg){
        ElMessage.error(result.msg)
      }
      clientStore.ipc.removeAllListeners(icpList.socket.message+'_get_deviation_data');
    });
  }
}



//MCU初始化
async function  connect_mcu__init(){
  if(clientStore.isClient){

    socketStore.sendMessage({
      type: 'init_mcu',
      data:{
        value:true
      }
    })
  }
}

const allTipsCount = ref(0)
async  function  AllChangeNum (){
  allTipsCount.value = 7
  changeNum('相机电机','set_deviation','camera_high_motor_deviation',0, 50)
  changeNum('相机舵机','set_deviation','camera_steering_deviation',-90, 90)
  changeNum('转盘舵机','set_deviation','turntable_steering_deviation',-720, 720)
//  changeNum('转盘前后电机','set_deviation','turntable_front_end_deviation',0, 950)
  changeNum('翻板舵机中位','set_deviation','overturn_steering_middle',0, 180)
  changeNum('翻板舵机高位','set_deviation','overturn_steering_high',0, 180)
  changeNum('翻板舵机上升速度','set_deviation','overturn_steering_up_speed',0, 10)
  changeNum('翻板舵机下降速度','set_deviation','overturn_steering_down_speed',0, 10)

}


//设置 移动 调整
async function changeNum(action_name, type, key, min, max) {
  if(key && (min || max)){
    if(editRowData.value[key] < min || editRowData.value[key] > max){
      if(editRowData.value[key] < min){
        editRowData.value[key] = min;
      }else{
        editRowData.value[key] = max;
      }
      ElMessage.error(`${action_name}值应在${min}到${max}之间`);
    }
  }
  socketStore.sendMessage({
    type,
    data: {
      action_name,
      value:Number(editRowData.value[key]) || 0
    }
  });

  clientStore.ipc.removeAllListeners(icpList.socket.message+'_set_deviation');
  clientStore.ipc.on(icpList.socket.message+'_set_deviation', (event, result) => {
    console.log('set_deviation')
    console.log(result)
    if(result.code === 0){
      if(result.msg === "相机舵机 设置成功"){

        editRowData.value.camera_steering_deviation = 0
      }
      if(allTipsCount.value === 1){
        allTipsCount.value--;
        ElMessage.success('设定成功')
        return;
      }
      if(allTipsCount.value > 0){
        allTipsCount.value--;
        return;
      }
      ElMessage.success(result.msg)
    }else if(result.msg){
      ElMessage.error(result.msg)
    }
    clientStore.ipc.removeAllListeners(icpList.socket.message+'_get_deviation_data');
  });

}



</script>

<style scoped lang="scss">
.bottom-wrap {
  position: fixed;
  bottom:0px;
  left: 0;
  right: 0;
  padding: 10px 0;
  background: #fff;
}
.page {
  padding-bottom: 60px;
}
.el-col {
    position: relative;
  ::v-deep {
    .error-msg{
      display: none;
      position: absolute;
      top: 28px;
      left: 8px;
      z-index: 22;
      color: #dc2626;
      font-size: 12px;
    }
    &:hover{
      .error-msg{
        display: block;
      }
    }
  }
}
</style>
