<template>
  <div v-loading="loading" style="padding: 16px;min-height: calc(100vh - 84px);display: flex; justify-content: center;align-items: center;">
    <div class="div-box">
      <div class="div-box-title"> 智能生猪饲喂系统 </div>
      <div class="div-ul">
        <div class="div-li">
          <div>
            <span>当前重量：</span>{{deviceInfo.weight || '-'}}g
          </div>
        </div>
        <!--   ON：开启 OFF：关闭 -->
        <div class="div-li">
          <div>
            <span>电机状态：</span>
            <el-switch disabled v-model="value2" active-color="#13ce66" inactive-color="#ff4949">
            </el-switch>
          </div>
        </div>
      </div>
      <div class="div-tips"><span>阈值设定：</span>{{deviceInfo.threshold || '-'}} <i class="el-icon-edit"
          style="color: rgb(64, 158, 255);margin-left: 4px;cursor: pointer;" @click="handleSettings"></i></div>
    </div>
    <!-- 添加或修改公告对话框 -->
    <el-dialog title="阈值设定" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="阈值" prop="threshold">
          <el-input v-model="form.threshold" placeholder="请输入阈值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryProjectInfoByKey, deviceList, attributes, sendAttribute, sendCommand } from "@/api/school";
export default {
  name: "Index",
  data() {
    return {
      deviceId: "", // 设备id 
      deviceInfo: {
        weight: 0, // 重量
        threshold: 0, // 阈值
      },
      open: false,
      value2: false,
      loading: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        threshold: [
          { required: true, message: "阈值不能为空", trigger: "blur" }
        ],
      },
      // 版本号
      version: "3.8.9",
    };
  },
  created() {
    this.getqueryProjectInfoByKey() // 根据项目key查找项目id
  },
  methods: {
    // 阈值设定弹窗
    handleSettings() {
      this.open = true;
      this.form = {
        threshold: this.deviceInfo.threshold,
      };
      // this.$message.success('');
    },

    // 根据项目key查找项目id
    getqueryProjectInfoByKey() {
      this.loading = true;
      queryProjectInfoByKey({
        projectKey: "qKStHKKt"
      }).then(res => {
        this.getDeviceList(res.data.id) // 根据项目id查找设备列表
      });
    },
    // 根据项目id查找设备列表
    getDeviceList(_id) {
      deviceList({
        projectId: _id
      }).then(res => {
        const w2Device = res.rows.find(device => device.deviceName === "P1");
        this.deviceId = w2Device.id;
        this.getAttributes(this.deviceId); // 根据设备id查找设备信息
      })
    },

    // 根据设备id查找设备信息
    getAttributes(_id) {
      attributes({
        deviceId: _id,
      }).then(res => {
        this.loading = false;
        // 获取重量和阈值信息，并赋值给deviceInfo
        let deviceAttr = res.data.deviceAttributesList
        let weight = deviceAttr.find(row => row.id === 447247498590464)?.attributeValue;
        let threshold = deviceAttr.find(row => row.id === 447247498590465)?.attributeValue;
        console.log('threshold',threshold);
        
        // 阈值 threshold 特殊处理
        if (threshold && threshold.indexOf("#") !== -1) {
          threshold = threshold.slice(1); // 去掉#
        }
        this.deviceInfo = {
          weight: weight,
          threshold: threshold,
        }

        // 根基重量值是否低于阈值，设置报警器、加热器的状态
        this.value2 = false;
        // 转为数字类型判断
        this.deviceInfo.weight = Number(this.deviceInfo.weight);
        this.deviceInfo.threshold = Number(this.deviceInfo.threshold);
        if (this.deviceInfo.weight < this.deviceInfo.threshold) {
          this.value2 = true;
        }
      });
    },
    /** 下发属性 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          sendAttribute({
            "attributeAndValues": [
              {
                "attributeIdentifier": "Value",
                "attributeValue": this.form.threshold
              }
            ],
            "deviceId": this.deviceId // 设备id
          }).then(response => {
            this.getAttributes(this.deviceId); // 根据设备id查找设备信息
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            
          });
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.div-box {
  transform: scale(1.4);
  width: 500px;
  box-sizing: border-box;
  padding: 16px;
  border: 1px solid #ccc;
  border-radius: 10px;

  .div-box-title {
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 20px;
    text-align: center;
    border-bottom: 1px solid #ccc;
    padding-bottom: 16px;
  }
}

.div-ul {
  // .div-li>div:nth-of-type(1) {
  //   border-right: 1px solid #ccc;
  //   margin-right: 16px;
  // }

  .div-li {
    font-size: 18px;
    display: flex;
    justify-content: space-between;
    margin-bottom: 12px;

    div {
      flex: 1;
      align-items: center;
    }

    span {
      color: #666;
    }
  }
}

.div-tips {
  font-size: 18px;
  line-height: 22px;
  display: flex;
  align-items: center;

  span {
    color: #666;
  }
}
</style>
