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

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

      <a-form-item
        label="名称"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['beanName', {rules: [{required: true, message: '名称不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="方法名"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['methodName', {rules: [{required: true, message: '方法名不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="参数"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['params']" />
      </a-form-item>
      <a-form-item
        label="cron表达式"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['cronExpression', {rules: [{required: true, message: 'cron表达式不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="任务状态"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-select :disabled="statusFlag" v-decorator="['status', {initialValue:0,rules: [{required: true, message: '任务状态不能为空'}]}]">
          <a-select-option :value="parseInt(0)">正常</a-select-option>
          <a-select-option :value="parseInt(1)">暂停</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="备注"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['remark', {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 { addTask, updateTask } from '@/api/upms/task'
import BaseTool from '../../../../utils/tool'

export default {
  name: 'BaseInvestProjectInfo',
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      statusFlag: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false
    }
  },
  props: {
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        this.statusFlag = true
        return
      }
      this.modalTitle = '编辑'
      if (BaseTool.Object.isBlank(record.jobId)) {
        this.modalTitle = '复制'
      }
      const { form: { setFieldsValue } } = this
      this.statusFlag = false
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'jobId',
          'beanName',
          'methodName',
          'params',
          'cronExpression',
          'status',
          'remark'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        if (this.BaseTool.String.isBlank(values.jobId)) {
          addTask(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateTask(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }

  }
}
</script>
