<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('类型')" prop="money_kind_id">
        <el-radio-group v-model="form.money_kind_id" size="medium">
          <el-radio
            v-for="(item, index) in money_kind_idOptions"
            :key="index"
            :disabled="item.disabled"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <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="record_money">
        <el-input
          v-model="form.record_money"
          clearable
          :placeholder="__('请输入余额')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('描述')" prop="record_desc">
        <el-input
          v-model="form.record_desc"
          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/consume/record'

  export default {
    name: 'ConsumeRecordEdit',
    data() {
      return {
        form: {
          money_kind_id: 1,
        },
        rules: {
          money_kind_id: [
            {
              required: true,
              message: this.__('类型不能为空'),
              trigger: 'change',
            },
          ],
          user_id: [
            {
              required: true,
              message: this.__('请输入会员编号'),
              trigger: 'blur',
            },
          ],
          record_money: [
            {
              required: true,
              message: this.__('请输入余额'),
              trigger: 'blur',
            },
          ],
          record_desc: [
            {
              required: true,
              message: this.__('请输入描述'),
              trigger: 'blur',
            },
          ],
        },
        money_kind_idOptions: [
          {
            label: this.__('增加'),
            value: 1,
          },
          {
            label: this.__('减少'),
            value: 2,
          },
        ],
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit() {
        this.title = this.__('添加')
        this.dialogFormVisible = true
      },
      close() {
        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>
