<template>
  <a-modal
    centered
    class="productCategoryEdit-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="productCategoryEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="所属分类" v-if="parentType">
            {{ parentType }}
          </a-form-model-item>
          <a-form-model-item label="产品分类名称" prop="productTypeName">
            <a-input
              v-model.trim="form.productTypeName"
              @change="filterEmpty"
              id="productCategoryEdit-name"
              :maxLength="30"
              allowClear
              placeholder="请输入产品分类名称(最多30个字符)" />
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="product-category-edit-modal-back" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
          <a-button type="primary" id="product-category-edit-modal-save" @click="handleSave">保存</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable } from '@/components'
// 接口
import { productTypeSave } from '@/api/productType'
export default {
  name: 'ProductCategoryEditModal',
  mixins: [commonMixin],
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {// 当前分类sn
      type: [Number, String],
      default: ''
    },
    nowData: {// 当前分类数据信息
      type: Object,
      default: null
    }
  },
  computed: {
    // 弹窗标题
    modalTit () {
      return (this.itemSn ? '编辑' : '新增') + '产品分类'
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // 表单布局设置
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      // 表单数据
      form: {
        productTypeName: '' // 产品分类名称
      },
      // 表单验证规则
      rules: {
        productTypeName: [
          { required: true, message: '请输入产品分类名称', trigger: 'blur' }
        ]
      },
      parentType: '' //  父级分类
    }
  },
  methods: {
    // 筛选符合格式的产品分类名称
    filterEmpty () {
      let str = this.form.productTypeName
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.productTypeName = str
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(_this.form))
          if (_this.itemSn) { //  编辑
            formData.productTypeSn = _this.nowData && _this.nowData.productTypeSn ? _this.nowData.productTypeSn : undefined
          } else { //  新增子级
            formData.parentSn = _this.nowData && _this.nowData.productTypeSn ? _this.nowData.productTypeSn : undefined
          }
          _this.spinning = true
          productTypeSave(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 {
        //  获取父级分类 名称
        if (this.nowData && this.nowData.namePaths) {
          let namePathsArr = []
          namePathsArr = this.nowData.namePaths.split(',')
          if (this.itemSn) { //  编辑    编辑时不显示当前分类
            namePathsArr = namePathsArr.slice(0, namePathsArr.length - 1)
          }
          this.parentType = ''
          namePathsArr.map((item, index) => {
            this.parentType += item + ' > '
          })
          this.parentType = this.parentType ? this.parentType.substr(0, this.parentType.length - 3) : ''
        } else {
          this.parentType = ''
        }
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.form.productTypeName = this.nowData && this.nowData.productTypeName ? this.nowData.productTypeName : ''
      } else {
        this.form.productTypeName = ''
      }
    }
  }
}
</script>

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