<template>
  <el-dialog
    :title="type==='edit'?'编辑':'添加'"
    :visible.sync="visible"
    width="30%"
    @close="handleClose">
    <el-form
      style="padding:20px"
      inline
      :model="form"
      ref="form"
      label-width="100px">
      <el-form-item
        verify
        class="is-required"
        label="名称"
        prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item
        v-if="type === 'edit'"
        label="状态"
        prop="status">
        <el-select v-model="form.status">
          <el-option label="激活" :value="1"></el-option>
          <el-option label="禁用" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付参数" prop="payParam" :error="payParamError">
        <el-input
          v-model="form.payParam"
          type="textarea"
          :rows="5"
          placeholder="请输入JSON格式字符串，如：{&quot;key&quot;: &quot;value&quot;}"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="visible= false">取 消</el-button>
      <el-button
        type="primary"
        @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import { updatePayChannel, addPayChannel } from '@/api/payChannel'
const defaultForm = {
  name: '',
  payParam: ''
}
export default {
  computed: {
    payParamError () {
      if (!this.form.payParam) return ''
      try {
        JSON.parse(this.form.payParam)
        return ''
      } catch (e) {
        try {
          // 兼容 key 无引号的 JS 对象字面量写法，先补双引号再解析
          const fixed = this.form.payParam.replace(/(['"])?([a-zA-Z_\u4e00-\u9fa5]\w*)\1\s*:/g, '"$2":')
          JSON.parse(fixed)
          return ''
        } catch (e2) {
          return 'JSON格式不正确，请检查'
        }
      }
    }
  },
  data () {
    return {
      type: '',
      visible: false,
      form: {
        ...defaultForm
      }
    }
  },
  methods: {
    show (type, data) {
      this.type = type
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (type === 'edit' && data) {
          this.form = Object.assign({}, this.form, JSON.parse(JSON.stringify(data)))
        } else {
          this.form = JSON.parse(JSON.stringify(defaultForm))
        }
      })
    },
    handleClose () {
      this.visible = false
    },
    handleSubmit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.payParam) {
            try {
              JSON.parse(this.form.payParam)
            } catch (e) {
              try {
                const fixed = this.form.payParam.replace(/(['"])?([a-zA-Z_\u4e00-\u9fa5]\w*)\1\s*:/g, '"$2":')
                JSON.parse(fixed)
              } catch (e2) {
                this.$message.error('支付参数必须是有效的JSON格式')
                return
              }
            }
          }
          if (this.type === 'edit') {
            updatePayChannel(this.form).then(res => {
              this.$message.success('操作成功')
              this.handleClose()
              this.$emit('update')
            })
          } else {
            addPayChannel(this.form).then(res => {
              this.$message.success('操作成功')
              this.handleClose()
              this.$emit('update')
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
