<template>
  <p-modal
    title="添加规则"
    width="50%"
    :visible="visible"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <p-grid-form ref="aaa" :items="items" @submit="hanlderSubmit">
      <div slot="check" slot-scope="vaule" style="margin: 0">
        <p-checkbox-group :options="vaule" v-model="value" />
        <!-- {{ options }} -->
      </div>
    </p-grid-form>
  </p-modal>
</template>

<script>
import pick from 'lodash.pick'
import { FactorAdd } from '../../api'
export default {
  name: 'EditModel',
  data() {
    return {
      value: [],
      ModalText: 'Content of the modal',
      //   visible: false,
      confirmLoading: false
    }
  },
  props: {
    types: { type: Boolean, default: true },
    visible: {
      type: Boolean,
      default: true
    },
    items: {
      type: Array,
      default: () => []
    },
    rule: {
      type: Object,
      default: () => {}
    }
  },
  mounted() {
  },
  methods: {
    setValue(data) {
      this.$nextTick(() => {
        this.$refs.aaa.form.setFieldsValue(pick(data, 'name', 'matchType', 'value'))
      })
    },
    reset() {
      this.$nextTick(() => {
        this.$refs.aaa.form.resetFields()
      })

      //   .form.resetFields()
    },
    hanlderSubmit(evnet, form) {
      evnet.preventDefault()
    },
    async handleOk(e) {
      let that = this
      let a = {
        check: that.value
      }
      let data
      this.$refs.aaa.form.validateFields(async (err, value) => {
        if (!err) {
          data = Object.assign({}, value, a)
          let value1 = { ruleId: that.rule.id, factorId: data.id, value: data.value, matchType: data.matchType }
          this.confirmLoading = true
          if (this.types) {
            let res = await FactorAdd(value1)
            if (res.success) {
              this.confirmLoading = false
              this.$emit('hidemodel', true)
            }
          } else {
            this.confirmLoading = false
            this.$emit('hidemodel', true)
          }
        }
      })

    },
    handleCancel(e) {
      this.$emit('hidemodel', false)
    }
  }
}
</script>

<style></style>
