<template>
  <!-- <div id="directlyLibrary"> -->
  <div class="global">
    <div class="mainContainer">
      <div class="title">新增仓房</div>

      <div class="main">
        <div class="main_top">
          <div>仓房名称:</div>
          <div class>
            <el-input @input="iptchange" v-model="objdata.hname" placeholder="请输入内容" />
          </div>
        </div>
        <div class="main_bottom">
          <div>所属:</div>
          <div>山西分公司></div>
          <div>中央储备粮太原直属库有限公司</div>
        </div>
        <div class="btns">
          <el-button @click="cancel">取消</el-button>
          <el-button @click="savecf"  v-if="this.$route.query.state != 'edit'" >保存</el-button>
          <el-button @click="editcf"  v-if="this.$route.query.state == 'edit'" >保存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  searchcfname,
  savecf,
  findbyidcf,
  updatecf
} from "@/api/organizationalStructureManagement/index.js"
export default {
  name: "AddWarehouse",
  data() {
    return {
      formLabelWidth: "100px",
      objdata: {
        hname: "",
        depotId: this.$route.query.id
      }
    }
  },
  created() {
    if (this.$route.query.state == "new") {
      // this.objdata.parentId= this.$route.query.id
    }

    if (this.$route.query.state == "edit") {
      findbyidcf(this.$route.query.hid ).then(res=>{
        this.objdata= res.data
      })
    }
  },
  methods: {
    iptchange(val) {
      searchcfname(this.$route.query.id, val).then(res => {
        if (res.data != 0) {
          this.$message({
            type: "warning",
            message: "该名称已存在,请重新输入"
          })
          this.objdata.hname = ""
        }
      })
    },

    savecf() {
      savecf(this.objdata).then(res => {
        if (res.code === 200) {
          this.$message({
            type: "success",
            message: "新增成功"
          })
          this.$router.push({
            path:
              "/systemManagement/organizationalStructureManagement/granaryOrganizationalStructureManagement"
          })
        }
      })
    },
    editcf() {
      updatecf(this.objdata).then(res => {
        if (res.code === 200) {
          this.$message({
            type: "success",
            message: "更新成功"
          })
          this.$router.push({
            path:
              "/systemManagement/organizationalStructureManagement/granaryOrganizationalStructureManagement"
          })
        }
      })
    },
    cancel() {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="scss" scoped>
.mainContainer {
  padding: 20px;
  .title {
    margin-bottom: 20px;
  }
  .main {
    margin-left: 40px;
  }
  .main_top {
    height: 40px;
    line-height: 40px;
    margin-bottom: 20px;
  }
  .main_top,
  .main_bottom {
    display: flex;
  }

  .el-input--medium {
    width: 100% !important;
  }

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