<template>
  <!-- 修改入库实绩页面 -->
  <div class="shipTransport">
    <page-title>返回</page-title>
    <div class="form">
      <div class="form_box">
        <dil-form :formId="113" v-model="form1"></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>
    <!-- 确定和取消 -->
    <div class="button_box">
      <el-button @click="onClickCancel">返回</el-button>
      <el-button type="primary" @click="onClickConfirm">确认</el-button>
    </div>
  </div>
</template>

<script>
import PageTitle from "@/components/Page/Title";
import { sjTime } from '@/utils/sharedJsFile'
export default {
  components: { PageTitle },
  data() {
    return {
      form1: {},
      carrierId:"",//承运单位id
      carriers:[],
      downSwimPortId:"",//提货单位id
      downSwimPorts:[],
      portId:"",//到达港id
      ports:[],
      value2: undefined,
      value3: undefined,
      batchInfactoryId:null,
    };
  },
  mounted() {
    this.getCarriers();
    this.getPorts();
    this.information();
  },
  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,
            });
          }
        });
    },
    information() {
      //编辑请车作业
      this.axios
        .post(
          "/api/v1/tms/getDeliveryAttorneyId/" + this.$route.params.attorneyId
        )
        .then((res) => {
          res.data.data.forEach((e) => {
            this.batchInfactoryId=e.batchInfactoryId;
            this.form1 = e;
            this.carrierId=e.carrierId;
            this.downSwimPortId=e.downSwimPortId;
            this.portId = e.portId;
            console.log(this.form1.batchId);
          });
        });
    },
    // 返回
    onClickCancel() {
      this.$router.go(-1);
    },
    // 确认
    onClickConfirm() {
      let AmsshipDeliveryAttorney = {
        resultId: this.$route.params.attorneyId,
        portId: this.portId,
        batchId:this.batchInfactoryId,
        attorneyContactTelephone:this.form1.attorneyContactTelephone,
        realForeignShipName:this.form1.realForeignShipName,
        attorenyPickupIdcard: this.form1.attorenyPickupIdcard,
        attorneyPickupContactPerson: this.form1.attorneyPickupContactPerson,
        downSwimPortId: this.downSwimPortId,
        attorneyTime: sjTime(this.form1.attorneyTime),
        carrierId:this.carrierId,
        requesterGroupId:this.form1.requesterGroupId
      };
      //校验下游港口，不能为万州港
      try {
        let downSwim=this.carriers.find(e=>{
          return e.id==this.downSwimPortId
        })
        if(downSwim.label=='万州港'){
          this.$message.warning("提货单位不能是万州港!");
          return;
        }
      } catch (error) {
        
      }
      //身份证校验
      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 (
        AmsshipDeliveryAttorney.attorneyTime == 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/editDeliveryAttroney", AmsshipDeliveryAttorney)
          .then((res) => {
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "修改成功!",
              });
            }
            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;
              }
            }
          }
        }
      }
    }
  }
  .button_box {
    margin-left: 45%;
  }
}
</style>