// 添加/修改商户
<template>
  <el-dialog
    :title="isEdit ? '编辑商户' : '添加商户'"
    width="575px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
    :center="true"
  >
    <el-form ref="form" :rules="rules" :model="form" label-position="left" label-width="120px">
      <el-form-item prop="id" style="margin-bottom: 0;">&nbsp;</el-form-item>
      <el-form-item label="名称" prop="name" required>
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="设备appId" prop="machineAppId" required>
        <el-input v-model="form.machineAppId" />
      </el-form-item>
      <el-form-item label="	商户appId" prop="serviceAppId" required>
        <el-input v-model="form.serviceAppId" />
      </el-form-item>
      <el-form-item label="商户平台地址" prop="serviceAddress" required>
        <el-input v-model="form.serviceAddress" />
      </el-form-item>
      <el-form-item label="是否三方应用" prop="thirdAuth" required>
        <el-radio-group v-model="form.thirdAuth">
          <el-radio :label="false">否</el-radio>
          <el-radio :label="true">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="form.thirdAuth" label="三方授权token" prop="appAuthToken" required>
        <el-input v-model="form.appAuthToken" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button type="primary" :loading="submitLoading" @click="submit">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add } from '@/api/merchant'

export default {
  data() {
    return {
      form: {
        id: null,
        machineAppId: null,
        name: null,
        serviceAddress: null,
        serviceAppId: null,
        thirdAuth: false,
        appAuthToken: null
      },
      rules: {
        machineAppId: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        name: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        serviceAddress: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        serviceAppId: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        appAuthToken: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ]
      },
      visible: false,
      submitLoading: false,
      isEdit: false
    }
  },
  methods: {
    open(obj) {
      this.visible = true
      this.submitLoading = false
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (obj && Object.keys(obj).length > 0) {
          this.form = obj
          this.isEdit = true
        } else {
          this.isEdit = false
        }
      })
    },
    submit(e) {
      e.preventDefault()
      this.$refs.form.validate(async(valid) => {
        if (!valid) {
          return false
        }
        if (this.submitLoading) {
          return
        }
        this.submitLoading = true
        try {
          if (!this.form.thirdAuth) {
            this.form.appAuthToken = null
          }
          await add(this.form)
          this.visible = false
          this.$emit('success', this.isEdit)
        } catch (e) {
          console.info('add', e)
        } finally {
          this.submitLoading = false
        }
      })
    }
  }
}
</script>
