<template>
  <!-- 修改车皮出库实绩 -->
  <div class="editOutBoundWagon">
    <page-title>返回</page-title>

    <div class="form">
      <div class="form_box">
        <el-form :inline="true" class="demo-form-inline">
          <div class="preview-group">
            <el-form-item label="车皮">
              <el-input v-model="form.wagonNo"></el-input>
            </el-form-item>
            <el-form-item label="采购订单号">
              <el-input v-model="form.purchaseOrderNo" disabled></el-input>
            </el-form-item>
            <el-form-item label="是否拼装">
              <el-switch v-model="isAssembly"> </el-switch>
            </el-form-item>
            <el-form-item v-show="isAssembly" label="标重">
              <el-input type="number" v-model="form.tonnage"></el-input>
            </el-form-item>
            <el-form-item label="发站">
              <el-autocomplete
                class="inline-input"
                v-model="sendStation"
                :fetch-suggestions="querySearchSendStation"
                placeholder="请输入发站名称"
                disabled
                :trigger-on-focus="false"
                @select="handleSelectSendStation"
              >
                <template slot-scope="{ item }">
                  <div class="name">{{ item.arrivalName }}</div>
                </template>
              </el-autocomplete>
            </el-form-item>
            <el-form-item label="到站">
              <el-autocomplete
                class="inline-input"
                v-model="toTheStation"
                disabled
                :fetch-suggestions="querySearchToTheStation"
                placeholder="请输入到站名称"
                :trigger-on-focus="false"
                @select="handleSelectToTheStation"
              >
                <template slot-scope="{ item }">
                  <div class="name">{{ item.arrivalName }}</div>
                </template>
              </el-autocomplete>
            </el-form-item>
            <el-form-item label="备注">
              <el-input type="textarea" v-model="form.resultMarks"></el-input>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>
    <!-- 确定和取消 -->
    <div class="button_box">
      <el-button @click="cancel">返回</el-button>
      <el-button type="primary" @click="makeSure">确认</el-button>
    </div>
  </div>
</template>

<script>
import PageTitle from "@/components/Page/Title";
import { sjTime, isNumber } from "@/utils/sharedJsFile";
import { getCookie } from "@/utils/util.js";
export default {
  components: { PageTitle },
  data() {
    return {
      form: {},
      wagonNo: null,
      tonnage: null,
      isAssembly: false,
      sendStationId: 4, //发站id和到站id都写死了，并且赋了港口名，如果数据库id变化，要注意
      toTheStationId: 1,
      sendStation: "万州港",
      toTheStation: "老区轨道衡",
    };
  },
  mounted() {
    this.information();
  },
  methods: {
    information() {
      console.log("resultId" + this.$route.params.resultId);
      //编辑车皮装车作业
      this.axios
        .post(
          "/api/v1/tms/getWareHouseOutresult/" + this.$route.params.resultId
        )
        .then((res) => {
          res.data.data.forEach((e) => {
            this.form = e;
            this.wagonNo = e.wagonNo;
            this.tonnage = e.tonnage;
            console.log(e);
          });
        });
    },
    handleCheckedCitiesChange(value) {
      console.log(value);
    },
    // 返回
    cancel() {
      this.$router.go(-1);
    },
    currentRadioChange(row) {
      this.aaaa = row;
    },
    //发站弹出层
    handleSelectSendStation(item) {
      this.sendStationId = item.arrivalId;
      this.sendStation = item.arrivalName;
    },
    //以下是发站边输边查搜索
    querySearchSendStation(queryString, cb) {
      this.axios
        .get("/api/v1/uc/getArrivalByLike?index=" + this.sendStation)
        .then((res) => {
          if (res.data.code == "200") {
            var restaurantsSupplier = res.data.data;
            console.log(restaurantsSupplier);
            var results = queryString
              ? restaurantsSupplier.filter(
                  this.createFilterSendStation(queryString)
                )
              : restaurantsSupplier;
            // 调用 callback 返回建议列表的数据
            cb(results);
          }
        });
    },
    //发站
    createFilterSendStation(queryString) {
      return (restaurantsSupplier) => {
        return (
          restaurantsSupplier.arrivalName
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) > -1
        );
      };
    },
    //到站弹出层
    handleSelectToTheStation(item) {
      this.toTheStationId = item.arrivalId;
      this.toTheStation = item.arrivalName;
    },
    //以下是到站边输边查搜索
    querySearchToTheStation(queryString, cb) {
      this.axios
        .get("/api/v1/uc/getArrivalByLike?index=" + this.toTheStation)
        .then((res) => {
          if (res.data.code == "200") {
            var restaurantsSupplier = res.data.data;
            console.log(restaurantsSupplier);
            var results = queryString
              ? restaurantsSupplier.filter(
                  this.createFilterToTheStation(queryString)
                )
              : restaurantsSupplier;
            // 调用 callback 返回建议列表的数据
            cb(results);
          }
        });
    },
    //到站
    createFilterToTheStation(queryString) {
      return (restaurantsSupplier) => {
        return (
          restaurantsSupplier.arrivalName
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) > -1
        );
      };
    },
    // 确认
    makeSure() {
     
      let map = {
        loadingTempId: this.$route.params.resultId,
        OutResultId: this.form.OutResultId,
        isAssembly: this.isAssembly,
        wagonNo: this.wagonNo,
        wagonNew: this.form.wagonNo,
        batchId: this.form.batchId,
        portId: this.form.portId,
        wagonWeight: this.tonnage,
        wagonWeightNew: this.form.tonnage,
        resultRemarks: this.form.resultRemarks,
        purchaseOrderNo: this.form.purchaseOrderNo,
        userId: getCookie("userId"),
        resultType: 1,
      };
      console.log(map);
      if (
        map.loadingTempId == null ||
        map.wagonNew == null ||
        map.purchaseOrderNo == null ||
        (map.isAssembly && map.wagonWeightNew == null)
      )
        this.$message.error("必填项存在空值！");
      else
        this.axios
          .post("/api/v1/tms/updateWarehouseResult", map)
          .then((res) => {
            if (res.data.code == "200") {
              this.$message({
                type: "success",
                message: "修改成功!",
              });
              this.$router.go(-1);
            } else {
              this.$message({
                type: "error",
                message: res.data.data,
              });
            }
          });
    },
  },
};
</script>
<style lang="scss">
.editOutBoundWagon {
  .form {
    display: flex;
    .form_box {
      width: 340px;
      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;
              }
              .el-textarea {
                .el-textarea__inner {
                  width: 220px;
                  margin-left: 65px;
                  margin-top: 0.03rem;
                }
              }
            }
          }
        }
      }
    }
  }
  .button_box {
    margin-left: 42%;
    margin-top: 55px;
  }
  .fromOther .el-input__inner {
    width: 250px;
  }
}
</style>