<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: {
    visible: {
      type: Boolean,
      default: true
    },
    items: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    types() {
      let that = this
      this.$nextTick(() => {
        that.$refs.aaa.form.resetFields()
        that.type = false
      })
    },
    sss() {
      let that = this
      this.$nextTick(() => {
        that.$refs.aaa.form.visibleItem('clz')
        that.$refs.aaa.form.invisibleItem('scriptData')
      })
    },
    sass() {
      let that = this
      this.$nextTick(() => {
        that.$refs.aaa.form.visibleItem('scriptData')
        that.$refs.aaa.form.invisibleItem('clz')
      })
    },
    hanlderSubmit(evnet, form) {
      evnet.preventDefault()
    },
    setvalue(data) {
      let that = this
      this.type = true
      let key = this.items.map(item => item.name)
      this.$nextTick(() => {
        that.$refs.aaa.form.resetFields()
        that.$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>
