<template>
  <div>
    <a-spin :spinning="spinning" tip="Loading...">
      <a-modal class="modalBox" :title="titleText" v-model="isshow" @cancle="cancel">
        <a-form :form="form" @submit="handleSubmit">
          <!-- 类别编码 -->
          <a-form-item label="类别编码" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
            <a-input
              placeholder="请输入类别编码"
              :disabled="true"
              v-decorator="['formData.code', {
                initialValue: formData.code,
                rules: [{ required: true, message: '请输入类别编码!' }] }]"
            />
          </a-form-item>

          <!-- 名称 -->
          <a-form-item label="名称" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
            <a-input
              placeholder="请输入名称(最多30个字符)"
              :maxLength="30"
              v-decorator="['formData.name', {
                initialValue: formData.name,
                rules: [{ required: true, message: '请输入名称!' }] }]"
            />
          </a-form-item>

          <!-- 排序值 -->
          <a-form-item label="排序值" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
            <a-input-number
              style="width: 100%;"
              placeholder="请输入排序值(0~999999)"
              v-decorator="['formData.sort', {
                initialValue: formData.sort,
                rules: [{ required: true, message: '请输入排序值!' }] }]"
              :max="999999"
              :min="0"
            />
          </a-form-item>
          <!-- 状态 -->
          <a-form-item label="启用" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
            <a-radio-group
              name="radioGroup"
              v-decorator="[
                'formData.isEnable',
                {
                  initialValue: formData.isEnable,
                  rules: [{ required: true, message: '请选择状态!' }] },
              ]"
            >
              <a-radio :value="1">是</a-radio>
              <a-radio :value="0">否</a-radio>
            </a-radio-group>
          </a-form-item>

          <a-form-item label="备注" :label-col="{ span: 5 }" :wrapper-col="{ span: 14 }">
            <a-textarea
              v-decorator="[
                'formData.remarks',
                { initialValue: formData.remarks,
                  rules: [] },
              ]"
              :maxLength="500"
              style="min-height: 60px;"
              placeholder="最多500字符"
              autosize />
          </a-form-item>

          <a-form-item :wrapper-col="{ span: 12, offset: 5 }">
            <a-button type="primary" @click="handleSubmit()">
              保存
            </a-button>
            <a-button :style="{ marginLeft: '8px' }" @click="handleCancel()">
              取消
            </a-button>
          </a-form-item>
        </a-form>
      </a-modal>
    </a-spin>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { saveLookUp } from '@/api/lookup.js'
export default {
  name: 'CodeModal',
  components: {
    STable, VSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      spinning: false,
      titleText: '添加字典',
      isshow: this.visible,
      formLayout: 'horizontal',
      form: this.$form.createForm(this, { name: 'miniForm' }),
      formData: {
        name: '',
        code: '',
        sort: '',
        isEnable: '', // 活动状态
        remarks: ''
      }

    }
  },
  methods: {
    cancel (e) {
      this.clear()
      this.$emit('close')
    },
    // 保存提交
    handleSubmit () {
      const _this = this
      this.form.validateFields((err, values) => {
        if (!err) {
          _this.spinning = true
          saveLookUp(Object.assign(this.formData, values.formData)).then(res => {
            if (res.status + '' === '200') {
              this.$message.success(res.message ? res.message : '保存成功')
              this.$emit('refresh')
              setTimeout(function () {
                _this.cancel()
                _this.spinning = false
              }, 300)
            } else {
              _this.spinning = false
              // this.$message.warning(res.message)
            }
          })
        }
      })
    },
    // 取消
    handleCancel () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定取消吗?',
        centered: true,
        onOk () {
          _this.clear()
          _this.cancel()
        }
      })
    },
    clear () {
      this.form.resetFields()
      delete this.formData.id
      this.formData.name = ''
      this.formData.code = ''
      this.formData.sort = ''
      this.formData.isEnable = ''
      this.formData.remarks = ''
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this, { name: 'miniForm' })
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (newValue) {
        if (this.data.id) { // 编辑
          this.formData.titleText = '编辑字典'
          this.formData = Object.assign({}, this.data)
          delete this.formData.no
          this.formData.isEnable = Number(this.formData.isEnable)
        } else {
          this.formData.titleText = '添加字典'
        }
      } else {
        this.cancel()
      }
    }
  }
}
</script>

<style >
  .ant-modal-footer {
    display: none;
  }
</style>
