<template>
  <a-card :bordered="false" v-show="visible" class="card" :title="modalTitle">
    <a-row :gutter="48" slot="extra">
      <a-col :md="48" :sm="48">
        <span class="table-page-search-submitButtons" style="float: right">
          <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
          <a-button style="margin-left: 8px" type="default" @click="handleCancel()">返回</a-button>
        </span>
      </a-col>
    </a-row>
    <a-form :form="form">

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

      <row-list :col="2">
        <row-item>
          <a-form-item label="关联类型" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-select v-decorator="['linkType', {rules: [{required: true, message: '关联类型不能为空'}]}]">
              <a-select-option v-for="(label,value) in linkTypeMap" :value="value" :key="value">
                {{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item label="关联流程" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-select v-model="flowId">
              <a-select-option v-for="{name,id} in workFlowList" :value="id" :key="id">
                {{ name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="2">
        <row-item>
          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-textarea
              :rows="3"
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { addCustomWorkflowRelation, updateCustomWorkflowRelation } from '@/api/custom/flowrelation'
import { queryWorkflow } from '@/api/workflow/workflow'

export default {
  name: 'BaseCustomWorkflowRelation',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      flowId: null,
      linkTypeMap: {},
      workFlowList: []
      // 下拉框map
    }
  },
  props: {},
  created () {
    // 下拉框map
    this.linkTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.LINK_TYPE)
    queryWorkflow().then((res) => {
      this.workFlowList = res.data
    })
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加关联'
        return
      }
      this.modalTitle = '编辑关联'
      const { form: { setFieldsValue } } = this
      this.flowId = record.flowId
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'linkType',
          'flowId',
          'remark'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        values.flowId = this.flowId
        if (!values.flowId) {
          this.$message.error('流程不可为空')
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addCustomWorkflowRelation(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
            this.confirmLoading = false
          })
        } else {
          updateCustomWorkflowRelation(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)
      } else {
        this.$emit('ok')
      }
    }
  }
}
</script>
