<template>
  <!-- 添加车皮装车实绩 -->
  <div class="addWagonLoad">
    <page-title>新增</page-title>
    <el-divider content-position="left">车皮装车实绩</el-divider>
    <div class="cp">
      <span>车皮号：</span>
      <el-input
        placeholder="请输入内容"
        v-model="input"
        @change="onchang"
        clearable
      >
      </el-input>
    </div>
    <div class="cpList">
      <div class="list">
        <span class="cpList1" v-for="(item, i) in list" :key="i">
          {{ item }}
          <i class="el-icon-close itu" @click="onclick(i)"></i>
        </span>
      </div>
    </div>
    <div class="matching">
      <span class="text">是否知道采购订单号:</span>
      <el-switch
        v-model="matching"
        active-color="#27d452"
        inactive-color="#e1e3e6"
      >
      </el-switch>
      <span class="span"></span>
    </div>
    <div class="material from">
      <span class="text">物资：</span>
      <el-input v-model="materialName" disabled> </el-input>
      <el-button type="primary" @click="ondrawer(1)">浏览</el-button>
    </div>

    <div class="forwardingUnit from">
      <span class="text">发货单位：</span>
      <el-input v-model="supplierName" disabled> </el-input>
      <el-button type="primary" @click="ondrawer(2)">浏览</el-button>
    </div>
    <div class="remark from" v-if="matching">
      <span class="text">船名/备注：</span>
      <el-input
        v-model="remark"
        @blur="onBlur"
        placeholder="可输入船名\备注重新匹配采购订单"
      >
      </el-input>
      <span class="span"></span>
    </div>
    <div class="purchaseOrder from" v-if="matching">
      <span class="text">采购订单号：</span>
      <el-input
        v-model="purchaseOrderNo"
        placeholder="请选择物资和发货单位"
        disabled
      >
      </el-input>
      <span class="span"></span>
    </div>

    <div class="form">
      <div class="form-box" style="margin-right: 10rem">
        <dil-form :formId="364" v-model="form1" ref="from1"></dil-form>
      </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" size="30%">
      <el-input
        placeholder="请输入内容"
        v-model="inputText"
        style="margin-top: 0rem; margin-right: 0.1rem; width: 230px"
        clearable
      ></el-input>
      <el-button
        type="primary"
        class="btn"
        @click="onClick(a)"
        style="margin-bottom: 0.9375rem"
      >
        <i class="el-icon-search"></i>查询
      </el-button>
      <div v-show="a == 1">
        <dilTable
          v-bind.sync="frist"
          @radio-change="currentRadioChange1"
        ></dilTable>
      </div>
      <div v-show="a == 2">
        <dilTable
          v-bind.sync="secend"
          @radio-change="currentRadioChange2"
        ></dilTable>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import PageTitle from "@/components/Page/Title";

export default {
  components: { PageTitle },
  data() {
    return {
      list: [],
      form1: {},
      input: "",
      //是否匹配采购订单号
      matching: false,
      value: undefined,
      //船名
      remark: null,
      //采购订单id
      purchaseOrderId: null,
      //采购订单号
      purchaseOrderNo: null,
      //物资名称
      materialName: null,
      //发货单位名称
      supplierName: null,
      //发货单位id
      supplierId: null,
      //物资id
      materialId: null,
      //是否需要打开模态窗口
      drawer: false,
      a: 1,
      direction: "rtl",
      inputText: "",
      frist: {
        requestUrl: "/api/v1/uc/queryAPOMaterialByLike?apiId=244&startNum=050",
        selectionType: "radio",
        mapList1: [],
      },
      secend: {
        requestUrl:
          "/api/v1/uc/getSupplierMesByMaterialId?apiId=247&materialId=",
        selectionType: "radio",
        mapList2: [],
      },
      unloadPoint: {
        requestUrl: "/api/v1/uc/getUnloadingMesByLike?apiId=374",
        selectionType: "radio",
      },
    };
  },
  watch: {
    matching(val) {
      if (val) {
        this.getPurchaseOrderNo();
      }
    },
  },
  created() {
    this.form1 = {
      arrivalStationId: 2,
    };
  },
  methods: {
    currentRadioChange1(selection) {
      this.input = null;
      this.materialName = selection.materialName;
      this.materialId = selection.materialId;
      if (this.matching) {
        this.getPurchaseOrderNo();
      }
    },
    currentRadioChange2(selection) {
      this.supplierName = selection.supplierName;
      this.supplierId = selection.supplierId;
      if (this.matching) {
        this.getPurchaseOrderNo();
      }
    },
    ondrawer(num) {
      this.drawer = true;
      this.a = num;
      if (num == 2) {
        this.secend.requestUrl =
          "/api/v1/uc/getSupplierMesByMaterialId?apiId=247&materialId=" +
          this.materialId;
      }
    },
    onBlur() {
      if (this.matching) {
        this.getPurchaseOrderNo();
      }
    },
    getPurchaseOrderNo() {
      console.log(this.matching);
      let map = {
        materialId: this.materialId,
        supplierId: this.supplierId,
      };
      if (this.remark) {
        map.remark = this.remark;
      }
      console.log(this.map);
      if (this.materialId && this.supplierId) {
        this.axios.post("/api/v1/oms/selectNewOrderId", map).then((res) => {
          console.log(res);
          if (res.data.code == "200") {
            if (res.data.data == null) {
              this.$message({
                message: "没有匹配的采购订单号!",
                type: "warning",
              });
              this.form1 = [];
            } else {
              this.remark = res.data.data.remark;
              this.purchaseOrderId = res.data.data.purchaseOrderId;
              this.purchaseOrderNo = res.data.data.purchaseOrderNo;
            }
          } else {
            this.$message.error("请求失败");
          }
        });
      }
    },
    onClick(a) {
      if (a == 1) {
        this.frist.requestUrl =
          "/api/v1/uc/queryAPOMaterialByLike?apiId=244&startNum=050&index=" +
          this.inputText;
      } else if (a == 2) {
        this.secend.requestUrl =
          "/api/v1/uc/getSupplierMesByMaterialId?apiId=247&materialId=" +
          this.materialId +
          "&index=" +
          this.inputText;
      } else if (a == 4) {
        this.unloadPoint.requestUrl =
          "/api/v1/uc/getUnloadingMesByLike?apiId=374&index=" + this.inputText;
      }
    },
    onchang() {
      var state = false;
      var cptop = this.input.substring(0, 2);
      var cptop1 = this.input.substring(0, 3);
      if (
        cptop == "45" ||
        cptop == "46" ||
        cptop == "42" ||
        cptop == "47" ||
        cptop == "15" ||
        cptop == "16" ||
        cptop == "17" ||
        cptop == "18"
      ) {
        state = true;
      } else if (
        cptop1 == "140" ||
        cptop1 == "141" ||
        cptop1 == "142" ||
        cptop1 == "143" ||
        cptop1 == "145"
      ) {
        state = true;
      } else {
        this.$message({
          type: "warning",
          message: "车皮号格式不对，请重新输入！",
        });
      }
      if (state) {
        if (this.input.length == 7) {
          this.list.push(this.input);
          this.input = "";
        } else if (this.input.length < 7) {
          this.$message({
            type: "warning",
            message: "车皮号长度不足7位数，请重新输入！",
          });
        } else if (this.input.length > 7) {
          this.$message({
            type: "warning",
            message: "车皮号长度超过7位数，请重新输入！",
          });
        }
      }
    },
    onclick(index) {
      this.list.splice(index, 1);
    },
    // 确定
    makeSure() {
      let state = 0;
      if (
        this.list.length > 0 &&
        this.form1.sendStationId &&
        this.form1.arrivalStationId &&
        this.form1.resultLoadingDate
      ) {
        state = 1;
      } else {
        if (this.list.length == 0) {
          this.$message({
            type: "warning",
            message: "请输入车皮号！",
          });
        } else {
          if (this.form1.sendStationId) {
            if (this.form1.arrivalStationId) {
              if (this.form1.resultLoadingDate) {
                if (this.supplierId) {
                  if (this.materialId) {
                  } else {
                    this.$message({
                      type: "warning",
                      message: "请选择物资",
                    });
                  }
                } else {
                  this.$message({
                    type: "warning",
                    message: "请选择发货单位",
                  });
                }
              } else {
                this.$message({
                  type: "warning",
                  message: "请选择装车时间！",
                });
              }
            } else {
              this.$message({
                type: "warning",
                message: "请选择到站！",
              });
            }
          } else {
            this.$message({
              type: "warning",
              message: "请选择发站！",
            });
          }
        }
      }
      if (state == 1) {
        let map = {
          wagonNoList: this.list,
          sendStationId: this.form1.sendStationId,
          arrivalStationId: this.form1.arrivalStationId,
          resultLoadingDate: sjTime(this.form1.resultLoadingDate),
          resultRemarks: this.form1.resultRemarks,
          resultType: 3,
          sendUnitId: this.supplierId,
          materialId: this.materialId,
        };
        if (this.matching) {
          map = {
            wagonNoList: this.list,
            sendStationId: this.form1.sendStationId,
            arrivalStationId: this.form1.arrivalStationId,
            resultLoadingDate: sjTime(this.form1.resultLoadingDate),
            resultRemarks: this.form1.resultRemarks,
            resultType: 3,
            sendUnitId: this.supplierId,
            materialId: this.materialId,
            purchaseOrderRailPlanId: this.purchaseOrderId,
          };
        }
        this.axios
          .post("/api/v1/tms/addDomesticLoadResult", map)
          .then((res) => {
            console.log(res);
            if (res.data.code == "200") {
              this.$message({
                type: "success",
                message: "新增成功",
              });
              this.$router.push("/wagonLoad");
            }
          });
      }
    },
    cancel() {
      this.$router.push("/wagonLoad");
    },
  },
};
</script>
<style lang='scss'>
.addWagonLoad {
  color: #606266;
  font-size: 0.675rem;
  font-weight: 500;
  .cp {
    width: 49%;
    display: flex;
    justify-content: center;
    align-items: center;
    padding-top: 0.625rem;
    padding-bottom: 0.625rem;
    margin-left: 364px;
    .cp_title {
      .cp_top {
        width: 3.125rem;
        padding-right: 1.55rem;
      }
    }
  }
  .cpList {
    display: flex;
    justify-content: center;
    width: 100%;
    height: 10rem;
    .list {
      width: 45%;
      border: #000 0.0625rem solid;
      padding: 0.625rem;
      display: flex;
      flex-wrap: wrap;
      .cpList1 {
        background-color: #c9c7c7;
        height: 1.875rem;
        margin-right: 0.5rem;
        padding: 0.625rem;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        .itu {
          position: absolute;
          top: -0.125rem;
          right: 0.0625rem;
          width: 0.625rem;
          height: 0.625rem;
        }
      }
    }
  }
  .matching {
    width: 100%;
    height: 3.125rem;
    display: flex;
    justify-content: center;
    align-items: center;
    .text {
      width: 8.75rem;
    }
    .span {
      display: inline-block;
      width: 310px;
      height: 0.0625rem;
    }
  }
  .material {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 3.75rem;
    margin-left: 27rem;
    width: 20rem;
    .text {
      text-align: right;
      width: 8.125rem;
    }
  }
  .forwardingUnit {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 3.75rem;
    margin-left: 27rem;
    width: 20rem;
    .text {
      text-align: right;
      width: 8.125rem;
    }
  }
  .remark {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 3.75rem;
    .text {
      text-align: right;
      width: 5.625rem;
    }
    .span {
      display: inline-block;
      width: 1.875rem;
      height: 1.25rem;
    }
  }
  .purchaseOrder {
    display: flex;
    justify-content: center;
    align-items: center;
    .span {
      display: inline-block;
      width: 1.875rem;
      height: 0.0625rem;
    }
  }
  .form {
    margin-top: 1.25rem;
    display: flex;
    justify-content: center;
    padding-left: 9.375rem;
  }
  .button-box {
    display: flex;
    justify-content: center;
    padding-top: 30px;
  }
}
</style>

