<template>
  <a-modal
    centered
    class="productBrandEdit-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="productBrandEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="产品品牌名称" prop="brandName">
            <a-input
              id="productBrandEdit-brandName"
              :maxLength="30"
              v-model.trim="form.brandName"
              @change="filterEmpty"
              placeholder="请输入产品品牌名称(最多30个字符)"
              allowClear />
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="product-brand-edit-modal-save" @click="handleSave">保存</a-button>
          <a-button id="product-brand-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 { dealerProductBrandSave } from '@/api/dealerProductBrand'
export default {
  name: 'ProductBrandEditModal',
  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: {
        brandName: '' // 产品品牌名称
      },
      rules: {
        brandName: [
          { required: true, message: '请输入产品品牌名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    filterEmpty () {
      let str = this.form.brandName
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.brandName = str
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(_this.form))
          formData.id = _this.itemId ? _this.itemId : undefined
          const fa = formData.brandName.substr(0, 1)
          if (!isNaN(fa)) {
            this.$message.error('品牌名称只能以汉字或字母开头')
            return false
          }
          _this.spinning = true
          dealerProductBrandSave(formData).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              _this.isShow = false
              _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')
        this.$refs.ruleForm.resetFields()
      } else {
        this.form.brandName = this.nowData && this.nowData.brandName ? this.nowData.brandName : ''
      }
    }
  }
}
</script>

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