<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">
      <t-table>
        <t-tr>
          <t-td>设备编号</t-td>
          <t-td>{{ facilityObj.deviceCode }}</t-td>
        </t-tr>
        <t-tr>
          <t-td>此次绑定数量</t-td>
          <t-td>
            <input v-model="bindNum" type="number" />
          </t-td>
        </t-tr>
        <t-tr v-if="heatFlag">
          <t-td>设定温度(℃)</t-td>
          <t-td>
            <input v-model="heat" type="number" />
          </t-td>
        </t-tr>
        <t-tr v-if="range.length > 0">
          <t-td>对应操作</t-td>
          <t-td>
            <uni-data-select
              v-model="operate"
              :localdata="range"
              @change="change"
            ></uni-data-select>
          </t-td>
        </t-tr>
        <t-tr v-if="operate == '振光'">
          <t-td>振光针规格</t-td>
          <t-td>
            <input v-model="oneData" />
          </t-td>
        </t-tr>
        <t-tr v-if="ppsName == '玻璃饼排蜡玻化（箱式炉）'">
          <t-td>玻化温度</t-td>
          <t-td>
            <input v-model="oneData" />
          </t-td>
        </t-tr>
        <template v-if="ppsName == '玻璃饼印字'">
          <t-tr>
            <t-td>油墨物料号</t-td>
            <t-td>
              <input v-model="oneData" />
            </t-td>
          </t-tr>
          <t-tr>
            <t-td>印字板编号</t-td>
            <t-td>
              <input v-model="twoData" />
            </t-td>
          </t-tr>
        </template>
        <template v-if="operate == '配胶'">
          <t-tr>
            <t-td>灌封胶牌号</t-td>
            <t-td>
              <input v-model="oneData" />
            </t-td>
          </t-tr>
          <t-tr>
            <t-td>灌封胶批次号</t-td>
            <t-td>
              <input v-model="twoData" />
            </t-td>
          </t-tr>
          <t-tr>
            <t-td>组分A重量(g)</t-td>
            <t-td>
              <input v-model="threeData" />
            </t-td>
          </t-tr>
          <t-tr>
            <t-td>组分B重量(g)</t-td>
            <t-td>
              <input v-model="fourData" />
            </t-td>
          </t-tr>
        </template>
        <template v-if="ppsName == '玻璃饼压制'">
          <t-tr>
            <t-td>玻璃饼牌号</t-td>
            <t-td>
              <input v-model="oneData" />
            </t-td>
          </t-tr>
          <t-tr>
            <t-td>玻璃饼检验编号</t-td>
            <t-td>
              <input v-model="twoData" />
            </t-td>
          </t-tr>
          <t-tr>
            <t-td>制饼粉重(g)</t-td>
            <t-td>
              <input v-model="threeData" />
            </t-td>
          </t-tr>
          <t-tr>
            <t-td>模具编号</t-td>
            <t-td>
              <input v-model="fourData" />
            </t-td>
          </t-tr>
        </template>
      </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-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="!(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,
      ppsName: null,
      operate: null,
      range: [],
      bindNum: 0,
      heat: 200,
      heatFlag: false,
      oneData: null,
      twoData: null,
      threeData: null,
      fourData: null,
    };
  },

  methods: {
    removeClick(obj) {
      this.planList.forEach((item, index) => {
        if (item.id == obj.id) {
          this.planList.splice(index, 1);
        }

        this.setBindNum();
      });
    },
    oneInputData(code) {
      console.log(code);
      this.getData(code, 1);
    },
    twoInputData(code) {
      uni.hideKeyboard();
      this.getEcByDeviceCode(code);
    },

    change(e) {
      this.operate = e;
    },
    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;
        if (this.planList.length > 0) {
          if (this.ppsName != data.ppsName) {
            bool = false;
            uni.showToast({
              icon: "none",
              title: "此订单的加工工序与上订单不符!!!",
            });
          }
        }
        this.planList.forEach((item) => {
          if (item.id === data.id) {
            bool = false;
            uni.showToast({
              icon: "none",
              title: "此订单已存在!!!",
            });
          }
        });
        if (bool) {
          this.planList.unshift(data);
          this.setBindNum();
          this.ppsName = data.ppsName;
          this.setHeatFlag();
          if (this.ppsName == "玻璃封接清洗") {
            this.range = [
              {
                value: "清洗",
                text: "清洗",
              },
              {
                value: "烘干",
                text: "烘干",
              },
            ];
          } else if (this.ppsName == "玻璃饼振光") {
            this.range = [
              {
                value: "振光",
                text: "振光",
              },
              {
                value: "烘干",
                text: "烘干",
              },
            ];
          } else if (this.ppsName == "HD玻璃封接灌胶") {
            this.range = [
              {
                value: "I面涂覆",
                text: "I面涂覆",
              },
              {
                value: "I面固化",
                text: "I面固化",
              },
              {
                value: "II面涂覆",
                text: "II面涂覆",
              },
              {
                value: "II面固化",
                text: "II面固化",
              },
            ];
          } else if (this.ppsName == "真空炉灌胶") {
            this.range = [
              {
                value: "配胶",
                text: "配胶",
              },
              {
                value: "第一面灌胶前烘干",
                text: "第一面灌胶前烘干",
              },
              {
                value: "第一面真空灌封",
                text: "第一面真空灌封",
              },
              {
                value: "第一面固化",
                text: "第一面固化",
              },
              {
                value: "第二面灌胶前烘干",
                text: "第二面灌胶前烘干",
              },
              {
                value: "第二面真空灌封",
                text: "第二面真空灌封",
              },
              {
                value: "第二面固化",
                text: "第二面固化",
              },
            ];
          } else {
            this.range = [];
          }
          uni.showToast({
            title: "数据获取成功",
            icon: "none",
          });
        }
        this.clearOneInput();
      });
      //   this.$ajax.request({
      //     url,
      //     method: "GET",
      //     success: (data) => {
      //       // console.log(data);
      //   var bool = true;
      //   if (this.planList.length > 0) {
      //     if (this.ppsName != data.bindModel.ppsName) {
      //       bool = false;
      //       uni.showToast({
      //         icon: "none",
      //         title: "此订单的加工工序与上订单不符!!!",
      //       });
      //     }
      //   }
      //   this.planList.forEach((item) => {
      //     if (item.id === data.bindModel.id) {
      //       bool = false;
      //       uni.showToast({
      //         icon: "none",
      //         title: "此订单已存在!!!",
      //       });
      //     }
      //   });
      //   if (bool) {
      //     this.planList.unshift(data.bindModel);
      //     this.setBindNum();
      //     this.ppsName = data.bindModel.ppsName;
      //     this.setHeatFlag();
      //     if (this.ppsName == "玻璃封接清洗") {
      //       this.range = [
      //         {
      //           value: "清洗",
      //           text: "清洗",
      //         },
      //         {
      //           value: "烘干",
      //           text: "烘干",
      //         },
      //       ];
      //     } else if (this.ppsName == "玻璃饼振光") {
      //       this.range = [
      //         {
      //           value: "振光",
      //           text: "振光",
      //         },
      //         {
      //           value: "烘干",
      //           text: "烘干",
      //         },
      //       ];
      //     } else if (this.ppsName == "HD玻璃封接灌胶") {
      //       this.range = [
      //         {
      //           value: "I面涂覆",
      //           text: "I面涂覆",
      //         },
      //         {
      //           value: "I面固化",
      //           text: "I面固化",
      //         },
      //         {
      //           value: "II面涂覆",
      //           text: "II面涂覆",
      //         },
      //         {
      //           value: "II面固化",
      //           text: "II面固化",
      //         },
      //       ];
      //     } else if (this.ppsName == "真空炉灌胶") {
      //       this.range = [
      //         {
      //           value: "配胶",
      //           text: "配胶",
      //         },
      //         {
      //           value: "第一面灌胶前烘干",
      //           text: "第一面灌胶前烘干",
      //         },
      //         {
      //           value: "第一面真空灌封",
      //           text: "第一面真空灌封",
      //         },
      //         {
      //           value: "第一面固化",
      //           text: "第一面固化",
      //         },
      //         {
      //           value: "第二面灌胶前烘干",
      //           text: "第二面灌胶前烘干",
      //         },
      //         {
      //           value: "第二面真空灌封",
      //           text: "第二面真空灌封",
      //         },
      //         {
      //           value: "第二面固化",
      //           text: "第二面固化",
      //         },
      //       ];
      //     } else {
      //       this.range = [];
      //     }
      //     uni.showToast({
      //       title: "数据获取成功",
      //       icon: "none",
      //     });
      //   }
      //   this.clearOneInput();
      //     },
      //   });
    },
    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",
        });
      });
      //   this.$ajax.request({
      //     url: "pdaLoad/getEcByDeviceCode",
      //     method: "POST",
      //     data: {
      //       deviceCode: code,
      //     },
      //     success: (data) => {
      //       this.facilityObj = data;
      //       this.clearTwoInput();
      //       uni.showToast({
      //         title: "数据获取成功",
      //         icon: "none",
      //       });
      //     },
      //   });
    },
    submitClick() {
      if (this.flag) return;
      this.flag = true;
      if (this.range.length > 0 && !this.operate) {
        this.flag = false;
        return uni.showToast({
          title: "请选择对应操作",
          icon: "none",
        });
      }
      let planList = [];
      this.planList.forEach((item) => {
        planList.push(item.id);
      });
      this.$u.api
        .fbBindingSjCrad({
          ecId: this.facilityObj.id,
          planList,
          operate: this.operate,
          ppsName: this.ppsName,
          bindNum: this.bindNum,
          heat: this.heat,
          oneData: this.oneData,
          twoData: this.twoData,
          threeData: this.threeData,
          fourData: this.fourData,
        })
        .then((res) => {
          uni.showToast({
            title: "绑定成功",
          });
          this.clearInput();
          this.facilityObj = {};
          this.planList = [];
          this.clearFcous();
          this.range = [];
          this.operate = null;
          this.ppsName = null;
          this.bindNum = null;
          this.heat = null;
          this.oneData = null;
          this.twoData = null;
          this.threeData = null;
          this.fourData = null;

          this.flag = false
        }).catch(err=>{
            this.flag = false
        })
      //   this.$ajax.request({
      //     url: "pdaSave/sjEcBindingCard",
      //     method: "POST",
      //     data: {
      //   ecId: this.facilityObj.ecId,
      //   planList,
      //   operate: this.operate,
      //   ppsName: this.ppsName,
      //   bindNum: this.bindNum,
      //   heat: this.heat,
      //   oneData: this.oneData,
      //   twoData: this.twoData,
      //   threeData: this.threeData,
      //   fourData: this.fourData,
      //     },
      //     success: (data) => {
      //   uni.showToast({
      //     title: "绑定成功",
      //   });
      //   this.clearInput();
      //   this.facilityObj = {};
      //   this.planList = [];
      //   this.clearFcous();
      //   this.range = [];
      //   this.operate = null;
      //   this.ppsName = null;
      //   this.bindNum = null;
      //   this.heat = null;
      //   this.oneData = null;
      //   this.twoData = null;
      //   this.threeData = null;
      //   this.fourData = null;
      //     },
      //     fail: (err) => {
      //       this.flag = false;
      //     },
      //     complete: () => {
      //       this.flag = false;
      //     },
      //   });
    },
    setBindNum() {
      this.bindNum = 0;
      this.planList.forEach((item) => {
        this.bindNum += item.makeQty;
      });
    },
    setHeatFlag() {
      if (this.ppsName == "玻璃封接退火") {
        this.heatFlag = true;
      } else {
        this.heatFlag = 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>