<template>
  <!-- 修改供应商信息 -->
  <div id="contractDetails">
    <page-title>编辑</page-title>
    <div class="main">
    </div>
    <div class="contractTitle">
      <div class="form-box">
        <div class="form-one">
          <dil-form :formId="321" v-model="form1"></dil-form>
        </div>
      </div>
    </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="onClickCancel">返回</el-button>
      <el-button type="primary" @click="onClickConfirm">确认</el-button>
    </div>
  </div>
</template>

<script>
import PageTitle from "@/components/Page/Title";
export default {
  components: { PageTitle },
  data() {
    return {
      form1: {},
       //发货单位名称
      supplierName: "",
      //发货单位id
      supplierId: null,
      restaurantsSupplier:null,
      stateSupplier:null,
      farName: "",
    };
  },
 
  mounted() {
      console.log(this.$route)
    this.information();
  },
  methods: {
     //发货单位弹出层
    handleSelectSupplier(item){
      console.log("enter")
      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);
        };
      },
      //以上是发货单位边输边查搜索

    information() {
      console.log("supplierIds",this.$router.params);
      //编辑
      this.axios
        .post(
          "/api/v1/rms/getSupplierById/" +
            this.$route.params.supplierId
        )
        .then((res) => {
            this.form1 = res.data.data[0];
            this.stateSupplier = res.data.data[0].farName;
            this.supplierId = res.data.data[0].shipperId;
          
        });
    },
    // 返回
    onClickCancel() {
      this.$router.go(-1);
    },
    // 确认
    onClickConfirm() {
      console.log(this.form1)
       let RmsSupplier={
        supplierOfId: this.form1.supplierId,
        supplierName: this.form1.supplierName,
        supplierAddress: this.form1.supplierAddress,
        supplierRegisterDate: this.form1.supplierRegisterDate,
        supplierContactsName: this.form1.supplierContactsName,
        supplierContactNumber: this.form1.supplierContactNumber,
        supplierIds: this.supplierId
      };
      console.log("RmsSupplier",RmsSupplier)
       if(
        RmsSupplier.supplierName==null 
      )this.$message.error("存在空值！");
        this.axios
          .post(
            "/api/v1/rms/updateSupplier",
            RmsSupplier
          )
          .then((res) => {
            if (res.data.code == "200") {
              this.$router.go(-1);
            }
          });
    },
  },
};
</script>
<style lang='scss' scoped>
.form-box,
 .button-box{
    display: flex;
    justify-content: center;
    .el-button{
      width: 80px;
      margin-right: 10px;
    }
  }
  .f-box{
    margin-left: 36%;
  }
</style>