<template>
  <div>
    <a-modal class="modalBox" :title="titleText" v-model="isshow" @cancle="cancel">
      <a-form :form="form" @submit="handleSubmit">
        <a-row :gutter="24">
          <a-col :span="24">
            <!-- code -->
            <a-form-item label="字典码" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
              <a-input
                disabled
                placeholder=""
                v-decorator="['formData.lookupCode', {
                  initialValue: formData.lookupCode,
                  rules: [] }]"
              />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :span="12" style="padding-right: 0px;">
            <!-- 名称 -->
            <a-form-item label="名称" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-input
                placeholder="请输入名称"
                v-decorator="['formData.dispName', {
                  initialValue: formData.dispName,
                  rules: [{ required: true, message: '请输入名称!' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <!-- 项编码 -->
            <a-form-item label="项编码" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-input
                placeholder="请输入项编码"
                v-decorator="['formData.code', {
                  initialValue: formData.code,
                  rules: [{ required: true, message: '请输入项编码!' }] }]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12" style="padding-right: 0px;">
            <a-form-item label="启用" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-radio-group
                name="radioGroup"
                v-decorator="[
                  'formData.isEnable',
                  {
                    initialValue: formData.isEnable,
                    rules: [] },
                ]"
              >
                <a-radio :value="1">是</a-radio>
                <a-radio :value="0">否</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <!-- 排序值 -->
          <a-col :span="12">
            <a-form-item label="排序值" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-input
                type="number"
                placeholder="请输入排序值"
                v-decorator="['formData.sort', {
                  initialValue: formData.sort,
                  rules: [{ required: true, message: '请输入排序值!' }] }]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <!-- 扩展参数 -->
        <a-row :gutter="24">
          <a-col :span="12" style="padding-right: 0px;">
            <!-- 扩展一 -->
            <a-form-item label="扩展一" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-input
                placeholder=""
                v-decorator="['formData.attr1', {
                  initialValue: formData.attr1,
                  rules: [] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <!-- 扩展二 -->
            <a-form-item label="扩展二" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-input
                placeholder=""
                v-decorator="['formData.attr2', {
                  initialValue: formData.attr2,
                  rules: [] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12" style="padding-right: 0px;">
            <!-- 扩展三 -->
            <a-form-item label="扩展三" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-input
                placeholder=""
                v-decorator="['formData.attr3', {
                  initialValue: formData.attr3,
                  rules: [] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <!-- 扩展四 -->
            <a-form-item label="扩展四" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-input
                placeholder=""
                v-decorator="['formData.attr4', {
                  initialValue: formData.attr4,
                  rules: [] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12" style="padding-right: 0px;">
            <!-- 扩展五 -->
            <a-form-item label="扩展五" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-input
                placeholder=""
                v-decorator="['formData.attr5', {
                  initialValue: formData.attr5,
                  rules: [] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <!-- 扩展六 -->
            <a-form-item label="扩展六" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-input
                placeholder=""
                v-decorator="['formData.attr6', {
                  initialValue: formData.attr6,
                  rules: [] }]"
              />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-item label="备注" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
              <a-textarea
                v-decorator="[
                  'formData.remarks',
                  {
                    initialValue: formData.remarks,
                    rules: [] },
                ]"
                style="min-height: 50px;"
                placeholder=""
                autosize />
            </a-form-item>
          </a-col>
        </a-row>

        <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>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { saveLookUpItem } from '@/api/lookup.js'
export default {
  name: 'CodeItemModal',
  components: {
    STable, VSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      titleText: '添加字典项',
      isshow: this.visible,
      formLayout: 'horizontal',
      form: this.$form.createForm(this, { name: 'miniForm' }),
      roleList: [],
      formData: {
        lookupCode: '',
        code: '', // code
        dispName: '',
        isEnable: 1, // 活动状态
        sort: '',
        attr1: '',
        attr2: '',
        attr3: '',
        attr4: '',
        attr5: '',
        attr6: '',
        remarks: ''
      }

    }
  },
  methods: {
    cancel (e) {
      this.clear()
      this.$emit('close')
    },

    // 保存提交
    handleSubmit () {
      const _this = this
      this.form.validateFields((err, values) => {
        console.log(values, 'values222')
        if (!err) {
          saveLookUpItem(Object.assign(this.formData, values.formData)).then(res => {
            console.log(res, 'res--save')
            if (res.status + '' === '200') {
              this.$message.success(res.message)
              this.$emit('refresh')
              setTimeout(function () {
                _this.cancel()
              }, 300)
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    },
    // 取消
    handleCancel () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定取消吗?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          _this.clear()
          _this.cancel()
        }
      })
    },
    clear () {
      this.form.resetFields()
      delete this.formData.id
      this.formData.lookupCode = ''
      this.formData.code = ''
      this.formData.dispName = ''
      this.formData.isEnable = 1
      this.formData.sort = ''
      this.formData.attr1 = ''
      this.formData.attr2 = ''
      this.formData.attr3 = ''
      this.formData.attr4 = ''
      this.formData.attr5 = ''
      this.formData.attr6 = ''
      this.formData.remarks = ''
    }

  },
  mounted () {
  },
  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.titleText = '编辑字典项'
          this.formData = Object.assign({}, this.data)

          delete this.formData.no
          this.formData.isEnable = Number(this.formData.isEnable)
          console.log(this.formData, 'this.formData')
        } else {
          this.formData.lookupCode = this.data.lookupCode
          this.titleText = '添加字典项'
        }
      } else {
        this.cancel()
      }
    }
  }
}
</script>

<style >
  .modalBox{
    width: 650px !important;
  }
  .ant-modal-footer {
    display: none;
  }
</style>
