<template>
  <div>
    <el-dialog
      title="库区"
      :visible.sync="addFormVisible"
      :before-close="handleFormClose"
      width="60%">
        <el-form
        :disabled="mode==='detail'"
          inline
          ref="addForm"
          label-position="top"
          :model="formData"
          :rules="rules"
          >
          <el-form-item
          label="库区编码"
          prop="zoneCode">
            <el-input v-model="formData.zoneCode"></el-input>
          </el-form-item>
          <el-form-item
          label="库区名称"
          prop="zoneName">
            <el-input v-model="formData.zoneName"></el-input>
          </el-form-item>
          <el-form-item
          label="库区类型"
          prop="type">
            <el-select v-model="formData.type">
              <el-option label="平面仓库区" value="1"></el-option>
              <el-option label="货架仓库区" value="2"></el-option>
              <el-option label="高架仓库区" value="3"></el-option>
              <el-option label="重力式货架库区" value="4"></el-option>
              <el-option label="堆垛机货架库区" value="5"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
          label="所属区域"
          prop="areaName">
           <PublicQuery
            title="所属区域"
            single
            :rowData.sync="areaData"
            cfgKey="bdWmsArea"
            baseUrl="/bd_web/bdweb"
            :valueField.sync="formData.areaCode"
            :textField.sync="formData.areaCode"
          ></PublicQuery>
          </el-form-item>
          <el-form-item
          label="库区温层"
          prop="tempLevel">
            <el-select v-model="formData.tempLevel">
              <el-option label="常温" value="NT"></el-option>
              <el-option label="恒温" value="TT"></el-option>
              <el-option label="冷藏" value="RT"></el-option>
              <el-option label="冷冻" value="FT"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
          label="库区面积"
          prop="zoneArea">
            <el-input v-model="formData.zoneArea"></el-input>
          </el-form-item>
          <el-form-item
          label="面积单位"
          prop="areaUnit">
            <el-select v-model="formData.areaUnit">
              <el-option label="平方米" value="M2"></el-option>
              <el-option label="平方厘米" value="CM2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
          label="备注"
          prop="remark">
            <el-input v-model="formData.remark"></el-input>
          </el-form-item>
        </el-form>
         <hr style="background-color:#F5F5F5;height:1px;border:none" v-if="mode==='detail'">
        <div class="row-mrg-t" v-if="mode==='detail'">
            <div class="operation-info">
              <span>创建人登录名(姓名)：{{formData.creatorLoginName}}({{formData.creatorNameCn}})</span>
              <span>创建时间：{{formData.createTime}}</span>
              <span>修改人登录名(姓名)：{{formData.modifierLoginName}}({{formData.modifierNameCn}})</span>
              <span>修改时间：{{formData.modifyTime}}</span>
            </div>
        </div>
        <div v-if="mode!=='detail'">
          <el-button v-auth="50400020105" type="primary" @click="submitForm">保存</el-button>
          <el-button v-auth="50400020106" type="primary" @click="submitAddForm">保存并新增</el-button>
        </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      formData: {},
      rules: {
        zoneCode: [
          { required: true, message: '库区编码必填', trigger: 'blur' }
        ],
        zoneName: [
          { required: true, message: '库区名称必填', trigger: 'blur' }
        ],
        areaCode: [
          { required: true, message: '所属区域必填', trigger: 'blur' }
        ]
      },
      areaData: {}
    }
  },
  props: {
    addFormVisible: Boolean,
    rowData: {
      type: Object,
      default: () => ({})
    },
    mode: String
  },
  methods: {

    // 发送保存数据
    psotList () {
      const url = this.$API.warehouseBasic.libraryArea.addFormSave
      const zoneModel = JSON.stringify(this.formData)
      const param = { zoneModel }
      // console.log(param)
      this.$https.post(url, param).then(res => {
        if (res.success) {
          this.$emit('saveOK')
        }
      })
    },

    // 关闭新增弹窗
    handleFormClose () {
      this.$emit('handleFormClose')
      this.$refs.addForm.resetFields()
    },

    // 保存
    submitForm () {
      console.log(this.formData)
      this.$refs.addForm.validate((vaild) => {
        if (vaild) {
          this.psotList()
          this.$nextTick(() => {
            this.handleFormClose()
          })
        } else {
          return false
        }
      })
    },

    // 保存并新增
    submitAddForm () {
      console.log(this.formData)
      this.$refs.addForm.validate((vaild) => {
        if (vaild) {
          this.psotList()
          this.$nextTick(() => {
            this.$refs.addForm.resetFields()
          })
        } else {
          return false
        }
      })
    },
  },
  watch: {
    rowData: {
      handler (item) {
        this.formData = item
      },
      deep: true
    },
    areaData: {
      handler (item) {
        this.formData.areaCode = item.areaCode
        this.formData.areaName = item.areaName
      }
    }
  }
}
</script>
