<template>
  <ifrm ref="ifrm">
    <!-- 流程卡-挂具绑定 -->
    <u-sticky offset-top="0">
      <uni-forms ref="wrForm" class="formBox">
        <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>
    </u-sticky>

    <view class="tableBox">
      <uni-card title="挂具信息">
        <t-table>
          <t-tr>
            <t-td>编号</t-td>
            <t-td>{{ bsRackSet.rsCode }}</t-td>
          </t-tr>
          <t-tr>
            <t-td>名称</t-td>
            <t-td>{{ bsRackSet.rsType }}</t-td>
          </t-tr>
          <t-tr>
            <t-td>状态</t-td>
            <t-td>{{ bsRackSet.statusName }}</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 bottomFixed">
      <button
        class="button"
        type="primary"
        :disabled="Object.keys(this.bsRackSet).length && !(planList.length > 0)"
        @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 {
      bsRackSet: {},
      planList: [],
      flag: false,
      totalArea: 0,
    };
  },
  methods: {
    oneInputData(code) {
      this.getData(code, 1);
    },
    twoInputData(code) {
      uni.hideKeyboard();
      this.getData(code, 2);
    },
    setArea() {
      this.totalArea = 0;
      if (this.planList.length > 0) {
        this.planList.forEach((item) => {
          console.log(item, "item");
          this.totalArea += item.makeQty * item.area;
        });
      }
    },
    getData(code, num) {
      // let url = "";
      // if (num == 1) {
      //   url = "pdaLoad/loadBindData/" + code;
      //   this.oneInput = code;
      // } else {
      //   url = "pdaLoad/getBsRackSetByCode/" + code;
      //   this.twoInput = code;
      // }
      if (num == 1) {
        this.$u.api.loadBindData(code).then((res) => {
          let data = res.data;
          if (num == 1) {
            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",
          });
        });
      } else {
        this.$u.api.getBsRackSetByCode(code).then((res) => {
          let data = res.data;
          this.bsRackSet = 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.planList.forEach((item) => {
      //     if (item.id === data.bindModel.id) {
      //       bool = false;
      //     }
      //   });
      //   if (bool) {
      //     this.planList.unshift(data.bindModel);
      //   }
      //   this.clearOneInput();
      //   this.setArea();
      // } else {
      //   this.bsRackSet = data;
      //   this.clearTwoInput();
      // }
      // uni.showToast({
      //   title: "数据获取成功",
      //   icon: "none",
      // });
      //   },
      // });
    },
    removeClick(obj) {
      this.planList.forEach((item, index) => {
        if (item.id == obj.id) {
          this.planList.splice(index, 1);
        }

        this.setArea();
      });
    },
    async submitClick() {
      if (this.flag) return;
      this.flag = true;
      let planList = [];
      this.planList.forEach((item) => {
        planList.push(item.id);
      });
      this.$u.api
        .gjBindingCrad({ ...this.bsRackSet,planListIds: planList.join(',') })
        .then((res) => {
          uni.showToast({
            title: "绑定成功",
          });
          this.bsRackSet = {};
          this.planList = [];
          this.clearInput();
          this.clearFcous();

          this.setArea();
          this.flag = false;
        })
        .catch((err) => {
          this.flag = false;
        });
      // this.$ajax.request({
      //   url: "pdaSave/gjBindingCrad",
      //   method: "POST",
      //   data: {
      //     rsId: this.bsRackSet.rsId,
      //     planList,
      //   },
      //   success: (data) => {
      // uni.showToast({
      //   title: "绑定成功",
      // });
      // this.bsRackSet = {};
      // this.planList = [];
      // this.clearInput();
      // this.clearFcous();

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

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