<template>
  <div class="contractDetails">
    <page-title>返回</page-title>

    <div class="form">
      <div class="form_box">
        <dil-form :formId="228" v-model="form1" ref="from1"></dil-form>
      </div>
    </div>

     <div class="fromOther">
      <el-form :inline="true" class="demo-form-inline" label-width="80px">
        <el-form-item label="批次">
          <el-autocomplete
            class="inline-input"
            v-model="stateBatch"
            :fetch-suggestions="querySearchBatch"
            placeholder="请输入批次"
            :trigger-on-focus="false"
            @select="handleSelectBatch"
          >
            <template slot-scope="{ item }">
              <div class="name">{{ item.value}}</div>
            </template>
          </el-autocomplete>
        </el-form-item>
      </el-form>
    </div>

    <!-- 确定和取消 -->
    <div class="button_box">
      <el-button @click="onClickCancel">返回</el-button>
      <el-button type="primary" @click="onClickConfirm">确认</el-button>
    </div>
  </div>
</template>

<script>
import PageTitle from "@/components/Page/Title";
import { getCookie } from "@/utils/util.js";
export default {
  components: { PageTitle },
  data() {
    return {
      form1: {},
      stateBatch: null,
      batchId: "",
      value: "",
    };
  },
  mounted() {
    this.information();
  },
  methods: {
    information() {
      //渲染数据
      this.axios
        .post("/api/v1/wmsh/getResultById/" + this.$route.params.resultId)
        .then((res) => {
          res.data.data.forEach((e) => {
            this.form1 = e;
          });
          this.stateBatch = res.data.data[0].resultForeignShipName;
          this.batchId = res.data.data[0].batchId;
        });
    },
   
    //批次弹出层
    handleSelectBatch(item) {
      //  console.log("item",item);
      this.batchId = item.batchId;
      item.value = this.value;
    },
  
    //以下是发货单位边输边查搜索
    querySearchBatch(queryString, cb) {
      this.axios("/api/v1/uc/getBatchByLike?index=" + queryString).then(
        (res) => {
          if (res.data.code == "200") {
            // console.log(res);
            var restaurantsforeignShipName = res.data.data;
            var results = queryString
              ? restaurantsforeignShipName.filter(
                  this.createFilterConsignee(queryString)
                )
              : restaurantsforeignShipName;
            // 调用 callback 返回建议列表的数据
            cb(results);
          }
        }
      );
    },
    createFilterConsignee(queryString) {
      return (restaurantsforeignShipName) => {
        return (
          restaurantsforeignShipName.value
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) > -1
        );
      };
    },

    // 返回
    onClickCancel() {
      this.$router.go(-1);
    },
    // 确认
    onClickConfirm() {
      let mes = {
        resultId: this.$route.params.resultId,
        batchId: this.batchId,
        portId: this.form1.portId,
        resultMaterialNumber: this.form1.resultMaterialNumber,
        userId: getCookie("orgCode"),
      };
      function isNumber() {
        var value = mes.resultMaterialNumber;
        //验证是否为数字
        var patrn = /^(-)?\d+(\.\d+)?$/;
        if (patrn.exec(value) == null || value == "") {
          return false;
        } else {
          return true;
        }
      }
      var val = this.value;
      if (
        mes.batchId == null ||
        mes.portId == null ||
        mes.resultMaterialNumber == null
      )
        this.$message.error("存在空值！");
      else if (!isNumber(val)) this.$message.error("物资件数必须是数字");
      else
        this.axios
          .post("/api/v1/wmsh/updateWareHosingResult", mes)
          .then(() => {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
            this.$router.go(-1);
          });
    },
  },
};
</script>
<style lang="scss" scoped>
.contractDetails {
  .form {
    display: flex;
    .form_box {
      width: 320px;
      margin-left: 35%;
      margin-top: 30px;
      margin-right: 20px;
      .el-form {
        .preview-group {
          .el-form-item {
            .el-form-item__label {
              display: inline-block;
              width: 70px !important;
            }
            .el-form-item__content {
              .el-select {
                width: 250px;
              }
              .el-input {
                width: 250px;
              }
            }
          }
        }
      }
    }
  }
  .button_box {
    margin-left: 42%;
    margin-top: 55px;
  }
}
.fromOther{
  margin-left: 34.5%;
  width: 50%;
  .inline-input{
    width: 300px;
  }
}
</style>