<template>
  <!-- 添加货权转移实绩 -->
  <div class="shipTransport">
    <page-title>返回</page-title>
    <!-- 物资 -->
    <div class="mofrom">
      <span class="motext">物资</span>
      <el-input class="moinput" v-model="materialName" disabled> </el-input>
      <el-button class="mobutton" type="primary" @click="ondrawer(1)">浏览</el-button>
    </div>
    <!-- 货权转移送达单位 -->
    <div class="mofrom">
      <span class="motext">货权转移送达单位</span>
      <el-input class="moinput" v-model="cargo" disabled> </el-input>
      <el-button class="mobutton" type="primary" @click="ondrawer(2)">浏览</el-button>
    </div>
    <!-- 货权转移送达单位 -->
    <div class="mofrom">
      <span class="motext">收货人</span>
      <el-input class="moinput" v-model="receiver" disabled> </el-input>
      <el-button class="mobutton" type="primary" @click="ondrawer(3)">浏览</el-button>
    </div>
    <div class="form">
      <div class="form_box">
          <dil-form :formId="109" 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="50%"
    >
      <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(a)"
        style="margin-bottom: 15px"
      >
        <i class="el-icon-search"></i>查询
      </el-button>
      <div v-show="a == 1">
        <dilTable
          v-bind.sync="second"
          @radio-change="currentRadioChange1"
        ></dilTable>
      </div>
      <div v-show="a == 2">
        <dilTable
          v-bind.sync="second1"
          @radio-change="currentRadioChange2"
        ></dilTable>
      </div>
      <div v-show="a == 3">
        <dilTable
          v-bind.sync="second2"
          @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 {
      materialName: "",
      cargo: "",
      receiver: "",
      a: 1,
      form1: {},
      value: undefined,
      drawer: false,
      direction: "rtl",
      input: "",
      second: {
        requestUrl: "/api/v1/uc/queryMaterialByLike?apiId=244",
        selectionType: "radio",
        mapList1: [],
      },
      second1: {
        requestUrl: "/api/v1/tms/selectShippeByName?apiId=215",
        selectionType: "radio",
        mapList2: [],
      },
      second2: {
        requestUrl: "/api/v1/tms/getGroupList?apiId=217",
        selectionType: "radio",
        mapList3: [],
      },
    };
  },
  
  mounted() {},
  methods: {
    currentRadioChange() {
      console.log(1);
    },
    ondrawer(num) {
      this.drawer = true;
      this.a = num;
      // 清空当前输入框中的数据
      this.input = "";
    },
    currentRadioChange1(selection) {
      (this.second.mapList1 = selection),
        (this.materialName = selection.materialName);
    },
    currentRadioChange2(selection) {
      (this.second1.mapList2 = selection), (this.cargo = selection.portName);
    },
    currentRadioChange3(selection) {
      (this.second2.mapList3 = selection), (this.receiver = selection.pierName);
    },

    // 确定git
    makeSure() {
      if(this.second.mapList1.length == 0){
        this.$message.warning("请选择物资")
        return
      }
      if(this.second1.mapList2.length == 0){
        this.$message.warning("请选择送达单位")
        return
      }
      if(this.second2.mapList3.length == 0){
        this.$message.warning("请选择收货人")
        return
      }
      let AmsshipCargoTransferResult = {
        resultContactPerson: this.form1.resultContactPerson,
        resultTelephoneFax: this.form1.resultTelephoneFax,
        resultFreightForwardingInfo: this.form1.resultFreightForwardingInfo,
        resultMemo: this.form1.resultMemo,
        resultNumberOfLoans: this.form1.resultNumberOfLoans,
        carrierId: this.second1.mapList2.portId,
        groupId: this.second2.mapList3.id,
        foreignShipName: this.form1.foreignShipName,
        materialId: this.second.mapList1.materialId,
        userId:getCookie("userId")
      };
       //判断放货数量是否为数字
       function isNumber() {
        var value = AmsshipCargoTransferResult.resultNumberOfLoans;
        //验证是否为数字
        var patrn = /^(-)?\d+(\.\d+)?$/;
        if (patrn.exec(value) == null || value == "") {
          return false;
        } else {
          return true;
        }
      }
      //判断是否为电话号码
       function isTelePhone() {
        var value2 = AmsshipCargoTransferResult.resultTelephoneFax;
        //验证是否为数字
        var patrn  = /^1[3-9]\d{9}$/;
        // var patrn2 = /^(\d{3,4}-)?\d{7,8}$/;
        if (patrn.exec(value2) == null || value2 == "") {
          return false;
        } else {
          return true;
        }
      }
      //电话号码赋值
      var val2=this.value2;
      var val = this.value;
      if (
        AmsshipCargoTransferResult.foreignShipName == null ||
        AmsshipCargoTransferResult.resultNumberOfLoans == null ||
        AmsshipCargoTransferResult.resultContactPerson == null ||
        AmsshipCargoTransferResult.resultTelephoneFax == null
      )
        this.$message.error("存在空值！");
          else if (!isNumber(val)) this.$message.warning("放货数量必须为数字");
            else if (!isTelePhone(val2)) this.$message.warning("电话号码或传真格式不正确");  
      else
        this.axios
          .post(
            "/api/v1/tms/insertamsshipCargoTransferResult",
            AmsshipCargoTransferResult
          )
          .then((res) => {
            if (res.data.code == 200) {
              this.$router.push("/ship/cargoResult");
              this.$message({
                type: "success",
                message: "新增成功",
              });
            }
            this.$router.go(-2);
          });
    },
    onclick(a) {
      console.log(a);
      if (a == 1) {
        this.second.requestUrl =
          "/api/v1/uc/queryMaterialByLike?apiId=244&index=" + this.input;
      } else if (a == 2) {
        this.second1.requestUrl =
          "/api/v1/tms/selectShippeByName?apiId=215&con=" + this.input;
      } else {
        this.second2.requestUrl =
          "/api/v1/tms/getGroupList?apiId=217&con=" + this.input;
      }
      
    },
    // 取消
    cancel() {
      this.$router.go(-1);
    },
  },
};
</script>
<style lang='scss'>
.shipTransport{
  .form{
    display: flex;
    .form_box{
      width: 320px;
      margin-left: 35%;
      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;
              }
            }
          }
        }
      }
    }
  }
  .mofrom {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 5px;
    margin-bottom: 20px;
    .motext{
      width: 70px;
    }
    .moinput{
      width: 250px;
    }
    .mobutton{
      margin-left: 5px;
    }
  }
  .button_box{
    margin-left: 45%;
  }
}
</style>
