<template>
  <!-- 添加运力信息 -->
  <div class="addWagonLoad">
    <PageTitle>返回</PageTitle>

     <div class="form_box" style="margin-right: 10rem">
        <dil-form :formId="309" v-model="form1" ref="from1"></dil-form>
      </div>
    <div class="inputBox">
    <span class="text">所属承运商</span>
      <el-autocomplete
        class="input"
        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";
import { getCookie } from "@/utils/util.js";
export default {
  components: { PageTitle },
  data() {
    return {
      form1: {},
      value: undefined,
      carrierIds: "",
      state: "",
      userId:null,
      restaurants: [],
      accessToken:null,
      userCode:null,
      carrierUserId:null,
      carrierId:null
    };
  },
  created(){
      this.carrierUserId = getCookie("userId");
      console.log(this.carrierUserId ,'carrierUserId')
      
  },
  mounted() {
   this.axios.post(
       "api/v1/rms/getCarrierNameBySSOId?carrierSSOId=" + this.carrierUserId
      )
      .then((res) => {
        if(res.data.code == "200"){
        console.log(res.data.data)
        this.state = res.data.data.carrierName
        this.carrierIds = res.data.data.carrierId
        }      
  })},
  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);
    },
    deleteUser(userId){
        this.axios.delete("pass/v1/sysusers/" + userId) .then((res) => {
           this.$message.error("添加失败,车牌可能重复");
           });
    },
    insertRole(userId,userCode){
        this.axios
        .post(
          "pass/v1/sysuserroles/addUserroles?userId=" +
            userId +
            "&userCode=" +
            userCode +
            "&roleId=" +
            '923693668269953024'
        ).then((res) => {
          if (res.code === "0") {
            this.$message.success("操作成功");
            this.saveLoading = false;
            this.rolesTree.loading = false;
          } else {
            this.$message.error(res.message);
          }
        })
    },
    makeSure() {
        let RmsCapacity = {
        capacityTypeId: this.form1.capacityTypeId,
        capacityNumber: this.form1.capacityNumber.toUpperCase(),
        capacityCorlor: this.form1.capacityCorlor,
        capacityOwneris: this.form1.capacityOwneris,
        capacityVip: this.form1.capacityVip,
        capacityBlacklist: this.form1.capacityBlacklist,
        carrierId:this.carrierIds,
        state: this.state
      };
      if (
        RmsCapacity.capacityNumber == null ||
        RmsCapacity.capacityCorlor == null ||
        RmsCapacity.capacityOwneris == null ||
        RmsCapacity.capacityVip == null ||
        RmsCapacity.capacityBlacklist == null
      )
        this.$message.error("存在空值！");
        var formData = new FormData();
        formData.append("userName",this.form1.capacityNumber.toUpperCase())
        console.log(formData.get("userName"))
        console.log(formData.set("userName",this.form1.capacityNumber.toUpperCase()))
        console.log(formData)
        formData.append('userCode',this.form1.capacityNumber.toUpperCase())
        formData.append('orgCode','yunli')
        formData.append('orgName','运力')
        formData.append('groupId','506514577756917769')
        formData.append('companyId','713710108567277568')
        formData.append('orgId','924126716337721344')
        console.log(formData.get("userCode"))
        console.log(typeof formData)
        this.$store.dispatch("system/usersManage/addUser", formData).then((res) => {
        console.log(res);
        if (res.code === "0") {
          console.log(res.data.userId)
          this.userId = res.data.userId
          this.userCode = res.data.userCode
          RmsCapacity.ssoId = res.data.userId
          console.log(RmsCapacity)
          this.axios.post("/api/v1/rms/insertCapacity",RmsCapacity).then((res) => {
              console.log(res)
              if(res.data.code == '200'){
                this.insertRole(this.userId,this.userCode)
                this.$message.success("添加成功");
                this.$router.push('capacity')
              }
              else if(res.data.code == '201'){
                this.deleteUser(this.userId)
              }else{
                this.deleteUser(this.userId)
              }
          })         
        } else {
          this.$message.error(res.message);
        }
      });
    },
    // 取消
    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;
          }
      }
    }
  }
  .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>
