<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="['code', {rules: [{required: true, message: '配置类型不能为空'}]}]" placeholder="请选择">
              <a-select-option v-for="(label,value) in classNameCodeMap" :key="value" :label="label" :value="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-input :disabled="!publicFlag" v-decorator="['title', {rules: [{required: true, message: '名称不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item label="自定义处理" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-switch v-decorator="['publicFlag', {initialValue: true, valuePropName: 'checked'}]" @change="changeSwitch" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item label="数据表" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-input :disabled="!publicFlag" v-decorator="['titleCode', {rules: [{required: true, message: '名称编码不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item label="数据列(中文)" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-input v-decorator="['titleColumnName', {rules: [{required: true, message: '具体列名不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item label="数据列(英文)" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-input v-decorator="['titleColumnCode', {rules: [{required: true, message: '具体列编码不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item label="数据处理类" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-input :disabled="!publicFlag" v-decorator="['handleName', {rules: [{required: true, message: '处理类不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item label="数据处理类编码" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-input :disabled="!publicFlag" v-decorator="['handleCode', {rules: [{required: true, message: '处理类编码不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item label="数据处理方法" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-input :disabled="!publicFlag" v-decorator="['methodName', {rules: [{required: true, message: '方法名称不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item label="数据处理方法编码" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-input :disabled="!publicFlag" v-decorator="['methodCode', {rules: [{required: true, message: '方法名称编码不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item label="备注" :labelCol="BaseTool.Constant.labelCol" :wrapperCol="BaseTool.Constant.wrapperCol">
            <a-input v-decorator="['remark']" />
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { addCustomClassName, updateCustomClassName } from '@/api/custom/className'
export default {
  name: 'BaseCustomClassName',
  data() {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      publicFlag: true,
      // 下拉框map
      classNameCodeMap: {},
    }
  },
  props: {},
  created() {
    // 下拉框map
    this.classNameCodeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CLASS_NAME_CODE)
  },
  methods: {
    base(record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      const {
        form: { setFieldsValue },
      } = this
      // 日期处理
      record.publicFlag = Boolean(record.publicFlag)
      this.$nextTick(() => {
        setFieldsValue(
          Object.assign(
            pick(record, [
              'id',
              'code',
              'title',
              'titleCode',
              'methodName',
              'methodCode',
              'remark',
              'titleColumnName',
              'titleColumnCode',
              'handleName',
              'handleCode',
              'publicFlag',
            ])
          )
        )
      })
    },
    changeSwitch(val) {
      this.publicFlag = val
      if (!val) {
        const {
          form: { setFieldsValue },
        } = this
        this.$nextTick(() => {
          setFieldsValue({
            title: '公共处理',
            titleCode: 't_common',
            handleName: '公共处理类',
            handleCode: 'common',
            methodName: '公共处理方法',
            methodCode: 'common_method',
          })
        })
      }
    },
    save() {
      const {
        form: { validateFieldsAndScroll },
      } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.publicFlag = Number(values.publicFlag)
        if (this.BaseTool.String.isBlank(values.id)) {
          addCustomClassName(values)
            .then(() => {
              this.handleCancel(values)
            })
            .catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateCustomClassName(values)
            .then(() => {
              this.handleCancel(values)
            })
            .catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel(values) {
      this.visible = false
      this.confirmLoading = false
      this.publicFlag = true
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      } else {
        this.$emit('ok')
      }
    },
  },
}
</script>
