<template>
  <ifrm ref="ifrm">
    <!-- 设备绑定(工艺卡) -->
    <uni-forms ref="form">
      <uni-forms-item
        ><input
          type="text"
          v-model="oneInput"
          :focus="oneInputFcous"
          class="uni-input-border"
          @input="oneInputBlur"
          placeholder="流程卡"
      /></uni-forms-item>
      <uni-forms-item
        ><input
          type="text"
          :focus="twoInputFcous"
          v-model="twoInput"
          class="uni-input-border"
          @input="twoInputBlur"
          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="'流程卡 ' + planList.length + ' 批; ' + ' 总面积 ' + totalArea"
      >
        <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-th align="center">镀种</uni-th>
            <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-th align="center">当前工序</uni-th>
          </uni-tr>
          <uni-tr v-for="(item, index) in planList" :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.woCode }}</uni-td>
            <uni-td align="center">{{ item.partCode }}</uni-td>
            <uni-td align="center">{{ item.batchNo }}</uni-td>
            <uni-td align="center">{{ item.plate }}</uni-td>
            <uni-td align="center">{{ item.prodIdent }}</uni-td>
            <uni-td align="center">{{ item.makeQty }}</uni-td>
            <uni-td align="center">{{ item.makeTeam }}</uni-td>
            <uni-td align="center">{{ item.orders }}</uni-td>
            <uni-td align="center">{{ item.ppsName }}</uni-td>
          </uni-tr>
        </uni-table>
      </uni-card>
    </view>
    <view class="buttonBox"
      ><button
        class="button"
        :disabled="Object.keys(facilityObj).length && !(planList.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: {},
      planList: [],
      flag: false,
      totalArea: 0,
    };
  },

  methods: {
    removeClick(obj) {
      this.planList.forEach((item, index) => {
        if (item.id == obj.id) {
          this.planList.splice(index, 1);
        }
        this.setArea();
      });
    },
    setArea() {
      this.totalArea = 0;
      if (this.planList.length > 0) {
        this.planList.forEach((item) => {
          this.totalArea += item.makeQty * item.area;
        });
      }
    },
    oneInputData(code) {
      this.getData(code, 1);
    },
    twoInputData(code) {
      uni.hideKeyboard();
      this.getEcByDeviceCode(code);
    },
    getData(code, num) {
      //   let url = "pdaLoad/loadBindData/" + code;
      this.oneInput = code;
      this.$u.api.loadBindData(code).then((res) => {
        let data = res.data;
        var bool = true;
        this.planList.forEach((item) => {
          if (item.id === data.id) {
            bool = false;
          }
        });
        if (bool) {
          this.planList.unshift(data);
        }
        this.clearOneInput();
        this.setArea();
        uni.showToast({
          title: "数据获取成功",
          icon: "none",
        });
      });
      //   this.$ajax.request({
      //     url,
      //     method: "GET",
      //     success: (data) => {
      //       console.log(data);
      //   var bool = true;
      //   this.planList.forEach((item) => {
      //     if (item.id === data.bindModel.id) {
      //       bool = false;
      //     }
      //   });
      //   if (bool) {
      //     this.planList.unshift(data.bindModel);
      //   }
      //   this.clearOneInput();
      //   this.setArea();
      //   uni.showToast({
      //     title: "数据获取成功",
      //     icon: "none",
      //   });
      //     },
      //   });
    },
    getEcByDeviceCode(code) {
      this.twoInput = code;
      this.$u.api.getEcByDeviceCode(code).then((res) => {
        let data = res.data;
        this.facilityObj = data;
        this.clearTwoInput();
        if (data.categorys == "烘箱" && data.status != 0) {
          setTimeout(() => {
            uni.showToast({
              title: "此烘箱处于非正常状态",
              duration: 3000,
              icon: "none",
            });
          }, 2000);
        }
        uni.showToast({
          title: "数据获取成功",
          icon: "none",
        });
      });
      // this.$ajax.request({
      //   url: "pdaLoad/getEcByDeviceCode",
      //   method: "POST",
      //   data: {
      //     deviceCode: code,
      //   },
      //   success: (data) => {
      // this.facilityObj = data;
      // this.clearTwoInput();
      // if (data.categorys == "烘箱" && data.status != 0) {
      //   setTimeout(() => {
      //     uni.showToast({
      //       title: "此烘箱处于非正常状态",
      //       duration: 3000,
      //       icon: "none",
      //     });
      //   }, 2000);
      // }
      // uni.showToast({
      //   title: "数据获取成功",
      //   icon: "none",
      // });
      //   },
      // });
    },
    submitClick() {
      if (this.flag) return;
      this.flag = true;
      let planList = [];
      this.planList.forEach((item) => {
        planList.push(item.id);
      });
      this.$u.api
        .ecBindingCrad({ ecId: this.facilityObj.id, planListIds:planList.join(',') })
        .then((data) => {
          uni.showToast({
            title: "绑定成功",
          });
          this.clearInput();
          this.facilityObj = {};
          this.planList = [];
          this.clearFcous();

          this.setArea();
        });
      // this.$ajax.request({
      //   url: "pdaSave/ecBindingCrad",
      //   method: "POST",
      //   data: {
      //     ecId: this.facilityObj.ecId,
      //     planList,
      //   },
      //   success: (data) => {
      // uni.showToast({
      //   title: "绑定成功",
      // });
      // this.clearInput();
      // this.facilityObj = {};
      // this.planList = [];
      // this.clearFcous();

      // this.setArea();
      //   },
      //   fail: (err) => {
      //     this.flag = false;
      //   },
      //   complete: () => {
      //     this.flag = false;
      //   },
      // });
    },
  },
  onNavigationBarButtonTap(btn) {
    this.$refs.ifrm.topMenuClick(btn);
  },
};
</script>

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

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