<template>
  <!-- 添加收货客户信息 -->
  <div class="addConsignee">
    <PageTitle>返回</PageTitle>
    <div class="form-box" style="margin-right: 10rem">
      <dil-form :formId="367" 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="stateConsignee"
            :fetch-suggestions="querySearchConsignee"
            placeholder="请输入收货父级单位名称"
            :trigger-on-focus="false"
            @select="handleSelectConsignee"
          >
            <template slot-scope="{ item }">
              <div class="name">{{ item.consigneeCompanyName }}</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'
import { getCookie } from '@/utils/util.js'
export default {
  components: { PageTitle },
  data() {
    return {
      form1: {},
      stateConsignee: null,
      restaurantsConsignee: null
    }
  },
  mounted() {},
  methods: {
    //收货单位弹出层
    handleSelectConsignee(item) {
      console.log(this.consigneeId)
      this.consigneeId = item.consigneeId
      item.consigneeCompanyName = this.consigneeCompanyName
      console.log(this.consigneeId)
      console.log('这是选中的收货单位')
    },
    //以下是发货单位边输边查搜索
    querySearchConsignee(queryString, cb) {
      this.axios
        .post('/api/v1/uc/getConsigneeByLike?index=' + queryString)
        .then(res => {
          if (res.data.code == '200') {
            console.log(res)
            var restaurantsConsignee = res.data.data
            var results = queryString
              ? restaurantsConsignee.filter(
                  this.createFilterConsignee(queryString)
                )
              : restaurantsConsignee
            // 调用 callback 返回建议列表的数据
            console.log(results, 'results')
            cb(results)
          }
        })
    },
    createFilterConsignee(queryString) {
      return restaurantsConsignee => {
        return (
          restaurantsConsignee.value
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) > -1
        )
      }
    },
    //以上是收货单位边输边查搜索
    makeSure() {
      console.log(this.form1)
      let RmsConsignee = {
        companyName: this.form1.companyName,
        consigneeAbbreviation: this.form1.consigneeAbbreviation,
        consigneeRegisteredAddress: this.form1.consigneeRegisteredAddress,
        consigneeReceiveAddress: this.form1.consigneeReceiveAddress,
        consigneeRegistrationTime: this.form1.consigneeRegistrationTime,
        consigneeContactName: this.form1.consigneeContactName,
        consigneeContactTel: this.form1.consigneeContactTel,
        consigneeFarId: this.consigneeId,
        userName: getCookie('loginName')
      }
      console.log('RmsConsignee', RmsConsignee)

      if (RmsConsignee.companyName == null) this.$message.error('存在空值！')
      else
        this.axios
          .post('/api/v1/rms/insertConsignee', RmsConsignee)

          .then(res => {
            console.log('res.data.code', res.data.code)
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '新增成功!'
              })
              // this.$refs.table.refreshData();
              this.$router.go(-1)
            } else {
              this.$message.error('新增失败,可能有重复')
            }
          })
    },
    // 取消
    cancel() {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="scss">
.addConsignee {
  .f-box {
    margin-left: 36%;
  }
  .button-box {
    display: flex;
    justify-content: center;
    .el-button {
      width: 80px;
      margin-right: 10px;
    }
  }
  .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;
        }
      }
    }
  }
}
</style>
