<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="areaCode">
            <el-input v-model="formData.areaCode" :disabled="mode==='edit'"></el-input>
          </el-form-item>
          <el-form-item
          label="库区名称"
          prop="areaName">
            <el-input v-model="formData.areaName"></el-input>
          </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="50400010105" type="primary" @click="submitForm">保存</el-button>
          <el-button v-auth="50400010106" type="primary" @click="submitAddForm">保存并新增</el-button>
        </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      formData: {},
      rules: {
        areaCode: [
          { required: true, message: '库区编码必填', trigger: 'blur' }
        ],
        areaName: [
          { required: true, message: '库区名称必填', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    addFormVisible: Boolean,
    rowData: {
      type: Object,
      default: () => ({})
    },
    mode: String
  },
  methods: {

    // 发送保存数据
    psotList () {
      const url = this.$API.warehouseBasic.libraryRegion.addFormSave
      const areaModel = JSON.stringify(this.formData)
      const param = { areaModel }
      // 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 () {
      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) {
        // console.log('编辑数据：',row)
        this.formData = item
      },
      deep: true
    }
  }
}
</script>
<style lang="scss" scoped>
  .operation-info {
    display: flex;
    justify-content: space-between;
    }
</style>
