<template>
  <div class="gpsbuttonBox">
    <el-button type="primary" @click="handleShowGPS">获取GPS位置 </el-button>
    <el-dropdown class="dropgps" trigger="click" @command="handleCommandGps">
      <el-button type="primary">
        GPS开关<i class="el-icon-arrow-down el-icon--right"></i>
      </el-button>
      <el-dropdown-menu slot="dropdown" class="gpsdropStyle">
        <el-dropdown-item command="open">开启GPS</el-dropdown-item>
        <el-dropdown-item command="close">关闭GPS</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <gps-position ref="gpsdialog_ref"></gps-position>
  </div>
</template>
<script>
// import infoDialog from "./infoDialog.vue";
import gpsPosition from "./gpsPosition.vue";
import { getTermStatus, setTermGPSJoggle } from "@/utils/api/index";
export default {
  components: {
    gpsPosition,
  },
  data() {
    return {};
  },
  watch: {
    termId: {
      handler(newVal, oldVal) {},
      deep: true,
      immediate: true,
    },
    cmdId: {
      handler(newVal, oldVal) {},
      deep: true,
      immediate: true,
    },
  },
  computed: {
    termId() {
      return this.$store.state.termId;
    },
    cmdId() {
      return this.$store.state.cmdId;
    },
  },
  methods: {
    //切换gps开启关闭
    handleCommandGps(command) {
      switch (command) {
        case "open": //开启
          this.changeGPS(1, this.cmdId);
          break;
        case "close": //关闭
          this.changeGPS(0, this.cmdId);
          break;
      }
    },
    changeGPS(val, cmdid) {
      console.log(val, cmdid);
      getTermStatus({ termId: this.termId }).then((res) => {
        console.log(res);
        if (res.data.isonline) {
          setTermGPSJoggle({ gpsstatus: val, cmdId: cmdid })
            .then((res) => {
              console.log(res);
              if (val == 0) {
                this.$message({
                  duration: 1500,
                  message: "GPS已关闭",
                  type: "warning",
                  showClose: true,
                });
              } else {
                this.$message({
                  duration: 1500,
                  message: "成功开启GPS",
                  type: "success",
                  showClose: true,
                });
              }
            })
            .catch((err) => {
              console.log(err); //代码错误、请求失败捕获
            });
        } else {
          this.$message({
            duration: 1500,
            showClose: true,
            message: "装置下线，发送指令失败",
            type: "error",
          });
        }
      });
    },
    //获取GPS位置
    handleShowGPS() {
      this.$refs.gpsdialog_ref.display(this.cmdId, this.termId);
      this.$refs.gpsdialog_ref.getgpssql();
    },
  },
};
</script>
<style lang="less">
.gpsbuttonBox {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
</style>
