<template>
  <view>
    <connectingDevice ref="refCD" @onFeedback="onFeedback" />
    <block v-for="(item, index) in list" :key="item.w_functionid">
      <view
        v-if="item.paramName != 'E013'"
        class="item"
        :hover-class="item.inputType == 'switch' ? '' : 'navigator-hover'"
        @click="item.inputType == 'switch' ? '' : onClick(item)"
      >
        <view class="label">
          {{ item.funcname }}
          <block v-if="item.inputType == 'radio'">
            {{ radioName(item) }}
          </block>
          <block v-if="item.inputType != 'switch'">
            ：{{
              item.inputType == "radio"
                ? item.showValue || "未设置"
                : item.params.lastvalue || "未设置"
            }}
            <text v-if="item.params.unit" style="margin-left: 2px"
              >({{ item.params.unit }})</text
            >
          </block>
        </view>
        <view>
          <block v-if="item.inputType == 'switch'">
            <u-switch
              :value="item.params.lastvalue == 1"
              asyncChange
              size="24"
              :loading="item.loading"
              @change="changeSwitch(item, index)"
            />
          </block>
          <block v-else>
            <u-loading-icon v-if="item.loading" />
            <text
              v-else
              class="iconfont icon-a-wodetiaozhuan"
              style="color: #fff"
            />
          </block>
        </view>
      </view>
    </block>
    <My_input mode="1" ref="MyInput" @sendMsg="onSendMsg" />
  </view>
</template>

<script>
export default {
  name: "prodnum-FW01",
  components: {},
  props: {
    control: Object,
  },
  data() {
    return {
      list: [],
    };
  },
  watch: {
    control: function (newVal) {
      if (newVal) {
        let names = [];
        for (const key in newVal.function) {
          names.push(key);
        }
        this.list = this.__proto__.getControlItem(names, newVal, {
          安装位置: "radio",
          第二域名: "string",
        });
        console.log(this.list);
        try {
          const E013 = this.list.find((v) => v.paramName == "E013");
          if (E013) {
            this.$refs.refCD.send(
              { password: E013.params.options[0].value },
              E013
            );
          }
        } catch (error) {}

        this.$refs.refCD.init();
      }
    },
  },
  methods: {
    onClick(item) {
      this.$refs.MyInput.openInput(item);
    },
    radioName(item) {
      try {
        item.showValue =
          item.params.options.find((v) => v.value == item.params.lastvalue)
            .label || "";
      } catch (error) {
        item.showValue;
      }
      return "";
    },
    changeSwitch(item, index) {
      const value = item.params.lastvalue == 1 ? 0 : 1;
      let msg = {
        d: {
          [item.params.param]: value,
        },
      };
      this.$refs.refCD.send(msg, item);
      this.$set(this.list[index], "loading", true);
    },
    onSendMsg(w_functionid, d) {
      const index = this.list.findIndex((v) => v.w_functionid == w_functionid);
      let msg = {
        d,
      };
      this.$refs.refCD.send(msg, this.list[index]);
      this.$set(this.list[index], "loading", true);
    },
    onFeedback(res) {
      console.log("onFeedback", res);

      const index = res.node
        ? this.list.findIndex((v) => res.node == v.paramName)
        : this.list.findIndex((v) => v.msgid == res.msgid);

      if (index != -1) {
        let item = this.list[index];
        let MyInput = this.$refs.MyInput;
        MyInput.loading = false;
        if (res.status == 1) {
          if (res.node) {
             item.params.lastvalue = res.v;
          } else if (res.msgid) {
            item.params.lastvalue = item.msg.d[item.params.param];
          }
          MyInput.show = false;
          MyInput.stepIsCalculate = null;
        } else if (res.status == 0) {
          if (item.paramName == "E013") {
            uni.showModal({
              title: "失败",
              content: `身份验证失败,将断开连接`,
              showCancel: false,
            });
            uni.closeBLEConnection({
              deviceId: that.connected
            });
            uni.closeBluetoothAdapter();
          } else {
            uni.showModal({
              title: "失败",
              content: `“${item.funcname}”设置失败`,
              showCancel: false,
            });
          }
        }
        MyInput.$refs.uModal.loading = false;
        delete item.loading;
        // delete item.msgid;
        // delete item.msg;

        this.$set(this.list, index, item);
        console.log(item);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 10px 0;
  box-sizing: border-box;
  padding: 16px 20px;
  border-radius: 6px;
  background: rgba($color: #ffffff, $alpha: 0.1);
  .label {
    color: #fff;
    font-size: 14px;
    width: 230px;
  }
}
</style>