<template>
  <!-- 新增策略 / 编辑策略 -->
  <p-modal
    :title="title"
    width="50%"
    :visible="visible"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <p-grid-form ref="aaa" :span="8" :label-col="{ span: 6 }" :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: [],
      ModalText: 'Content of the modal',
      //   visible: false,
      confirmLoading: false
    }
  },
  props: {
    strategyType: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: '编辑策略'
    },
    visible: {
      type: Boolean,
      default: true
    },
    items: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    hanlderSubmit(evnet, form) {
      evnet.preventDefault()
    },
    resetFields() {
      this.$nextTick(() => {
        this.$refs.aaa.form.resetFields()
      })
    },
    setvalue(value) {
      let that = this
      this.$nextTick(() => {
        that.$refs.aaa.form.setFieldsValue(
          pick(
            value,
            'category',
            'name',
            'id',
            'cargoOwnerName',
            'cargoOwnerId',
            'warehouseName',
            'warehouseId',
            'warehouseCode'
          )
        )
      })
    },
    async handleOk(e) {
      let that = this
      let data = Object.assign({}, this.$refs.aaa.form.getFieldsValue())
      if (this.title == '新增策略') {
        let res = await addItem(data)
        this.confirmLoading = true
        if (res.success) {
          setTimeout(() => {
            this.confirmLoading = false
            this.$emit('hidemodel', true)
            this.$emit('reload')
          }, 200)
        } else {
          that.$exception(res)
        }
      } else {
        let res = await updateItem(data)
        this.confirmLoading = true
        if (res.success) {
          setTimeout(() => {
            this.confirmLoading = false
            this.$emit('hidemodel', true)
            this.$emit('reload')
          }, 200)
        } else {
          that.$exception(res)
        }
      }
    },
    handleCancel(e) {
      this.$emit('hidemodel', false)
    }
  }
}
</script>

<style></style>
