<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>
      </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">确定</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 {
      input: "",
      drawer: false,
      direction: "rtl",
      options: {
        requestUrl: "/api/v1/tms/getBatchListForAttorney?apiId=375",
        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);
  },
  methods: {
    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() {
      // 判断用户是否输入了批次号
      if (this.batchIds == "" || this.batchIds == undefined) {
        this.$message.warning("请选择批次号");
        return;
      }
      let AmsshipDeliveryAttorney = {
        carrierId: this.form1.carrierId, //委托单位（requesterGroupId）承运单位（carrierId）转移到提货委托
        requesterGroupId: this.form1.requesterGroupId,
        downSwimPortId: this.form1.downSwimPortId,
        attorenyPickupIdcard: this.form1.attorenyPickupIdcard,
        attorneyContactTelephone: this.form1.attorneyContactTelephone,
        batchId: this.batchIds,
        attorneyTime: sjTime(this.form1.attorneyTime),
        attorneyPdffile: this.form1.attorneyPdffile,
        noticeId: this.form1.noticeId,
        portId: this.form1.portId,
        attorneyPickupContactPerson: this.form1.attorneyPickupContactPerson,
        userId: getCookie("userId"),
        // resultMemo: this.form1.resultMemo,
      };
      //身份证校验
      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)$/;
        // var patrn2 = /^(\d{3,4}-)?\d{7,8}$/;
        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 (
        // AmsshipDeliveryAttorney.attorneyId==null||
        // AmsshipDeliveryAttorney.downSwimPortId == null ||
        AmsshipDeliveryAttorney.attorenyPickupIdcard == null ||
        AmsshipDeliveryAttorney.attorneyContactTelephone == null ||
        AmsshipDeliveryAttorney.attorneyPickupContactPerson == 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: "新增成功",
              });
            } else {
              this.$message({
                type: "warning",
                message: res.data.data,
              });
            }
              this.$router.go(-1);
            
          });
    },
    // 取消
    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>
