<template>
  <p-modal title="规则因子" :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 { addItem, updateItem } from './api'
export default {
  name: 'EditModel',
  data() {
    return {
      value: [],
      type: true,
      confirmLoading: false
    }
  },
  props: {
    strategyType: {
      type: String,
      default: 'InStock'
    },
    visible: {
      type: Boolean,
      default: true
    },
    items: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    types() {
      this.$nextTick(() => {
        this.$refs.aaa.form.resetFields()
        this.type = false
      })
    },
    hanlderSubmit(evnet, form) {
      evnet.preventDefault()
    },
    setvalue(data) {
      this.type = true
      let key = this.items.map(item => item.name)
      this.$nextTick(() => {
        this.$refs.aaa.form.resetFields()
        this.$refs.aaa.form.setFieldsValue(pick(data, ...key))
      })
    },
    handleOk(e) {
      let that = this
      this.$refs.aaa.form.validateFields(async (err, values) => {
        if (!err) {
          that.confirmLoading = true
          if (that.type) {
            let res = await updateItem(values)
            if (res.success) {
              setTimeout(() => {
                that.$emit('hidemodel')
                that.confirmLoading = false
              }, 200)
            }
          } else {
            let res = await addItem(values)
            if (res.success) {
              setTimeout(() => {
                that.$emit('hidemodel')
                that.confirmLoading = false
              }, 200)
            }
          }
        }
      })
    },
    handleCancel(e) {
      this.$emit('hidemodel', true)
    }
  }
}
</script>

<style></style>
