<template>
  <el-drawer title="编辑API" :visible.sync="isAddDrawer" size='60%' center append-to-body>
    <mg-form :config="options" class="mb50" :form="form" ref="form" label-position="right">
    </mg-form>

    <div class="fixbottom p20 ">
      <div class="uni-right ">
        <el-button @click="isAddDrawer = false">取 消</el-button>
        <el-button type="primary" @click="sunbmit()">确 定</el-button>
      </div>
    </div>

  </el-drawer>
</template>

<script>
import paramTable from '../paramTable.vue'

import {
  addAPI, //新增API配置
  updateAPI, //修改API配置
} from '../../api/apiconfig.js'
export default {
  props: ['form'],
  data() {
    return {
      isAddDrawer: true, //是否显示添加节点抽屉
      options: [
        {
          label: 'API名称',
          prop: 'name',
          required: true,
          type: 'input',
        },
        {
          label: 'URL',
          prop: 'url',
          required: true,
          type: 'input',
        },
        {
          label: 'appKey',
          prop: 'appKey',
          type: 'input',
        },
        {
          label: '功能类型',
          prop: 'type',
          required: true,
          type: 'select',
          enum: 'API_type'
        },
        {
          label: '请求方式',
          prop: 'method',
          required: true,
          type: 'select',
          enum: 'method'
        },
        // {
        //   label: '入参',
        //   prop: 'param',
        //   dom: multipleInput
        // },
        {
          label: '入参',
          prop: 'param',
          dom: paramTable
        },
        {
          label: '出参',
          prop: 'response',
          dom: paramTable,
        },
      ]
    }
  },

  watch: {
    isAddDrawer() {
      if (!this.isAddDrawer) {
        this.handleClose()
      }
    }
  },
  methods: {
    sunbmit() {
      let form = JSON.parse(JSON.stringify(this.form))
      form.param = JSON.stringify(this.form.param)
      form.response = JSON.stringify(this.form.response)
      if (!form.id) {
        addAPI(this.form).then(res => {
          this.update = true
          this.isAddDrawer = false
        })
      } else {
        updateAPI(form).then(res => {
          this.update = true
          this.isAddDrawer = false
        })
      }

    },
    //关闭添加节点抽屉
    handleClose() {
      if (this.update) {
        this.$emit('closed', this.form)
      } else {
        this.$emit('closed')
      }

    }
  }
}
</script>
