<template>
  <a-modal
    :title='modalTitle'
    :width='800'
    :visible='visible'
    :confirmLoading='confirmLoading'
    class='ant-modal2'
    @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 v-show='false'>
        <a-input v-decorator="['status']" type='hidden' />
      </a-form-item>

      <row-list :col='2'>
        <row-item>

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

          <a-form-item
            label='图形类别'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-select
              v-decorator="['echartType', { initValue: 1, rules: [{required: true, message: '图形不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in echartTypeMap"
                :key="value"
                :label="label"
                :value="parseInt(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-textarea
              disabled='disabled'
              v-decorator="['parameter', {rules: [{required: true, message: '报表检索条件不能为空'}]}]" />
            <a-button type="primary" style="width:30%" @click="handleParameterSelect">新增</a-button>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='报表接口类型'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-select
              v-decorator="['interType', { initValue: 1, rules: [{required: true, message: '数据提供方式不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in interTypeMap"
                :key="value"
                :label="label"
                :value="parseInt(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-textarea
              v-decorator="['sqlVal', {rules: [{required: true, message: '自定义报表名称不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label='表格展示字段'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-textarea
              placeholder='输入数据库字段的驼峰名称，逗号,隔开'
              v-decorator="['columns', {rules: [{required: true, message: '表格展示字段不能为空'}]}]" />
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <template slot='footer'>
      <a-button :loading='confirmLoading' type='primary' @click='save()'>保存</a-button>
    </template>
    <parameter-modal ref="parameterModal" @selected="handleparameterSelectd" />
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import ParameterModal from '@/views/customize/report/modules/ParameterModal'
import { addCustomizeReport, updateCustomizeReport } from '@/api/customize/report'

export default {
  name: 'BaseCustomizeReport',
  components: {
    ParameterModal
  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      echartTypeMap: [],
      interTypeMap: [],
      parameter: []
    }
  },
  props: {},
  created () {
    // 下拉框map
    this.echartTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CUSTOMIZE_REPORT_ECHART_TYPE)
    this.interTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CUSTOMIZE_REPORT_INTER_TYPE)
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'name',
          'echartType',
          'parameter',
          'interType',
          'sqlVal',
          'columns',
          'status',
          'remark'
        ])))
      })
    },
    handleParameterSelect () {
      this.$refs.parameterModal.base()
    },
    handleparameterSelectd (rows) {
      this.parameter.push(rows)
      const parameterVal = JSON.stringify(this.parameter)
      this.form.setFieldsValue({
        'parameter': parameterVal
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          values.status = 0
          addCustomizeReport(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
            this.confirmLoading = false
          })
        } else {
          updateCustomizeReport(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>
