<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="photoBox">
   <el-upload
      class="upload-demo"
      action="https://jsonplaceholder.typicode.com/posts/"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      multiple
      :limit="1"
      :on-exceed="handleExceed"
      :file-list="fileList"
    >
      <div>驾驶证
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">
      </div>
      </div>
    </el-upload> 
    </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: "",
      sex: "男",
    };
  },
  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)
    },
   

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    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,
   //     carrierId: this.form1.carrierId,
        driverPermitNo: this.form1.driverPermitNo,
        driverLicence: this.form1.driverLicence,
        driverLicence: this.form1.driverLicence,
        driverLicence: this.form1.driverLicence,
        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'>
.button-box{
  display: flex;
  text-align: center;
  align-items: center;
  justify-content: center;
  margin-top: 0.3125rem;
  margin-bottom: 1.25rem;
}
.inputBox{
     display: flex;
  text-align: center;
  align-items: center;
  justify-content: center;
  margin-top: 0.1000rem;
  margin-bottom: 0.25rem;
  margin-left: -12.5rem;
}
.photoBox{
   display: flex;
  text-align: center;
  align-items: center;
  justify-content: center;
  margin-top: 0.1000rem;
  margin-bottom: 0.25rem;
  margin-left: -18rem;
}
.form-box{
  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-input{
        .el-input__inner{
          width:250px;
        }
      }
    }
  }
  
}
</style>