<template>
  <!-- 添加托运人信息 -->
  <div class="addWagonLoad">
    <PageTitle>返回</PageTitle>
     <div class="form-box" style="margin-right: 10rem">
       <!-- 323 -->
        <dil-form :formId="323" v-model="form1" ref="from1"></dil-form>
      </div>
      <div class="department2">
         <span>二级部门：</span>
        <el-select v-model="value1" placeholder="请选择" @change="onchange1">
          <el-option
            v-for="(item,i) in options1"
            :key="i"
            :label="item.label"
            :value="item.value"
            >
            </el-option>
          </el-select>       
      </div>
      <div class="department2">
        <span>三级部门：</span>
        <el-input v-model="input" placeholder="请输入内容"></el-input>
      </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,
      shipperName:"四川达钢",
      shipperAbbreviation:"四川达钢",
      //二级部门
      options1:[],
      //选中的二级部门名称
      value1:'',
      input:''
    };
  },
  mounted() {
    this.form1.shipperName = this.shipperName;
    this.form1.shipperAbbreviation = this.shipperAbbreviation;
    //初始化二级机构和角色
    this.initialization();
  },
  methods: {
    initialization(){
       //初始化选择二级机构
         this.axios.get('/api/v1/rms/getSecondShipper').then((res)=>{
             this.options1 = res.data.data;
         })
         //初始化角色
        const formData = new FormData();
        this.$store.dispatch('system/rolesManage/list',formData)
        .then((res)=>{
          var roleList = [];
          roleList=res.data;
          roleList.forEach((item,i)=>{
              if(item.roleId!='superadmin'){
                this.options3.push(item);
              }
          })
          // this.options3 = res.data;
        })
     },
    onchange1(val){
            //  查询选中的二级机构下的三级机构
             this.value2 = '';
             this.axios.get('/api/v1/rms/getThirdShipper?shipperId='+val).then((res)=>{
             this.options2 = res.data.data;    
         })
         //获取二级部门的机构ID和机构编码
         this.axios.post('/api/v1/rms/getShipperMap?shipperId='+val).then((res)=>{
             this.map1 = res.data.data;
         })
     },
     makeSure() {
       console.log(this.form1)
       let RmsShipper={
        shipperName: this.form1.shipperName,
        shipperAbbreviation: this.form1.shipperAbbreviation,
        shipperId:this.form1.shipperId,
        shipperCompanyBranch:this.value1,
        shipperBranchFactory:this.input
      };
      if(
        RmsShipper.shipperName==null ||
        RmsShipper.shipperAbbreviation ==null ||
        RmsShipper.shipperCompanyBranch ==null ||
        RmsShipper.shipperBranchFactory ==null
      )this.$message.error("存在空值！");
      else
      this.axios
        .post(
          "/api/v1/rms/insertShipper",
           RmsShipper

        )
        .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;
    justify-content: center;
    .el-button{
      width: 80px;
      margin-right: 10px;
    }
  }
.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;
        }
      }
    }
  }
}
.department2{
  display: flex;
  justify-content: center;
  margin-left: 36%;
  width: 250px;
}
</style>
