<template>
  <!-- 添加供应商信息 -->
  <div class="addSupplier">
    <PageTitle>返回</PageTitle>
     <div class="form-box" style="margin-right: 10rem">
        <dil-form :formId="320" v-model="form1" ref="from1"></dil-form>
     </div>
     <div class="f-box">
        <el-form
        :inline="true"
        class="demo-form-inline"
        label-width="80px"
        >
        <el-form-item label="父级单位">
         <el-autocomplete
          class="inline-input"
          v-model="stateSupplier"
          :fetch-suggestions="querySearchSupplier"
          placeholder="请输入父级单位名称(非必填)"
          :trigger-on-focus="false"
          @select="handleSelectSupplier"
        >
        <template slot-scope="{ item }">
          <div class="name">{{ item.supplierName }}</div>
        </template>
        </el-autocomplete>
        </el-form-item>
     </el-form>  
     </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 {
      form1: {},
      value: undefined,
       //发货单位名称
      supplierName: "",
      //发货单位id
      supplierId: null,
      restaurantsSupplier:null,
      stateSupplier:null,
    };
  },
  mounted() {},
  methods: {
    //发货单位弹出层
    handleSelectSupplier(item){
      this.supplierId = item.supplierId
      item.supplierName = this.supplierName
    },
    //以下是发货单位边输边查搜索
    querySearchSupplier(queryString, cb) {
        this.axios.post('/api/v1/uc/getSupplierMesByLike?index='+this.stateSupplier).then((res)=>{
        if(res.data.code == "200"){    
          var restaurantsSupplier = res.data.data
          var results = queryString ? restaurantsSupplier.filter(this.createFilterSupplier(queryString)) :restaurantsSupplier;
          // 调用 callback 返回建议列表的数据
          cb(results);
        }
      })  
      },
    createFilterSupplier(queryString) {
        return (restaurantsSupplier) => {
          return (restaurantsSupplier.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1);
        };
      },
      //以上是发货单位边输边查搜索
     makeSure() {
       console.log(this.form1)
      let RmsSupplier = {
        supplierName: this.form1.supplierName,
        supplierAddress: this.form1.supplierAddress,
        supplierRegisterDate: this.form1.supplierRegisterDate,
        supplierContactsName: this.form1.supplierContactsName,
        supplierContactNumber: this.form1.supplierContactNumber,
        supplierId:this.supplierId
      };
      console.log(RmsSupplier);
      if(
        RmsSupplier.supplierName==null
      )this.$message.error("存在空值！");
      else
      this.axios
        .post(
          "/api/v1/rms/insertSupplier",
           RmsSupplier
        )
        .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' >
 .addSupplier{
    .button-box{
    display: flex;
    justify-content: center;
    .el-button{
      width: 80px;
      margin-right: 10px;
    }
  }
  .f-box{
    margin-left: 38%;
  }
.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>
