<template>
  <ifrm ref="ifrm">
    <!-- 挂备绑定-->
    <uni-forms ref="form" err-show-type="toast">
      <uni-forms-item
        ><input
          type="text"
          :focus="oneInputFcous"
          @input="oneInputBlur"
          v-model="oneInput"
          class="uni-input-border"
          placeholder="挂具"
      /></uni-forms-item>
      <uni-forms-item
        ><input
          type="text"
          :focus="twoInputFcous"
          v-model="twoInput"
          @input="twoInputBlur"
          class="uni-input-border"
          placeholder="设备"
      /></uni-forms-item>
    </uni-forms>
    <view class="tableBox">
      <uni-card title="设备信息">
        <t-table>
          <t-tr>
            <t-td>编号</t-td>
            <t-td>{{ facilityObj.deviceCode }}</t-td>
          </t-tr>
          <t-tr>
            <t-td>名称</t-td>
            <t-td>{{ facilityObj.deviceName }}</t-td>
          </t-tr>
          <t-tr>
            <t-td>状态</t-td>
            <t-td>{{ facilityObj.usedText }}</t-td>
          </t-tr>
        </t-table>
      </uni-card>
      <uni-card title="挂具信息">
        <uni-table border stripe emptyText="暂无更多数据" class="table">
          <uni-tr>
            <uni-th align="center">操作</uni-th>
            <uni-th align="center">编号</uni-th>
            <uni-th align="center">名称</uni-th>
            <uni-th align="center">状态</uni-th>
          </uni-tr>
          <uni-tr v-for="(item, index) in rackList" :key="index">
            <uni-td align="center" style="width: 200rpx"
              ><button
                size="mini"
                class="button"
                type="primary"
                @click="removeClick(item)"
              >
                删除
              </button></uni-td
            >
            <uni-td align="center">{{ item.rsCode }}</uni-td>
            <uni-td align="center">{{ item.rsType }}</uni-td>
            <uni-td align="center">{{ item.statusName }}</uni-td>
          </uni-tr>
        </uni-table>
      </uni-card>
    </view>
    <view class="buttonBox"
      ><button
        class="button"
        :disabled="Object.keys(facilityObj).length && !(rackList.length > 0)"
        type="primary"
        @click="submitClick"
      >
        绑定
      </button></view
    >
  </ifrm>
</template>

<script>
import ifrm from "@/pages/index/ifrm";
import tTable from "@/components/t-table/t-table.vue";
import tTh from "@/components/t-table/t-th.vue";
import tTr from "@/components/t-table/t-tr.vue";
import tTd from "@/components/t-table/t-td.vue";
import inputBlur from "@/mixin/inputBlur.js";
export default {
  mixins: [inputBlur],
  components: {
    ifrm,
    tTable,
    tTh,
    tTr,
    tTd,
  },
  data() {
    return {
      facilityObj: {},
      rackList: [],
      flag: false,
    };
  },
  methods: {
    oneInputData(code) {
      this.getData(code, 2);
    },
    twoInputData(code) {
      uni.hideKeyboard();
      this.getEcByDeviceCode(code);
    },
    getData(code, num) {
      let url = "pdaLoad/getBsRackSetByCode/" + code;
      this.oneInput = code;
      this.$u.api.getBsRackSetByCode(code).then((res) => {
        let data = res.data;
        var bool = true;
        this.rackList.forEach((item) => {
          if (item.id === data.id) {
            bool = false;
          }
        });
        if (bool) {
          this.rackList.unshift(data);
        }
        this.twoInputFcous = true;
        this.clearOneInput();
        uni.showToast({
          title: "数据获取成功",
          icon: "none",
        });
      });
      //   this.$ajax.request({
      //     url,
      //     method: "GET",
      //     success: (data) => {
      //   var bool = true;
      //   this.rackList.forEach((item) => {
      //     if (item.rsId === data.rsId) {
      //       bool = false;
      //     }
      //   });
      //   if (bool) {
      //     this.rackList.unshift(data);
      //   }
      //   this.twoInputFcous = true;
      //   this.clearOneInput();
      //   uni.showToast({
      //     title: "数据获取成功",
      //     icon: "none",
      //   });
      //     },
      //   });
    },

    getEcByDeviceCode(code) {
      this.twoInput = code;
      this.$u.api.getEcByDeviceCodeS({deviceCode:code}).then((res) => {
        let data = res.data;
        this.facilityObj = data;
        this.clearTwoInput();
        uni.showToast({
          title: "数据获取成功",
          icon: "none",
        });
        if (data.categorys == "烘箱" && data.status != 0) {
          setTimeout(() => {
            uni.showToast({
              title: "此烘箱处于非正常状态",
              duration: 3000,
              icon: "none",
            });
          }, 2000);
        }
      });
      //   this.$ajax.request({
      //     url: "pdaLoad/getEcByDeviceCode",
      //     method: "POST",
      //     data: {
      //       deviceCode: code,
      //     },
      //     success: (data) => {
      //   this.facilityObj = data;
      //   this.clearTwoInput();
      //   uni.showToast({
      //     title: "数据获取成功",
      //     icon: "none",
      //   });
      //   if (data.categorys == "烘箱" && data.status != 0) {
      //     setTimeout(() => {
      //       uni.showToast({
      //         title: "此烘箱处于非正常状态",
      //         duration: 3000,
      //         icon: "none",
      //       });
      //     }, 2000);
      //   }
      //     },
      //   });
    },
    submitClick() {
      if (this.flag) return;
      this.flag = true;
      let rsList = [];
      this.rackList.forEach((item) => {
        rsList.push(item.id);
      });
      this.$u.api
        .gjBindingEc({ rsList:rsList.join(','), ecId: this.facilityObj.id })
        .then((res) => {
          uni.showToast({
            title: "绑定成功",
          });
          this.facilityObj = {};
          this.rackList = [];
          this.clearInput();
          this.clearFcous();
        });
      //   this.$ajax.request({
      //     url: "pdaSave/gjBindingEc",
      //     method: "POST",
      //     data: {
      // rsList,
      // ecId: this.facilityObj.ecId,
      //     },
      //     success: (data) => {
      //   uni.showToast({
      //     title: "绑定成功",
      //   });
      //   this.facilityObj = {};
      //   this.rackList = [];
      //   this.clearInput();
      //   this.clearFcous();
      //     },
      //     fail: (err) => {
      //       this.flag = false;
      //     },
      //     complete: () => {
      //       this.flag = false;
      //     },
      //   });
    },
    removeClick(obj) {
      this.rackList.forEach((item, index) => {
        if (item.rsId == obj.rsId) {
          this.rackList.splice(index, 1);
        }
      });
    },
  },

  onNavigationBarButtonTap(btn) {
    this.$refs.ifrm.topMenuClick(btn);
  },
};
</script>

<style scoped lang="less">
.uni-forms-item {
  margin-bottom: 6px !important;
}
</style>