<script>
import SiderleftSerilportMethod from "../SiderleftSerilportMethod.js";
import {invoke} from "@tauri-apps/api/tauri";
import EventBus from "../../eventBus.js";
import eventBus from "../../eventBus.js";

export default {
  name: "SetWorkmode",
  data() {
    return {
      Devinfo: {
        work_mode: 0,
      }
    }
  },
  mounted() {

    // this.GetSenSorInfo();
  },
  methods: {
    async GetSenSorInfo() {
      var Command = {command: "get_sensor_info"};
      let data = {
        data: Command,
        datatype: "json"
      }
      let message = await invoke("sendtoport_andgetreturn", data);
      this.Devinfo = JSON.parse(message.content);
      this.Devinfo.start_time = this.convertimetostr(this.Devinfo.start_time);
      this.Devinfo.stop_time = this.convertimetostr(this.Devinfo.stop_time);

    },
    coverstrtotime(timeStr) {
// 分离小时和分钟
      let [hours, minutes] = timeStr.split(':').map(Number);
// 计算数值
      let num = hours + minutes / 100;

      console.log(num);  // 输出 10.4
      return num;

    },
    convertimetostr(num) {

      // num=10.40
// 分离整数部分和小数部分
      let hours = Math.floor(num);
      let minutes = Math.round((num - hours) * 100);
      if (hours < 10) {
        hours = '0' + hours;
      }
// 格式化分钟部分为两位数
      if (minutes < 10) {
        minutes = '0' + minutes;
      }

// 构建时间字符串
      let timeStr = `${hours}:${minutes}`;
      return timeStr;
    },
    async setWorkmode() {
      console.log(this.Devinfo);
      //单次和连续都属于手动模式
      if (this.Devinfo.work_mode == "manual_mode") {
        let Command = {command: "set_work_mode",work_mode:"manual_mode"};
        await this.senddatatoserilport(Command);

        Command = {command: "set_time_interval",time_interval:this.Devinfo.time_interval};
        await this.senddatatoserilport(Command);
          //设置采集模式连续
          //  Command = {command: "set_caiji_mode",work_mode:"continue"};
          // await this.senddatatoserilport(Command);



      }
      //自动模式
      if (this.Devinfo.work_mode =="time_mode") {
        let Command = {command: "set_work_mode",work_mode:"time_mode"};
        await this.senddatatoserilport(Command);
        Command = {command: "set_time_interval",time_interval:this.Devinfo.time_interval};
        await this.senddatatoserilport(Command);
        Command = {command: "set_start_time",start_time:this.coverstrtotime(this.Devinfo.start_time)};
        await this.senddatatoserilport(Command);
        Command = {command: "set_stop_time",stop_time:this.coverstrtotime(this.Devinfo.stop_time)};
        await this.senddatatoserilport(Command);

      }
      if (this.Devinfo.work_mode =="advanced_mode") {
        let Command = {command: "set_work_mode",work_mode:"advanced_mode"};
        await this.senddatatoserilport(Command);
      }
      EventBus.emit("updatamaindevinfo");
      eventBus.emit('showbox', {title: "工作模式", body: "设置工作模式成功",interval:10});

    },
   async senddatatoserilport(command){
      let data = {
        data: command,
        datatype: "json"
      }
     await invoke("sendtoport_andgetreturn", data);


    }
  }

}
</script>

<template>
  <div style="position: relative;width: 100%;height: 100%">
    <div style="position:relative ;width: 60%;;margin: auto">

      <b-input-group class="my-1">
        <BInputGroupPrepend is-text class="myinputprepend">工作模式</BInputGroupPrepend>
        <b-form-select v-model="Devinfo.work_mode" >
          <b-form-select-option value="manual_mode">单次采集</b-form-select-option>
          <b-form-select-option value="time_mode">自动采集</b-form-select-option>
          <b-form-select-option value="advanced_mode">高级模式</b-form-select-option>
        </b-form-select>
      </b-input-group>

      <b-input-group class="my-1" append="秒" v-if="Devinfo.work_mode!='advanced_mode'">
        <BInputGroupPrepend is-text class="myinputprepend">采集间隔</BInputGroupPrepend>
        <BFormInput type="number" id="input-none" :state="null" v-model="Devinfo.time_interval"/>
      </b-input-group>
      <b-input-group class="my-1" v-if="Devinfo.work_mode=='time_mode'">
        <BInputGroupPrepend is-text class="myinputprepend">开始时间</BInputGroupPrepend>
        <BFormInput type="time" id="input-none1" :state="null" v-model="Devinfo.start_time"/>

      </b-input-group>
      <b-input-group class="my-1" v-if="Devinfo.work_mode=='time_mode'">
        <BInputGroupPrepend is-text class="myinputprepend">结束时间</BInputGroupPrepend>
        <BFormInput type="time" id="input-none2" :state="null" v-model="Devinfo.stop_time"/>
      </b-input-group>


    </div>
    <p style="position:absolute ;bottom: 10px;width: 100%;height: 30px">
      <b-button @click="setWorkmode" style="position:absolute;right: 20px ">设置</b-button>
    </p>
  </div>


</template>

<style scoped>
.myinputprepend {
  width: 120px;
}
</style>
