<template>
  <div class="wagonLoadbu">
    <page-title>返回</page-title>
     <div class="purchaseOrder_table">
      <div class="search" style="display:flex">
         <el-input
        placeholder="发货单位"
        v-model="supplierName"
        style="margin-top: 10px; margin-left: 20px; width: 150px"
        clearable
      >
      </el-input>
      <el-input
        placeholder="物资名"
        v-model="materialName"
        style="margin-top: 10px; margin-left: 20px; width: 150px"
        clearable
      >
       </el-input>
       <el-input
        placeholder="卸货地点"
        v-model="resultForeignShipName"
        style="margin-top: 10px; margin-left: 20px; width: 150px"
        clearable
      >
       </el-input>
      <el-button
        type="primary"
        class="btn"
        @click="searchOrder()"
        style="margin: 10px;">
        <i class="el-icon-search"></i>查询
      </el-button>
      <!-- <el-button 
      type="primary"
      class="btn" 
      @click="makeSure" 
      style="margin: 10px;">确认配单
      </el-button> -->
      </div>
      <!-- 采购订单号 -->
      <dilTable
        v-bind.sync="purchaseOption"
        @radio-change="orderChange"
      ></dilTable>
      </div>
    <!-- <div class="material form">
      <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 form">
      <span class="text">发货单位：</span>
      <el-input v-model="supplierName" disabled> </el-input>
      <el-button type="primary" @click="ondrawer(2)">浏览</el-button>
    </div>
    <div class="forwardingUnit form">
      <span class="text">卸货点：</span>
      <el-input v-model="unloadPointName" disabled> </el-input>
      <el-button type="primary" @click="ondrawer(3)">浏览</el-button>
    </div>
    <div class="remark form">
      <span class="text" >船名(备注)：</span>
      <el-input v-model="remark" @blur="onBlur" > </el-input>
      <span class="span"></span>
    </div>
    <div class="form">
      <div class="form_box">  
      <dil-form :formId="361" v-model="form1" ref="from1"></dil-form>
      </div>
    </div> -->
    <!--待补录表格-->
    <!-- <div class="wagonLoadbu_table">
      <dilTable
        v-bind.sync="option"
        @selection-change="selectionChange"
        @row-click="selectionChange"
      ></dilTable>
    </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>
      <div v-show="a == 3">
        <dilTable
          v-bind.sync="unloadPoint"
          @radio-change="currentRadioChange3"
        ></dilTable>
      </div>
    </el-drawer> -->
  </div>
</template>

<script>
import PageTitle from "@/components/Page/Title";
import {getCookie} from "@/utils/util.js";
export default {
  components: { PageTitle },
  data() {
    return {
      form1: {},
      con:null,
      purchaseOption:{
         // 表格请求数据的地址
        requestUrl: "/api/v1/tms/findPurchaseOrderList?apiId=81",
        // 控制选择单列
        selectionType: "radio",
        pageSize:10,
      },
      option: {
        // 表格请求数据的地址
        requestUrl: "/api/v1/tms/getTmstrainWagonLoad?apiId=209&resultType=10",
        // 控制显示多选列
        selectionType: "select",
        pageSize:10,
      },
      //查询用数据
      search1:null,
      search2:null,
      //多选选中的数据
      selecTionListMap: [],
      //船名
      remark: null,
      //采购订单id
      purchaseOrderId: null,
      //物资名称
      materialName: '',
      //发货单位名称
      supplierName: '',
      resultForeignShipName:'',
      //发货单位id
      supplierId: null,
      //物资id
      materialId: null,
      //物资选项
      materialNames:[],
      //发货单位选项
      supplierNames:[],
      //卸货点
      unloadPointId:null,
      unloadPointName: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",
      },
    };
  },
  mounted(){
    // this.searchMaterialName('');
  },
  methods: {
    //确定
    makeSure() {
      if(this.selecTionListMap.length <= 0){
        this.$message({
          message: "请选择需要配单的装车信息!",
          type: "warning",
        });
      }else if(!this.purchaseOrderId){
         this.$message({
          message: "请选择采购订单!",
          type: "warning",
        });
      }
      else{
        let map = {
          resultIdList:this.selecTionListMap,
          purchaseOrderId:this.purchaseOrderId,
          userId:getCookie("userId")
        }
        console.log(map);
        let that=this;
        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",
            });
            that.tableChange();
            // this.cancel();//不退出
          }
        })
      }
      
    },
    //取消
    cancel() {
      this.$router.push("/wagonLoadMine");
    },
    //采购订单表格选中
    orderChange(selection){
      console.log(selection);
      this.purchaseOrderId=selection.purchaseOrderId;
      this.form1 = { purchaseOrderNo: selection.purchaseOrderNo };
      this.materialId=selection.materialId;
      this.materialName=selection.materialName;
      this.supplierId=selection.supplierId;
      this.supplierName=selection.supplierName;
      this.remark=selection.resultForeignShipName;
    },
    //表格选中
    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();
    },
    currentRadioChange3(selection) {
      this.unloadPointId = selection.warehouseId;
      this.unloadPointName = selection.warehouseName;
    },
    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("请求失败");
          }
        });
      }
    },
    searchOrder(){
      //查询订单
        this.purchaseOption.requestUrl=
        "/api/v1/tms/findPurchaseOrderList?apiId=81&supplierName="+this.supplierName
        +"&materialName="+this.materialName
        +"&resultForeignShipName="+this.resultForeignShipName
        +"&i="+new Date();
    },
    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 == 3) {
        this.third.requestUrl =
          "/api/v1/uc/getUnloadingMesByLike?apiId=347&index=" + this.inputText;
      }
    },
    searchMaterialName(index=''){
      //根据name查询物资名
      let that=this;
      that.axios.post("api/v1/uc/queryAPOMaterialByLike?apiId=244&startNum=050&index="+index).then((res)=>{
          console.log(res);
          if (res.data.code == "200" && res.data.data) {
            that.materialNames=res.data.data.list;
          } else {
            this.$message.error("请求失败");
          }
         
      });
    },
    searchSupplierName(index=''){
      //根据name查询发货单位
      let that=this;
      that.axios.post("/api/v1/uc/getSupplierMesByMaterialId?apiId=247&materialId="+that.materialId+"&index="+index).then((res)=>{
           console.log(res);
          if (res.data.code == "200" && res.data.data) {
            that.supplierNames=res.data.data.list;
          }else{
            this.$message.error("请求失败");
          }
      });
    },
    tableChange(){
      //刷新表格
      this.option.requestUrl="/api/v1/tms/getTmstrainWagonLoad?apiId=209&resultType=10&i="+new Date();
    }
  },
};
</script>

<style lang="scss">
.wagonLoadbu {
  color: #606266;
  font-size: 0.675rem;
  font-weight: 500;
  .top {
    display: flex;
    margin:0.375rem;
    .el-select{
      width: 15%;
      // height: 2%;
    }
    .text {
      text-align: left; 
      margin-top: 0.5rem;
      margin-left:0.375rem;
    }
  }
  .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 {
    display: flex;
    width: 340px;
    margin-left:23rem; 
    .form_box {
      .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: 250px;  
                  margin-top: 0.03rem;
                }
              }
            }
          }
        }
      }
    }
  }
  .button_box {
    display: flex;
    justify-content: center;
    margin: 30px;
  }
}
</style>