<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showEditModal"
      :close-on-click-modal="false"
      width="700px"
      :title="modalTitle"
    >
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="90">
          <Form-item label="前缀" prop="pre">
            <el-input v-model="formObj.pre" name="pre" placeholder="请输入前缀" clearable />
          </Form-item>
          <Form-item label="规则" prop="rule">
            <el-input
              v-model="formObj.rule"
              name="rule"
              type="textarea"
              placeholder="请输入规则"
              clearable
            />
          </Form-item>
          <Form-item label="长度" prop="length">
            <el-input-number
              v-model.number="formObj.length"
              :min="0"
              :step="1"
              name="length"
              style="width:100%"
              placeholder="请输入长度"
              size="small"
              controls-position="right"
              step-strictly
            />
          </Form-item>
          <Form-item label="编号截取长度" prop="sublength">
            <el-input-number
              v-model.number="formObj.sublength"
              :min="0"
              :step="1"
              name="sublength"
              style="width:100%"
              placeholder="请输入编号截取长度"
              size="small"
              controls-position="right"
              step-strictly
            />
          </Form-item>
          <Form-item label="类型" prop="type">
            <el-select v-model="formObj.type" placeholder="请选择类型" size="small" style="width:100%">
              <el-option
                v-for="item in codeTypeList"
                :key="item.id"
                :label="item.display"
                :value="item.name"
              />
            </el-select>
          </Form-item>
          <Form-item label="允许重复" prop="repeat">
            <el-radio-group v-model="formObj.repeat">
              <el-radio v-for="item in bool" :key="item.value" :label="item.value">
                {{ item.name }}
              </el-radio>
            </el-radio-group>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
/**
 * 添加编辑编号规则表
 */
import { codeRule, resEnum } from '../../../api'

export default {
  data() {
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        pre: '',
        rule: '',
        type: 'CONTRACT',
        repeat: 0,
        length: 8,
        sublength: 0
      },
      ruleValidate: {
        rule: [{ required: true, message: '规则不能为空', trigger: 'blur' }]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      codeTypeList: [],
      bool: [{ value: 0, name: '否' }, { value: 1, name: '是' }]
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          data.repeat = this.formObj.repeat
          data.type = this.formObj.type
          this._save(data)
        } else {
          this.$message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加
      if (this.$string(this.id).isEmpty()) {
        const result = await codeRule.add(data)
        this._resultChange(result, '添加成功')
      } else {
        // 编辑
        const result = await codeRule.edit({
          id: this.formObj.id,
          obj: data
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _open(formObj) {
      this.showEditModal = true
      this.$nextTick(() => {
        this.$refs.formObj.resetFields()
        this._getCodeType()
        this._hideLoading()
        if (this.$string(formObj).isEmpty()) {
          this.id = ''
          this.modalTitle = '添加编号规则'
        } else {
          this.id = formObj.id
          this.formObj = formObj
          this.formObj.type = formObj.type.value
          this.modalTitle = '编辑编号规则'
        }
      })
    },
    // 获取编号类型
    _getCodeType: async function() {
      this.codeTypeList = await resEnum.getByType('CodeType')
    }
  }
}
</script>
