<template>
  <ifrm ref="ifrm">
    <!-- 靶备绑定 -->
    <uni-forms ref="form" err-show-type="toast">
      <uni-forms-item name="allegation"
        ><input
          type="text"
          :focus="oneInputFcous"
          @input="oneInputBlur"
          v-model="oneInput"
          class="uni-input-border"
          placeholder="飞靶"
      /></uni-forms-item>
      <uni-forms-item name="allegation"
        ><input
          type="text"
          :focus="twoInputFcous"
          v-model="twoInput"
          @input="twoInputBlur"
          class="uni-input-border"
          placeholder="设备"
      /></uni-forms-item>
    </uni-forms>
    <view class="tableBox">
      <t-table>
        <t-tr>
          <t-td>解绑飞靶</t-td>
          <t-td>
            <switch :checked="unFsBool" @change="switchChange" />
          </t-td>
        </t-tr>
        <t-tr>
          <t-td>挂次号</t-td>
          <!-- <t-td><input type="text" v-model="hangNum" /></t-td> -->
          <t-td>
            <uni-data-select
              v-model="hangNum"
              :localdata="range"
              @change="change"
            ></uni-data-select>
          </t-td>
        </t-tr>
      </t-table>
      <uni-card title="飞靶信息">
        <t-table>
          <t-tr>
            <t-td>编号</t-td>
            <t-td>{{ feibaObj.fsCode }}</t-td>
          </t-tr>
          <t-tr>
            <t-td>名称</t-td>
            <t-td>{{ feibaObj.fsType }}</t-td>
          </t-tr>
          <t-tr>
            <t-td>状态</t-td>
            <t-td>{{ feibaObj.statusName }}</t-td>
          </t-tr>
        </t-table>
      </uni-card>
      <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>
    </view>
    <view class="buttonBox"
      ><button
        class="button"
        :disabled="
          !(
            !(Object.keys(feibaObj).length == 0) &&
            !(Object.keys(facilityObj).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: {},
      feibaObj: {},
      hangNum: "",
      unFsBool: false,
      range: [],
      flag: false,
    };
  },
  onNavigationBarButtonTap(btn) {
    this.$refs.ifrm.topMenuClick(btn);
  },
  methods: {
    change(e) {
      this.hangNum = e;
    },
    switchChange(e) {
      this.unFsBool = e.target.value;
    },
    oneInputData(code) {
      this.getData(code, 1);
    },
    twoInputData(code) {
      uni.hideKeyboard();
      this.getData(code, 2);
    },
    getData(code, num) {
      if (num == 1) {
        this.oneInput = code;
        this.$u.api.getBsFeiBaSetByCode(code).then((res) => {
          let data = res.data;
          this.feibaObj = data;
          this.twoInputFcous = true;
          this.clearOneInput();
          uni.showToast({
            title: "数据获取成功",
          });
        });
        // this.$ajax.request({
        //   url: "pdaLoad/getBsFeiBaSetByCode/" + code,
        //   method: "GET",
        //   success: (data) => {
        // this.feibaObj = data;
        // this.twoInputFcous = true;
        // this.clearOneInput();
        // uni.showToast({
        //   title: "数据获取成功",
        // });
        //   },
        // });
      } else {
        this.twoInput = code;
        this.$u.api.getEcByDeviceCodeS({deviceCode:code}).then((res) => {
          let data = res.data;
          this.facilityObj = data;
          this.clearTwoInput();
          this.getHangNumFun();
          uni.showToast({
            title: "数据获取成功",
          });
          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();
        // this.getHangNumFun();
        // uni.showToast({
        //   title: "数据获取成功",
        // });
        // if (data.categorys == "烘箱" && data.status != 0) {
        //   setTimeout(() => {
        //     uni.showToast({
        //       title: "此烘箱处于非正常状态",
        //       duration: 3000,
        //       icon: "none",
        //     });
        //   }, 2000);
        // }
        //   },
        // });
      }
    },
    getHangNumFun() {
      this.range = [];
      this.$u.api
        .getHangNum({ deviceCode: this.facilityObj.deviceCode })
        .then((res) => {
          console.log(data);
          let data = res.data;
          if (data.length > 0) {
            data.forEach((item) => {
              this.range.push({
                value: item.value,
                text: item.label,
              });
            });
          }
        });
      //   this.$ajax.request({
      //     url: "comBox/basic/getHangNum",
      //     method: "POST",
      //     data: {
      //       deviceCode: this.facilityObj.deviceCode,
      //     },
      //     success: (data) => {
      //     console.log(data);
      // if (data.length > 0) {
      //   data.forEach((item) => {
      //     this.range.push({
      //       value: item.value,
      //       text: item.label,
      //     });
      //   });
      // }
      //     },
      //   });
    },
    submitClick() {
      if (this.facilityObj.categorys == "产线设备" && this.hangNum == "") {
        return uni.showToast({
          icon: "none",
          title: "请选择对应的挂次号！！！",
        });
      }
      if (this.flag) return;
      this.flag = true;
      this.$u.api
        .fbBindingEc({
          fsId: this.feibaObj.id,
          ecId: this.facilityObj.id,
          hangNumId: this.hangNum,
          unFsBool: this.unFsBool ? 1 : 0,
        })
        .then((data) => {
          uni.showToast({
            title: "绑定成功",
          });
          this.facilityObj = {};
          this.feibaObj = {};
          this.hangNum = "";
          this.unFsBool = false;
          this.range = [];
          this.clearInput();
          this.clearFcous();
        })
        .catch((err) => {
          this.flag = false;
        });
      //   this.$ajax.request({
      //     url: "pdaSave/fbBindingEc",
      //     method: "POST",
      // data: {
      //   fsId: this.feibaObj.fsId,
      //   ecId: this.facilityObj.ecId,
      //   hangNum: this.hangNum,
      //   unFsBool: this.unFsBool,
      // },
      //     success: (data) => {
      //   uni.showToast({
      //     title: "绑定成功",
      //   });
      //   this.facilityObj = {};
      //   this.feibaObj = {};
      //   this.hangNum = "";
      //   this.unFsBool = false;
      //   this.range = [];
      //   this.clearInput();
      //   this.clearFcous();
      // },
      // fail: (err) => {
      //   this.flag = false;
      // },
      // complete: () => {
      //   this.flag = false;
      // },
      //   });
    },
  },
};
</script>

<style scoped lang="less">
.tabsBox {
  margin-top: 40rpx;
}

.uni-forms-item {
  margin-bottom: 6px !important;
}
</style>