<template>
  <a-modal
    centered
    class="transferTypeManagementEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <!-- 表单 -->
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="transferTypeManagementEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="父类型">
            {{ parentName }}
          </a-form-model-item>
          <a-form-model-item label="调拨类型名称" prop="name">
            <a-input
              id="transferTypeManagementEdit-name"
              :maxLength="30"
              v-model.trim="form.name"
              @change="filterEmpty"
              placeholder="请输入调拨类型名称(最多30个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="价格类型" prop="erpPriceType">
            <v-select
              code="ERP_PRICE_TYPE"
              ref="erpPriceType"
              id="transferTypeManagementEdit-erpPriceType"
              placeholder="请选择价格类型"
              v-model="form.erpPriceType"
            ></v-select>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="transferTypeManagementEdit-cancel" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
          <a-button type="primary" id="transferTypeManagementEdit-save" @click="handleSave">保存</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { VSelect } from '@/components'
// 接口
import { allocateTypeSave } from '@/api/allocateType'
export default {
  name: 'TransferTypeManagementEditModal',
  mixins: [commonMixin],
  components: { VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    },
    nowData: {
      type: Object,
      default: null
    }
  },
  computed: {
    // 弹窗标题
    modalTit () {
      return (this.itemSn ? '编辑' : this.nowData ? '添加子级' : '添加一级') + '调拨类型'
    },
    parentName () {
      return this.nowData && this.nowData.superior ? this.nowData.superior.name : '--'
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      // form 表单
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      // 提交参数
      form: {
        name: '', // 调拨类型名称
        allocateCategory: undefined, //  父节点sn
        erpPriceType: undefined //  价格类型
      },
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '请输入调拨类型名称', trigger: 'change' }
        ],
        erpPriceType: [
          { required: true, message: '请选择价格类型', trigger: 'change' }
        ]
      }
    }
  },
  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
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = _this.form
          form.allocateTypeSn = _this.itemSn ? _this.itemSn : null
          _this.spinning = true
          allocateTypeSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('close', 1)
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close', 0)
        this.$refs.ruleForm.resetFields()
      } else {
        if (this.nowData) { //  编辑 或 添加子级
          this.form.name = this.nowData && this.nowData.name ? this.nowData.name : ''
          this.form.allocateCategory = this.nowData && this.nowData.allocateCategory ? this.nowData.allocateCategory : ''
          this.form.erpPriceType = this.nowData && this.nowData.erpPriceType ? this.nowData.erpPriceType : undefined
          this.form.treeLevel = this.nowData && this.nowData.treeLevel ? this.nowData.treeLevel : ''
        } else { //  添加一级分类
          this.form.name = ''
          this.form.allocateCategory = 0
          this.form.erpPriceType = undefined
          this.form.treeLevel = 1
        }
      }
    },
    itemSn (newValue, oldValue) {
    }
  }
}
</script>

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