<template>
  <a-modal
    centered
    class="warehouseEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancle="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 表单 -->
      <div>
        <a-form-model
          id="warehouseEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="仓库名称" prop="name">
            <a-input
              id="warehouseEdit-name"
              :maxLength="30"
              v-model.trim="form.name"
              @change="filterEmpty"
              placeholder="请输入仓库名称(最多30个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="排序" prop="sort">
            <a-input-number
              v-model="form.sort"
              :min="0"
              :max="999999"
              :precision="0"
              :step="1"
              placeholder="请输入排序(0~999999)"
              style="width: 100%;" />
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="warehouse-edit-modal-save" @click="handleSave">保存</a-button>
          <a-button id="warehouse-edit-modal-back" @click="isShow = false" style="margin-left: 15px;">返回列表</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { warehouseSave } from '@/api/warehouse'
export default {
  name: 'WarehouseEditModal',
  components: { STable },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {
      type: [Number, String],
      default: ''
    },
    nowData: {
      type: Object,
      default: null
    }
  },
  computed: {
    modalTit () {
      return (this.itemId ? '编辑' : '新增') + '仓库'
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        name: '', // 仓库名称
        sort: '' //  排序
      },
      rules: {
        name: [
          { required: true, message: '请输入仓库名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    filterEmpty () {
      let str = this.form.name
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.name = str
    },
    //  详情
    getDetail () {
      this.form = {
        name: this.nowData && this.nowData.name ? this.nowData.name : '',
        sort: this.nowData && (this.nowData.sort || this.nowData.sort == 0) ? this.nowData.sort : ''
      }
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = {
            id: this.itemId ? this.itemId : undefined,
            name: this.form.name, //  仓库名称
            sort: this.form.sort //  排序
          }
          _this.spinning = true
          warehouseSave(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              setTimeout(function () {
                _this.isShow = false
                _this.$refs.ruleForm.resetFields()
                _this.spinning = false
              }, 300)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.form = {
          name: '',
          sort: ''
        }
      }
    },
    itemId (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetail()
      }
    }
  }
}
</script>

<style lang="less">
  .warehouseEdit-modal{
    .ant-modal-body {
    	padding: 40px 40px 24px;
    }
    .btn-cont {
    	text-align: center;
    	margin: 35px 0 10px;
    }
  }
</style>
