<template>
  <div class="wagonLoadbu">
    <el-divider content-position="left">选择需要补录的装车信息</el-divider>
    <div class="wagonLoadbu_table">
      <dilTable
        v-bind.sync="option"
        @selection-change="selectionChange"
      ></dilTable>
    </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">
      <span class="text">船名(备注)：</span>
      <el-input v-model="remark" @blur="onBlur"> </el-input>
      <span class="span"></span>
    </div>

    <div class="form">
      <dil-form :formId="361" v-model="form1" ref="from1"></dil-form>
    </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: 10px; margin-left: 20px"
        clearable
      ></el-input>
      <el-button
        type="primary"
        class="btn"
        @click="onClick(a)"
        style="margin-bottom: 15px"
      >
        <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>
export default {
  data() {
    return {
      form1: {},
      option: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/tms/getTmstrainWagonLoad?apiId=209&resultType=10",
        // 控制显示多选列
        selectionType: "select",
      },
      //多选选中的数据
      selecTionListMap: [],
      //船名
      remark: null,
      //采购订单id
      purchaseOrderId: 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",
      },
    };
  },
  methods: {
    //确定
    makeSure() {
      if(this.selecTionListMap.length <= 0){
        this.$message({
          message: "请选择需要补录的装车信息!",
          type: "warning",
        });
      }else{
        let map = {
          resultIdList:this.selecTionListMap,
          purchaseOrderId:this.purchaseOrderId,
          insertUpdateRemark: this.form1.insertUpdateRemark
        }
        this.axios.post('/api/v1/tms/updateDomesticLoadResult',map).then((res)=>{
          console.log(res.data.data)
          if(res.data.code == "200"){
            this.$message({
              message: "补录成功!",
              type: "success",
            });
            this.cancel();
          }
        })
      }
      
    },
    //取消
    cancel() {
      this.$router.push("/wagonLoad");
    },
    //表格选中
    selectionChange(selection) {
      this.selecTionListMap = [];
      selection.forEach(element => {
          this.selecTionListMap.push(element.resultId)
      });
      console.log( this.selecTionListMap)
    },
    currentRadioChange1(selection) {
      this.input = null;
      this.materialName = selection.materialName;
      this.materialId = selection.materialId;
      this.getPurchaseOrderNo();
    },
    currentRadioChange2(selection) {
      this.supplierName = selection.supplierName;
      this.supplierId = selection.supplierId;
      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() {
      this.getPurchaseOrderNo();
    },
    getPurchaseOrderNo() {
      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.form1 = { 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.third.requestUrl =
          "/api/v1/uc/getUnloadingMesByLike?apiId=347&index=" + this.inputText;
      }
    },
  },
};
</script>

<style lang="scss">
.wagonLoadbu {
  .material {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 3.75rem;
    .text {
      text-align: right;
      width: 150px;
    }
  }
  .forwardingUnit {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 3.75rem;
    .text {
      text-align: right;
      width: 9.375rem;
    }
  }
  .remark {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 3.75rem;
    .text {
      text-align: right;
      width: 80px;
    }
  }
  .form {
    margin-top: 1.25rem;
    display: flex;
    justify-content: center;
  }
  .button_box {
    width: 100%;
    height: 6.25rem;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>