<template>
  <div>
    <a-modal
      class="modalBox"
      :footer="null"
      centered
      :title="titleText"
      v-model="isshow"
      @cancle="cancel"
      :width="600">
      <a-form :form="form" ref="form" @submit="handleSubmit">
        <a-row :gutter="24">
          <a-col :span="20">
            <!-- 考评项目名称 -->
            <a-form-item label="考评项目名称:" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
              <a-input
                allowClear
                id="addEvaItemModal-name"
                :maxLength="128"
                v-decorator="[
                  'formData.name',
                  { initialValue: formData.name,
                    getValueFromEvent: $filterEmpty,
                    rules: [{ required: true, message: '请输入考评项目名称!' }] },
                ]"
                placeholder="请输入考评项目名称(最多128个字符)" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="20">
            <!-- 考评项目说明 -->
            <a-form-item label="考评项目说明:" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
              <a-textarea
                allowClear
                id="addEvaItemModal-desc"
                :maxLength="500"
                Input.TextArea
                :autoSize="{minRows:5}"
                v-decorator="[
                  'formData.desc',
                  { initialValue: formData.desc,getValueFromEvent: $filterEmpty },
                ]"
                placeholder="请输入考评项目说明(最多500个字符)" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-form-item :wrapper-col="{ span: 15, offset: 9 }">
          <a-button :loading="loading" id="addEvaItemModal-submit" type="primary" @click="handleSubmit()">
            保存
          </a-button>
          <a-button id="addEvaItemModal-cancel" :style="{ marginLeft: '8px' }" @click="cancel()">
            取消
          </a-button>
        </a-form-item>
      </a-form>

    </a-modal>
  </div>
</template>

<script>
import {
  STable,
  VSelect
} from '@/components'
import {
  itemDetail,
  itemSave
} from '@/api/evaluationItem.js'
export default {
  name: 'AddEvaItemModal',
  components: {
    STable,
    VSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    // 要编辑的项目id
    itemId: {
      type: [String, Number],
      default: ''
    }
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (newValue) {
        this.form.resetFields()
        // 编辑查详情
        if (this.itemId) {
          this.getFormData(this.itemId)
        }
      } else {
        this.cancel()
      }
    }
  },
  data () {
    return {
      isshow: this.visible, // 弹框显示隐藏
      form: this.$form.createForm(this, {
        name: 'AddEvaItemModal'
      }),
      formData: {
        name: '', // 方案名称
        desc: '' // 方案说明
      },
	  loading: false // 确定按钮loading
    }
  },
  computed: {
    // 弹框标题
    titleText () {
      return this.itemId ? '编辑' : '新增'
    }
  },
  methods: {
    // 点击弹框x号触发事件
    cancel (e) {
      this.formData.name = ''
      this.formData.desc = ''
      this.$emit('close')
    },
    // 查详情
    getFormData (id) {
      itemDetail({
        id: id
      }).then(res => {
        if (res.status == 200) {
          // console.log(res, 'rrrrrrrrrr')
          this.formData = Object.assign(this.formData, res.data)
          console.log(this.formData, 'this.formData')
        }
      })
    },
    // 保存提交
    handleSubmit () {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true
          const params = this.itemId ? Object.assign({
            id: this.itemId
          }, values.formData) : values.formData
          // console.log(params, 'ppppppppppppp')
          itemSave(params).then(res => {
            console.log(res, 'res--save')
            if (res.status + '' === '200') {
              this.$message.success(res.message ? res.message : '保存成功')
              this.$emit('refresh')
              setTimeout(() => {
                this.cancel()
              }, 300)
            } else {
              // this.$message.warning(res.message)
            }
            this.loading = false
          })
        }
      })
    },
    // 取消
    handleCancel () {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定取消吗?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          _this.cancel()
        }
      })
    },
    clear () {
      this.form.resetFields()
    }

  },
  beforeCreate () {
    this.form = this.$form.createForm(this, {
      name: 'AddEvaItemModal'
    })
  }

}
</script>

<style scoped>
	.modalBox {}

	.ant-modal-footer {
		display: none;
	}

	.time-text {
		color: #1890FF;
		padding: 0px 20px;
		cursor: pointer;
	}

	.labelT {
		position: absolute;
		left: -135px;
		top: 20px;
		color: rgba(0, 0, 0, 0.85);
	}
</style>
