<template>
  <!-- 装船指令新增页面 -->
  <div class="shipTransport">
    <page-title>返回</page-title>
    <div class="form">
      <div class="form_box">
        <dil-form :formId="114" v-model="form1" ref="from1"></dil-form>
        <span> 二程:海港-江港</span>
        <el-switch
          v-model="status"
          active-color="#13ce66"
          inactive-color="#909399"
          @change="changeStatus"
        >
        </el-switch>
      </div>
      <div class="liulan">
        <el-button type="primary" class="btn" @click="onDrawer">浏览</el-button>
      </div>
    </div>
    <!-- 确定和取消 -->
    <div class="button_box">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="makeSure">确定</el-button>
    </div>
    <!-- 模态框 -->
    <el-drawer
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
      size="35%"
    >
      <el-input
        placeholder="请输入内容"
        v-model="input"
        style="margin-top: 10px; margin-left: 20px; width: 250px"
        clearable
      >
      </el-input>
      <el-button
        type="primary"
        class="btn"
        @click="onclick()"
        style="margin-bottom: 15px"
      >
        <i class="el-icon-search"></i>查询
      </el-button>
      <dilTable
        v-bind.sync="options"
        @radio-change="currentRadioChange1"
      ></dilTable>
    </el-drawer>
  </div>
</template>

<script>
import PageTitle from "@/components/Page/Title";
import { sjTime, isNumber } from "@/utils/sharedJsFile";
export default {
  components: { PageTitle },
  data() {
    return {
      drawer: false,
      direction: "rtl",
      input: "",
      options: {
        requestUrl: "/api/v1/tms/getBatchListForInstruction?apiId=383",
        selectionType: "radio",
        maplist: [],
      },
      status: false,
      form1: {},
      value: undefined,
      batchId: "",
    };
  },
  mounted() {},
  methods: {
    onDrawer() {
      this.drawer = true;
    },
    changeStatus() {
      // this.status=!this.status;
      console.log(this.status);
      // console.log("aaa",this.status);
    },

    onclick() {
      this.options.requestUrl =
        "/api/v1/tms/getBatchListForInstruction?apiId=383&con=" + this.input;
    },
    currentRadioChange1(selection) {
      this.maplist = selection;
      if (JSON.stringify(this.maplist).length > 2) {
        this.$set(
          this.form1,
          "batchId",
          selection.resultForeignShipName +
            "-" +
            selection.materialName +
            "-" +
            selection.infactoryShipName
        );
        this.batchId = selection.batchId;
      }
    },
    handleClose(done) {
      done();
    },
    // 确定
    makeSure() {
      if (this.batchId == "" || this.batchId == undefined) {
        this.$message.warning("请选择批次号");
        return;
      }
      let omsshipShipmentInstructions = {
        batchId: this.maplist.batchId,
        instructionsTotalTonnage: this.form1.instructionsTotalTonnage,
        instructionTime: sjTime(this.form1.instructionTime),
        statu: this.status,
        transportPortName: this.form1.transportPortName,
        handoverMethod: this.form1.handoverMethod,
      };

      //判断合计吨位是否为数字
      function isNumber() {
        var value = omsshipShipmentInstructions.instructionsTotalTonnage;
        //验证是否为数字
        var patrn = /^(-)?\d+(\.\d+)?$/;
        if (patrn.exec(value) == null || value == "") {
          return false;
        } else {
          return true;
        }
      }
      var val = this.value;

      if (
        omsshipShipmentInstructions.batchId == null ||
        omsshipShipmentInstructions.instructionsTotalTonnage == null ||
        omsshipShipmentInstructions.instructionTime == null
      )
        this.$message.error("存在空值！");
      else if (!isNumber(val)) this.$message.warning("合计吨位请填写数字");
      else
        this.axios
          .post(
            "/api/v1/tms/addShipmentInstructions",
            omsshipShipmentInstructions
          )
          .then((res) => {
            if (res.data.code == "200") {
              this.$message({
                type: "success",
                message: "新增成功",
              });
            }
            this.$router.go(-1);
          });
    },
    // 取消
    cancel() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="scss">
.shipTransport {
  .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;
              }
            }
          }
        }
      }
    }
  }
  .liulan {
    width: 120px;
    margin-top: 30px;
    .el-button {
      width: 100px !important;
      margin: auto;
    }
    .btn {
      margin-top: 0px;
    }
    .btn1 {
      margin-top: 13px;
    }
    .btn2 {
      margin-top: 67px;
    }
  }
  .button_box {
    margin-left: 42.5%;
  }
}
</style>
