<template>
  <div>
    <el-form
      inline
      :model="form"
      :rules="rules"
      ref="form"
      class="form-wrap dialog-info"
      label-width="100px">

      <el-form-item
        label="分销商描述"
        prop="otaSourceDesc">
        <el-input v-model="form.otaSourceDesc" />
      </el-form-item>
      <el-form-item
        label="分销商参数"
        prop="param">
        <el-input
          v-model="form.param" />
      </el-form-item>
      <el-form-item
        label="下单回调地址"
        prop="createCallbackUrl">
        <el-input
          v-model="form.createCallbackUrl" />
      </el-form-item>
      <el-form-item
        label="核销回调地址"
        prop="checkCallbackUrl">
        <el-input
          v-model="form.checkCallbackUrl" />
      </el-form-item>
      <el-form-item
        label="退单回调地址"
        prop="cancelCallbackUrl">
        <el-input
          v-model="form.cancelCallbackUrl" />
      </el-form-item>
    </el-form>
    <div class="dialog-btn-wrap">
      <el-button @click="handleClose">关闭</el-button>
      <el-button
        @click="submit"
        type="primary">保存</el-button>
    </div>
  </div>
</template>

<script>
import { updateOtaSource } from '@/api/platform'

export default {
  props: {
    currentItem: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      visible: false,
      form: {
        id: '',
        otaSourceDesc: '',
        otaSourceSecret: '',
        createCallbackUrl: '',
        checkCallbackUrl: '',
        cancelCallbackUrl: '',
        param: ''
      },
      rules: {}
    }
  },
  methods: {
    handleClose () {
      this.$parent.handleClose()
    },
    reset () {
      this.$refs.form.resetFields()
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.updateOtaSource()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    updateOtaSource () {
      updateOtaSource(this.form).then(res => {
        this.$message.success('保存成功')
        this.reset()
        this.handleClose()
        this.$emit('updateList')
      })
    }
  },
  watch: {
    visible (val) {
      if (!val) return
      // 重置表单
      this.reset()
      // 初始化编辑数据
      if (this.currentItem) {
        const { id, otaSourceDesc, createCallbackUrl, checkCallbackUrl, cancelCallbackUrl, param } = this.currentItem
        this.form = {
          id,
          otaSourceDesc,
          createCallbackUrl,
          checkCallbackUrl,
          cancelCallbackUrl,
          param: param || ''
        }
      }
    }
  }
}
</script>

<style scoped>
.btn-wrap{
  padding-right: 20px;
}
</style>
