<template>
  <div class="rightFullPopUp" v-if="modelValue" @click="handleClose">
    <div class="container" @click.stop>
      <div class="headerTittle">物料采集</div>
      <div class="header">
        <div class="inputBox">
          <ScanCodeInput v-model="scanCode" @keyup.enter="enterfnc" />
        </div>
      </div>
      <el-scrollbar>
        <div class="body">
          <div
            :class="successStaus(item.materialCode) ? 'infoBox' : 'infoBox bg'"
            v-for="(item, index) in scanData"
            @click="setSelectIndex(index)"
            :key="index"
          >
            <div class="leftInfo">
              <div class="titleText">
                {{ item.materialName }}({{ item.spec }})
              </div>
              <div class="describeText">{{ item.materialCode }}</div>
              <div class="describeText">批次号:</div>
              <template v-if="selectIndex == index">
                <ScanCodeInput v-model="item.batchNo" />
              </template>
              <div v-else class="titleText">{{ item.batchNo }}</div>
              <!-- <div class="describeText">批次号:xxx</div>
              <div class="describeText">计划编号：xxx</div>
              <div class="describeText">订单编号：xxx</div>
              <div class="describeText">入库单号：xxx</div> -->
            </div>
            <div class="rightInput">
              <div></div>
              <div class="operate">
                <div class="describeText operateText">录入数量</div>
                <template v-if="selectIndex == index">
                  <NumberInput v-model="item.num" :max="item.needNum" />
                </template>
                <div v-else class="titleText">{{ item.num }}</div>
              </div>
            </div>
          </div>
          <Empty v-if="scanData.length == 0" />
        </div>
      </el-scrollbar>
      <div class="bottomBtn">
        <el-button class="leftBtn" @click="handleClose">取消</el-button>
        <el-button class="rightBtn" @click="emits('submit')" type="primary"
          >确认</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup>
import {
  getInfo,
  itemRecordAdd,
  recordList,
  searchMaterial,
} from "@/api/prosteps/wuliaocaiji";
import { useProcessStore } from "@/store";
const scanCode = ref("");
const store = useProcessStore();
const scanCodeCopyValue = ref("");
const opCompentDataList = inject("opCompentDataList");
//获取当前扫码总物料code code 以及是否数量满足 status
const resArray = ref([]);
const successStaus = (code) => {
  let res = true;
  for (let i = 0; i < resArray.value.length; i++) {
    if (code == resArray.value[i].code) {
      res = resArray.value[i].status;
    }
  }
  return res;
};
const getRes = () => {
  console.log("111");
  resArray.value = [];
  opCompentDataList.value.forEach((item) => {
    resArray.value.push({
      code: item.itemCode,
      status: true,
      needNum: item.needNum,
    });
  });

  for (let i = 0; i < resArray.value.length; i++) {
    let num = 0;
    for (let j = 0; j < scanData.value.length; j++) {
      if (resArray.value[i].code == scanData.value[j].materialCode) {
        num += scanData.value[j].num;
      }
    }

    if (resArray.value[i].needNum <= num) {
      resArray.value[i].status = false;
    }
  }
};
const enterfnc = async () => {
  let str = scanCode.value;
  scanCodeCopyValue.value = scanCode.value;
  scanCode.value = "";
  const { code, data } = await searchMaterial({
    operationId: store.odersData.operationId,
    processId: store.scanInfo.id,
    seqNo: store.scanInfo.seqNo,
    scanCode: str,
    workOrderCode: store.odersData.workOrderCode,
  });
  if (code == "200") {
    let newData = data[0];
    for (let i = 0; i < resArray.value.length; i++) {
      if ((resArray.value[i].code = newData.materialCode)) {
        for (let i = 0; i < resArray.value.length; i++) {
          if (resArray.value[i].status == false) {
            ElMessage.error("该物料数量已经满足需求数量，请勿再次添加");
            return;
          }
        }
      }
    }
    newData.itemSeq = scanCodeCopyValue.value;
    scanData.value.push(newData);
  }
};
const emits = defineEmits(["update:modelValue", "submit"]);
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  seqNo: {
    type: String,
    default: "",
  },
});
const selectIndex = ref();
const setSelectIndex = (index) => {
  selectIndex.value = index;
};
const scanData = inject("scanData");
const scanSum = ref(0);
const handleClose = () => {
  emits("update:modelValue", false);
};
const showSeqNo = computed(() => {
  return props.seqNo;
});
watch(
  () => scanData.value,
  () => {
    getRes();
  },
  {
    deep: true,
  }
);
defineExpose({
  selectIndex,
});
</script>

<style lang="scss" scoped>
.header {
  @include flex;
  width: 100%;
  margin: $p10 0;

  .inputBox {
    width: 75%;
  }
}

.describeText {
  line-height: 25px !important;
}

.body {
  .bg {
    background-color: rgb(100, 187, 92) !important;
    div {
      color: white;
    }
  }
  .infoBox {
    width: 100%;
    height: 200px;
    background-color: white;
    border-radius: 16px;
    display: flex;
    justify-content: space-between;
    padding: $p20;
    background-color: white;
    border-radius: 16px;
    margin-bottom: $f10;

    .leftInfo {
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      div {
        text-align: left;
      }
    }

    .rightInput {
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .operate {
        .operateText {
          text-align: left;
        }
      }
    }
  }
}

.bottomBtn {
  width: 100%;
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: $p10 10% 0 10%;

  .leftBtn {
    height: 55px;
    width: 45%;
    border-radius: 16px;
    font-size: $f20;
    color: #0a59f7;
    background-color: #00000015;
    border: 0px;
  }

  .rightBtn {
    height: 55px;
    width: 45%;
    border-radius: 16px;
    font-size: $f20;
    color: white;
  }
}
</style>
