<template>
 <div id="directlyLibrary">
    <div class="title">新增分库</div>
    <div class="main">
      <div class="main_top">
        <div>仓库名称:</div>
        <div>
          <el-input v-model="objdata.dname" @input="iptchange" placeholder="请输入内容"></el-input>
        </div>
      </div>
      <div class="main_bottom">
        <div>所属:</div>
        <div>山西分公司</div>
      </div>
      <div class="btns">
        <el-button @click="cancel">取消</el-button>
        <el-button v-if="this.$route.query.state != 'edit'" @click="saveinfo">保存</el-button>
        <el-button v-if="this.$route.query.state == 'edit'" @click="undateinfo">保存</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import {
  searchname,
  savezs,
  findbyidzs,
  updatezs
} from "@/api/organizationalStructureManagement/index.js"
export default {
  name: "AddDirectlyLibrary",
  data() {
    return {
      formLabelWidth: "100px",
      objdata: {
        dname: "",
        parentId: '',
        pbelonging:'1'
      },

    }
  },
 created(){
   if(this.$route.query.state == 'new'){
      this.objdata.parentId= this.$route.query.id
   }
  
   if ( this.$route.query.state == 'edit'){
    findbyidzs(this.$route.query.id ).then(res=>{
      this.objdata= res.data.depot
    })
   }
 },
  methods: {
    iptchange(val) {
      searchname(val).then(res => {
        if( res.data != 0){
            this.$message({
            type: "warning",
            message: "该名称已存在,请重新输入"
          })
          this.objdata.dname = ''
        }
      })
    },
    cancel() {
      this.$router.go(-1)
    },
    saveinfo() {
      savezs(this.objdata).then(res => {
        if (res.code === 200) {
          this.$message({
            type: "success",
            message: "新增成功"
          })
         this.$router.push( {path : '/systemManagement/organizationalStructureManagement/granaryOrganizationalStructureManagement'})
        }
      })
    },
    undateinfo() {
      updatezs(this.objdata).then(res => {
        if (res.code === 200) {
          this.$message({
            type: "success",
            message: "更新成功"
          })
         this.$router.push( {path : '/systemManagement/organizationalStructureManagement/granaryOrganizationalStructureManagement'})
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
#directlyLibrary {
  padding: 20px;
  .title {
    margin-bottom: 20px;
  }
  .main {
    margin-left: 20px;
  }
  .main_top {
    height: 40px;
    line-height: 40px;
    margin-bottom: 20px;
  }
  .main_top,
  .main_bottom {
    display: flex;
  }

  .btns {
    margin-top: 40px;
  }
}
</style>
<style>
.el-input__inner {
  width: 100%;
}
</style>