<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :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="points_kind_id">
        <el-radio-group v-model="form.points_kind_id" size="medium">
          <el-radio
            v-for="(item, index) in points_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="points_log_points">
        <el-input
          v-model.number="form.points_log_points"
          clearable
          :placeholder="__('请输入积分值')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('描述')" prop="points_log_desc">
        <el-input
          v-model="form.points_log_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 { doAdd } from '@/api/user/resource'

  export default {
    name: 'UserPointsHistoryEdit',
    data() {
      return {
        form: {
          points_kind_id: 1,
        },
        rules: {
          points_kind_id: [
            {
              required: true,
              message: '类型不能为空',
              trigger: 'change',
            },
          ],
          user_id: [
            {
              required: true,
              message: '请输入会员编号',
              trigger: 'blur',
            },
          ],
          points_log_points: [
            {
              required: true,
              message: '请输入积分值',
              trigger: 'blur',
            },
          ],
          points_log_desc: [
            {
              required: true,
              message: '请输入描述',
              trigger: 'blur',
            },
          ],
        },
        points_kind_idOptions: [
          {
            label: this.__('增加'),
            value: 1,
          },
          {
            label: this.__('减少'),
            value: 2,
          },
        ],
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = {
            points_log_id: row.points_log_id,
            points_kind_id: row.points_kind_id,
            user_id: row.user_id,
            points_log_points: row.points_log_points,
            points_log_desc: row.points_log_desc,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = { points_kind_id: 1 }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doAdd(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
