<template>
  <a-modal
    :title="modalTitle"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-form :form='form'>
      <row-list :col='2'>
        <row-item>
          <a-form-item
            label='条件名称'
            :labelCol='BaseTool.Constant.labelCol'
            :wrapperCol='BaseTool.Constant.wrapperCol'
          >
            <a-input
              v-decorator="['label', {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="['key', {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
              @change="selectVal()"
              v-decorator="['type', { initValue: 1, rules: [{required: true, message: '字段类型为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in parameterTypeMap"
                :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-textarea placeholder='select类型的值，请以英文,号隔开'
                        v-decorator="['value']" />
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="handleCancel()">取消</a-button>
      <a-button :loading="confirmLoading" type="primary" @click="handleSelect()">确定</a-button>
    </template>
  </a-modal>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import Detail from './Detail'

export default {
  name: 'ParameterModal',
  components: {
    STable,
    Ellipsis,
    Detail
  },
  props: {
    type: {
      type: String,
      default: 'radio'
    },
    selectedRowKey: {
      type: Array,
      default: () => {
        return []
      }
    },
    selectedRow: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      modalTitle: null,
      visible: false,
      record: null,
      form: this.$form.createForm(this),
      // 查询参数
      queryParam: {
      },
      extraQueryParam: {
      },
      projectStartTimeStart: undefined,
      projectStartTimeEnd: undefined,

      // 下拉框map
      selectedRowKeys: [],
      selectedRows: [],
      parameterTypeMap: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false,
      isCreated: false
    }
  },
  created () {
    // 下拉框map
    this.parameterTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PARAMETER_TYPE)
  },
  methods: {
    selectVal (val) {
      this.form.setFieldsValue({
        'value': ''
      })
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
      }
      this.$refs.table.refresh(true)
    },
    base (record) {
      this.visible = true
      this.modalTitle = '新增查询条件'
      this.form.resetFields()
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
    },
    handleSelect () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          return
        }
        console.log(values)
        this.$emit('selected', values)
        this.confirmLoading = false
        this.visible = false
      })
    },
    mySelect (selectedRowKeys, selectedRows) {
      if (this.type === 'radio') {
        this.$refs.table.updateSelect(selectedRowKeys, selectedRows)
        this.$refs.table.rowSelection.onChange(selectedRowKeys, selectedRows)
      } else {
        let mySelectedRowKeys
        let mySelectedRows = this.selectedRows.filter(item => item.id !== selectedRowKeys[0])
        if (this.selectedRowKeys.includes(selectedRowKeys[0])) {
          mySelectedRowKeys = this.selectedRowKeys.filter(item => item !== selectedRowKeys[0])
        } else {
          mySelectedRowKeys = [...selectedRowKeys, ...this.selectedRowKeys]
          mySelectedRows = [...mySelectedRows, ...selectedRows]
        }
        this.$refs.table.updateSelect(mySelectedRowKeys, mySelectedRows)
        this.$refs.table.rowSelection.onChange(mySelectedRowKeys, mySelectedRows)
      }
    }
  }
}
</script>
