<template>
  <!-- 添加提货委托 -->
  <div class="shipTransport">
    <page-title>返回</page-title>
    <div class="form">
      <div class="form_box">
        <dil-form :formId="113" v-model="form1" ref="from1"></dil-form>
        <el-form>
        <div class="preview-group">
            <el-form-item label="承运单位:">
                <el-select
                v-model="carrierId"
                filterable >
                <el-option
                  v-for="carrier in carriers"
                  :key="carrier.id"
                  :label="carrier.label"
                  :value="carrier.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="提货单位:">
                <el-select
                v-model="downSwimPortId"
                filterable >
                <el-option
                  v-for="carrier in carriers"
                  :key="carrier.id"
                  :label="carrier.label"
                  :value="carrier.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="到达港:">
                <el-select
                v-model="portId"
                filterable >
                <el-option
                  v-for="port in ports"
                  :key="port.portId"
                  :label="port.portName"
                  :value="port.portId">
                </el-option>
              </el-select>
            </el-form-item>
        </div>
        </el-form>
      </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" :loading="isLoading">确定</el-button>
    </div>
    <!-- 模态框 -->
    <el-drawer
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
      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()"
        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";
import { getCookie } from "@/utils/util.js";
export default {
  components: { PageTitle },
  data() {
    return {
      isLoading:false,
      input: "",
      carrierId:null,//承运单位id
      carriers:[],
      downSwimPortId:null,//提货单位id
      downSwimPorts:[],
      portId:null,//到达港id
      ports:[],
      drawer: false,
      direction: "rtl",
      options: {
        requestUrl: "/api/v1/tms/getBatchListForAttorney?apiId=383",
        selectionType: "radio",
        maplist: [],
      },
      batchIds: "",
      form1: {},
      value: undefined,
      value2: undefined,
    };
  },
  computed: {
    a1() {
      return this.form1.attorneyPickupContactPerson;
    },
  },
  watch: {
    a1() {
      this.getPersonInfo();
    },
  },
  created() {
    this.form1 = {
      downSwimPortId: 11,
      attorneyTime: new Date(),
    };
  },
  mounted() {
    this.$set(this.form1, "requesterGroupId", 1); //委托单位（requesterGroupId）承运单位（carrierId）转移到提货委托
    this.$set(this.form1, "carrierId", 11);
    this.getCarriers();
    this.getPorts();
  },
  methods: {
    //查询所有承运单位和提货单位
    getCarriers(){
      this.axios.get("/api/v1/tms/getPortName").then((res) => {
          if (res.data.code == 200) {
            //赋值
            this.carriers=res.data.data;
            this.downSwimPorts=res.data.data;
          } else {
            this.$message({
              type: "warning",
              message: res.data.data,
            });
          }
        });
    },
    //查询所有港口
    getPorts(){
      this.axios.post("/api/v1/rms/getPortName?index=").then((res) => {
          if (res.data.code == 200) {
            //赋值
            this.ports=res.data.data;
          } else {
            this.$message({
              type: "warning",
              message: res.data.data,
            });
          }
        });
    },
    getPersonInfo() {
      console.log(this.form1);
      console.log("我尽力了");
      this.axios
        .post(
          "/api/v1/tms/getPersonByName?personName=" +
            this.form1.attorneyPickupContactPerson
        )
        .then((res) => {
          console.log(this.form1);
          this.$set(this.form1, "attorenyPickupIdcard", res.data.data.personId);
          this.$set(
            this.form1,
            "attorneyContactTelephone",
            res.data.data.personPhone
          );
          console.log(this.form1);
        });
    },
    onDrawer() {
      this.drawer = true;
    },
    onclick() {
      this.options.requestUrl =
        "/api/v1/tms/getBatchListForAttorney?apiId=375&con=" + this.input;
    },
    handleClose(done) {
      done();
    },
    currentRadioChange1(selection) {
      this.maplist = selection;
      if (JSON.stringify(this.maplist).length > 2) {
        this.$set(
          this.form1,
          "batchId",
          selection.resultForeignShipName + selection.materialName
        );
        this.batchIds = selection.batchId;
      }
    },
    // 确定
    makeSure() {
      this.isLoading=true;
      // 判断用户是否输入了批次号
      if (this.batchIds == "" || this.batchIds == undefined) {
        this.$message.warning("请选择批次号");
        this.isLoading=false;
        return;
      }
      let AmsshipDeliveryAttorney = {
        carrierId: this.carrierId, //委托单位（requesterGroupId）承运单位（carrierId）转移到提货委托
        requesterGroupId: this.form1.requesterGroupId,
        downSwimPortId: this.downSwimPortId,
        attorenyPickupIdcard: this.form1.attorenyPickupIdcard,
        attorneyContactTelephone: this.form1.attorneyContactTelephone,
        realForeignShipName:this.form1.realForeignShipName,
        batchId: this.batchIds,
        attorneyTime: sjTime(this.form1.attorneyTime),
        attorneyPdffile: this.form1.attorneyPdffile,
        noticeId: this.form1.noticeId,
        portId: this.portId,
        attorneyPickupContactPerson: this.form1.attorneyPickupContactPerson,
        userId: getCookie("userId"),
        // resultMemo: this.form1.resultMemo,
      };
      console.log(AmsshipDeliveryAttorney);
      //身份证校验
      function checkCardNo() {
        var value3 = AmsshipDeliveryAttorney.attorenyPickupIdcard;
        //验证是否为数字
        var patrn =
          /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
        if (patrn.exec(value3) == null || value3 == "") {
          return false;
        } else {
          return true;
        }
      }

      //判断是否为电话号码
      function isTelePhone() {
        var value2 = AmsshipDeliveryAttorney.attorneyContactTelephone;
        //验证是否为数字
        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 val3 = this.value3;

      if (
        typeof AmsshipDeliveryAttorney.portId =='undefined' || AmsshipDeliveryAttorney.portId==null||
        typeof AmsshipDeliveryAttorney.downSwimPortId =='undefined' ||  AmsshipDeliveryAttorney.downSwimPortId == null || 
        typeof AmsshipDeliveryAttorney.requesterGroupId =='undefined' ||  AmsshipDeliveryAttorney.requesterGroupId == null ||
        AmsshipDeliveryAttorney.attorenyPickupIdcard == null ||
        AmsshipDeliveryAttorney.attorneyContactTelephone == null ||
        AmsshipDeliveryAttorney.attorneyPickupContactPerson == null ||
        AmsshipDeliveryAttorney.realForeignShipName == null
      )
        this.$message.error("存在空值,请核实以后再提交");
      else if (!isTelePhone(val2)) 
        this.$message.warning("联系电话格式不正确");
      else if (!checkCardNo(val3))
        this.$message.warning("请输入正确的身份证格式");
      else{
        this.axios
          .post(
            "/api/v1/tms/insertshipDeliveryAttorney",
            AmsshipDeliveryAttorney
          )
          .then((res) => {
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "新增成功",
              });
              this.cancel();
            } else {
              this.$message({
                type: "warning",
                message: res.data.data,
              });
            }
             this.isLoading=false;
            
          });
      }
      this.isLoading=false;
    },
    // 取消
    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: 45%;
  }
}
</style>
