<template>
  <!-- 添加司机信息 -->
  <div class="addWagonLoad">
    <PageTitle>返回</PageTitle>
    <el-divider content-position="left">司机信息</el-divider>
    <div class="form_box" style="margin-right: 10rem">
      <dil-form :formId="311" v-model="form1" ref="from1"></dil-form>
    </div>
    <div class="inputBox">
    <span class="text">所属承运商</span>
      <el-autocomplete
        v-model="state"
        @input="onInput"
        :fetch-suggestions="querySearch"
        placeholder="边输入边查询"
        :trigger-on-focus="false"
        @select="handleSelect"
      ></el-autocomplete>
      </div>
    
    <div class="button_box">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="makeSure">确定</el-button>
    </div>
  </div>
  
    
</template>

<script>
import PageTitle from "@/components/Page/Title";

export default {
  components: { PageTitle },
  data() {
    return {
      restaurants: [],
      form1: {},
      value: undefined,
      fileList: [],
      carrierIds:"",
      state: "",
    };
  },
  mounted() {
     this.form1.driverSex = this.sex;	
},
  methods: {
    onInput() {
      this.axios.post(
        "/api/v1/rms/getCarrierName?state="+this.state,
      )
      .then((res) => {
        if(res.data.code == "200"){
          res.data.data.forEach(element => {
            this.restaurants.push({
              value:element.carrierName,
              carrierIds:element.carrierId
            })
          });
        }
        
      });
    },
    querySearch(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
    handleSelect(item){
      this.carrierIds=item.carrierIds;
      console.log(item)
    },
    makeSure() {
      console.log(this.form1);
      let reg = new RegExp(/^[0-9]*$/);
      let RmsCarDriver = {
        driverName: this.form1.driverName,
        driverSex: this.form1.driverSex,
        driverAge: this.form1.driverAge,
        driverIdentityCard: this.form1.driverIdentityCard,
        driverPhone: this.form1.driverPhone,
        transportTypeId: this.form1.transportTypeId,
        state:this.state,
        carrierName:this.state,
        carrierIds: this.carrierIds,

      };
    if (
        reg.test(RmsCarDriver.driverAge) == false ||
        reg.test(RmsCarDriver.driverPhone) == false
      )
        this.$message.error("输入的格式不正确");
      else  if(
        RmsCarDriver.driverName==null ||
        RmsCarDriver.driverSex==null ||
        RmsCarDriver.driverAge==null ||
        RmsCarDriver.driverIdentityCard==null ||
        RmsCarDriver.driverPhone==null ||
        RmsCarDriver.transportTypeId==null ||
        RmsCarDriver.carrierIds==null 
     //   RmsCarDriver.carrierId==null 
    )this.$message.error("存在空值！");
     else
      this.axios
        .post("/api/v1/rms/insertCarDriver", RmsCarDriver)
       .then((res) => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "新增成功!",
            });
            // this.$refs.table.refreshData();
            this.$router.go(-1);
          } else {
            this.$message.error("新增失败,可能存在重复！");
          }
          this.$refs['table'].resetField();
        });
    },
    // 取消
    cancel() {
      this.$router.go(-1);
    },
  },
};
</script>
<style lang='scss'>
.addWagonLoad {
  .form_box {
    width: 100%;
    margin-top: 30px;
    display: flex;
    justify-content: center;
    .el-form-item{
      display: flex;
      justify-content: center;
      .el-form-item__label{
        display: flex;
        align-items: center;
      }
      .el-form-item__content{
         .el-select{
            width: 250px;
          }
          .el-input{
            width: 250px;
          }
      }
    }
  }
  .photoBox{
   display: flex;
  text-align: center;
  align-items: center;
  justify-content: center;
  margin-top: 0.1000rem;
  margin-bottom: 0.25rem;
}
  .inputBox{
    display: flex;
    justify-content: center;
    margin-bottom: 30px;
    .text{
      text-align: right;
      display: flex;
      align-items: center;
      margin-right: 5px;
    }
    .input{
      width: 250px;
    }
  }
  .button_box{
    display: flex;
    justify-content: center;
    .el-button{
      width: 80px;
      margin-right: 10px;
    }
  }
}
</style>