<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="showModal"
      :close-on-click-modal="false"
      width="700px"
      :title="modalTitle"
    >
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="中文名" prop="cn">
            <el-tooltip content="请输入中文且长度为0-30" placement="bottom-start">
              <el-input v-model="formObj.cn" name="cn" placeholder="请输入中文名" :maxlength="30" style="width: 100%" />
            </el-tooltip>
          </Form-item>
          <Form-item label="英文名" prop="en">
            <el-tooltip content="请输入英文且长度为0-40" placement="bottom-start">
              <el-input v-model="formObj.en" name="en" placeholder="请输入英文名" :maxlength="40" style="width: 100%" />
            </el-tooltip>
          </Form-item>
          <Form-item label="二字码" prop="code2">
            <el-tooltip content="请输入两个大写英文字符" placement="bottom-start">
              <el-input v-model="formObj.code2" name="code2" placeholder="请输入二字码" :maxlength="2" style="width: 100%" />
            </el-tooltip>
          </Form-item>
          <Form-item label="三字码" prop="code3">
            <el-tooltip content="请输入三个大写英文字符" placement="bottom-start">
              <el-input v-model="formObj.code3" name="code3" placeholder="请输入三字码" :maxlength="3" style="width: 100%" />
            </el-tooltip>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { sysCountry } from '../../../api'

export default {
  data() {
    const validateCn = (rule, value, callback) => {
      // 验证中文名称
      const reg = /[\u4E00-\u9FA5]+$/
      if (value === '') {
        callback(new Error('中文名不能为空'))
      } else if (!reg.test(value)) {
        callback(new Error('只能输入中文字符'))
      } else {
        callback()
      }
    }
    const validateEn = (rule, value, callback) => {
      // 验证英文名称
      const reg = /^[A-Za-z\s]+$/
      if (value === '') {
        callback(new Error('英文名不能为空'))
      } else if (!reg.test(value)) {
        callback(new Error('只能输入英文'))
      } else {
        callback()
      }
    }
    const validateCode2 = (rule, value, callback) => {
      // 验证二字码
      const reg = /^[A-Z]{2}$/
      if (value === '') {
        callback(new Error('二字码不能为空'))
      } else if (!reg.test(value)) {
        callback(new Error('请输入两个大写英文字符'))
      } else {
        callback()
      }
    }
    const validateCode3 = (rule, value, callback) => {
      // 验证二字码
      const reg = /^[A-Z]{3}$/
      if (value === '') {
        callback(new Error('三字码不能为空'))
      } else if (!reg.test(value)) {
        callback(new Error('请输入三个大写英文字符'))
      } else {
        callback()
      }
    }
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        cn: '',
        code2: '',
        code3: '',
        en: ''
      },
      ruleValidate: {
        cn: [{ required: true, validator: validateCn, trigger: 'blur' }],
        code2: [{ required: true, validator: validateCode2, trigger: 'blur' }],
        code3: [{ required: true, validator: validateCode3, trigger: 'blur' }],
        en: [{ required: true, validator: validateEn, trigger: 'blur' }]
      },
      showModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  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.showModal = 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')
          this._save(data)
        } else {
          this.$message.error('表单验证失败!')
          this._hideLoading()
        }
      })
    },
    _save: async function(data) {
      if (this.$string(this.id).isEmpty()) {
        const result = await sysCountry.add(data)
        this._resultChange(result, '添加成功!')
      } else {
        const result = await sysCountry.edit({ id: this.formObj.id, obj: data })
        this._resultChange(result, '编辑成功!')
      }
    },
    _cancel() {
      this.showModal = false
    },
    _open(formObj) {
      this.showModal = true
      this.$nextTick(() => {
        this.$refs.formObj.resetFields()
        this._hideLoading()
        if (this.$string(formObj).isEmpty()) {
          this.id = ''
          this.modalTitle = '添加国家'
        } else {
          this.id = formObj.id
          this.formObj = formObj
          this.modalTitle = '编辑国家'
        }
      })
    }
  }
}
</script>
