<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2021-03-25 16:23:24
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="100px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.basicData.CodeRules.PropertyName')" prop="name">
        <el-input v-model="dataForm.name" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.CodeRules.PropertyName')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.CodeRules.PropertyCode')" prop="code">
        <el-input v-model="dataForm.code" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.CodeRules.PropertyCode')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.visual.AttributeValue')" prop="value">
        <el-input v-model="dataForm.value" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.visual.AttributeValue')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.CodeRules.Length')" prop="codeLength">
        <el-input v-model="dataForm.codeLength" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.CodeRules.Length')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.CodeRules.order')" prop="codeOrder">
        <el-input v-model="dataForm.codeOrder" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.CodeRules.order')])" clearable />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { CodeRulesAttrDetail, CodeRulesAttrUpdate, CodeRulesAttrAdd, CodeRulesAttrCode } from '@/api/basicData/CodeRulesAttr'

export default {
  props: {
    codeRuleId: {
      type: String,
      default: () => ({})
    }
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        code: '',
        value: '',
        codeOrder: '',
        codeLength: ''
      },
      dataRule: {
        name: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.CodeRules.PropertyName')]), trigger: 'blur' }
        ],
        code: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.CodeRules.PropertyCode')]), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          CodeRulesAttrDetail(this.dataForm.id).then(res => {
            this.dataForm = res.data
          })
        } else {
          CodeRulesAttrCode().then(res => {
            this.dataForm.code = res.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'name': this.dataForm.name,
            'code': this.dataForm.code,
            'value': this.dataForm.value,
            'codeLength': this.dataForm.codeLength,
            'codeOrder': this.dataForm.codeOrder,
            'id': this.dataForm.id,
            'codeRuleId': this.codeRuleId
          }
          if (this.dataForm.id) {
            CodeRulesAttrUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            CodeRulesAttrAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
