<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('用户编号')" prop="user_id">
        <el-input
          v-model="form.user_id"
          clearable
          :placeholder="__('请输入用户编号')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('用户资金')" prop="user_money">
        <el-input
          v-model="form.user_money"
          clearable
          :placeholder="__('用户资金 - 正数为增加余额，负数为扣减余额')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('充值卡余额')" prop="user_recharge_card">
        <el-input
          v-model="form.user_recharge_card"
          clearable
          placeholder="正数为增加充值卡余额，负数为扣减充值卡余额"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('积分')" prop="user_points">
        <el-input
          v-model="form.user_points"
          clearable
          :placeholder="__('积分 - 正数为增加积分，负数为扣减积分积分')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { doEdit } from '@/api/user/resource'

  export default {
    name: 'UserResourceEdit',
    data() {
      return {
        form: {},
        rules: {
          user_id: [
            {
              required: true,
              message: '请输入用户编号',
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form.user_id = row.user_id
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {}
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
