<template>
  <div>
    <el-dialog :title="exData.id ? '编辑': '新增'" :visible.sync="dialog" width="450px" :close-on-click-modal="false"
               top="50px">
      <el-form ref="form" :model="form" :rules="formRules" label-width="100px" class="bind-phone">

        <el-form-item label="方法路径：" prop="method_path" :rules="formRules.required">
          <el-input v-model="form.method_path" placeholder="请输入方法路径"></el-input>
        </el-form-item>
        <el-form-item label="方法描述：" prop="method_desc" :rules="formRules.required">
          <el-input v-model="form.method_desc" placeholder="请输入方法描述"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer text-center">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="danger" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Boolean,
      default: true
    },
    exData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  components: {
    // upload,
  },
  data () {
    return {
      dialog: !!this.value,
      form: {
        method_path: '',
        method_desc: ''
      }
    }
  },
  methods: {
    async handleSubmit () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let url = this.exData.id ? '/method/edit' : '/method/add'
          const data = await this.$fetch('/api/auth' + url, { ...this.form })
          if (data.code === 200) {
            this.$message.success('提交成功')
            this.$emit('success')
            this.dialog = false
          }
        }
      })
    }
  },
  mounted () {
    if (this.exData.id) {
      this.form.id = this.exData.id
      for (const key in this.form) {
        this.form[key] = this.exData[key]
      }
    } else {
      this.form.method_project_id = this.exData.method_project_id
    }
  },
  watch: {
    dialog (val) {
      if (!val) this.$emit('input', val)
    }
  }
}
</script>

<style lang="scss" scoped>
.top-tip {
  margin-top: -20px;
  margin-bottom: 20px;
}
</style>
