<template>
  <a-modal
    title="详情"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
      </a-form-item>

      <a-form-item
        label="类型"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['type', {rules: [{required: true, message: '类型不能为空'}]}]" />
      </a-form-item>

      <a-form-item
        label="类型名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['typeName', {rules: [{required: true, message: '类型名称不能为空'}]}]" />
      </a-form-item>

      <a-form-item
        label="名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['label', {rules: [{required: true, message: '名称不能为空'}]}]" />
      </a-form-item>

      <a-form-item
        label="值"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['value', {rules: [{required: true, message: '值不能为空'}]}]" />
      </a-form-item>

      <a-form-item
        label="排序"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['sort', {initialValue:1,initialValue:1,rules: [{required: true, message: '排序不能为空'}]}]" type="number" />
      </a-form-item>

      <a-form-item
        label="标识"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input v-decorator="['code', {rules: [{required: true, message: '标识不能为空'}]}]"/>
      </a-form-item>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { updateLog, addLog } from '@/api/upms/log'

export default {
  name: 'BaseLog',
  data () {
    return {
      checked: true,
      confirmLoading: false,
      currentStep: 0,
      mdl: {},
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false
    }
  },
  props: {
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        // setFieldsValue(pick(record, ['id', 'type', 'typeName', 'label', 'value', 'valueType', 'sort', 'code']))
        setFieldsValue(Object.assign(pick(record, ['id', 'type', 'typeName', 'label', 'value', 'sort', 'code']),
          { 'valueType': record.valueType + '' }))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
        } else {
          if (this.BaseTool.String.isBlank(values.id)) {
            addLog(values)
              .then(() => {
                this.handleCancel(values)
              }).catch(() => {
                this.confirmLoading = false
              })
          } else {
            updateLog(values)
              .then(() => {
                this.handleCancel(values)
              }).catch(() => {
                this.confirmLoading = false
              })
          }
        }
        if (!errors) {

        } else {

        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }

  }
}
</script>
