<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>{{ 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>
      <t-table>
        <t-tr>
          <t-td>掺镀件面积</t-td>
          <t-td>
            <input type="text" v-model="formData.dopedParts" />
          </t-td>
        </t-tr>
        <t-tr>
          <t-td>网面积</t-td>
          <t-td>
            <input type="text" v-model="formData.net" />
          </t-td>
        </t-tr>
        <t-tr>
          <t-td>挂具面积</t-td>
          <t-td>
            <input type="text" v-model="formData.rack" />
          </t-td>
        </t-tr>
      </t-table>
      <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(feibaObj).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 {
      feibaObj: {},
      rackList: [],
      flag: false,
      formData: {
        dopedParts: null,
        net: null,
        rack: null,
      },
    };
  },
  methods: {
    oneInputData(code) {
      this.getData(code, 1);
    },
    twoInputData(code) {
      uni.hideKeyboard();
      this.getData(code, 2);
    },
    getData(code, num) {
      // let url = "";
      // if (num == 1) {
      //   url = "pdaLoad/getBsRackSetByCode/" + code;
      //   this.oneInput = code;
      // } else {
      //   url = "pdaLoad/getBsFeiBaSetByCode/" + code;
      //   this.twoInput = code;
      // }
      if (num == 1) {
        this.$u.api.getBsRackSetByCode(code).then((res) => {
          var bool = true;
          let data = res.data;
          console.log(data,'data',this.rackList);
          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",
          });
        });
      } else {
        this.$u.api.getBsFeiBaSetByCode(code).then((res) => {
          let data = res.data;
          this.feibaObj = data;
          this.clearTwoInput();

          uni.showToast({
            title: "数据获取成功",
            icon: "none",
          });
        });
      }
      //   this.$ajax.request({
      //     url,
      //     method: "GET",
      //     success: (data) => {
      //       console.log(data);
      //   if (num == 1) {
      //     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();
      //   } else {
      //     this.feibaObj = data;
      //     this.clearTwoInput();
      //   }
      //   uni.showToast({
      //     title: "数据获取成功",
      //     icon: "none",
      //   });
      //     },
      //   });
    },
    submitClick() {
      if (this.flag) return;
      this.flag = true;
      let rsList = [];
      this.rackList.forEach((item) => {
        rsList.push(item.id);
      });
      this.$u.api
        .gjBindingFb({
          rsList:rsList.join(','),
          fsId: this.feibaObj.id,
          ...this.formData
        })
        .then((res) => {
          uni.showToast({
            title: "绑定成功",
          });
          this.feibaObj = {};
          this.rackList = [];
          this.clearInput();
          this.clearFcous();
          this.formData = {
            dopedParts: null,
            net: null,
            rack: null,
          };
          this.flag = false;
        })
        .catch((err) => {
          this.flag = false;
        });
      //   this.$ajax.request({
      //     url: "pdaSave/gjBindingFb",
      //     method: "POST",
      //     data: {
      //       rsList,
      //       fsId: this.feibaObj.fsId,
      //       formData: this.formData,
      //     },
      //     success: (data) => {
      //   uni.showToast({
      //     title: "绑定成功",
      //   });
      //   this.feibaObj = {};
      //   this.rackList = [];
      //   this.clearInput();
      //   this.clearFcous();
      //   this.formData = {
      //     dopedParts: null,
      //     net: null,
      //     rack: null,
      //   };
      //     },
      //     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>