<template>
  <div class="edit-user-game-coupon-reward-form">
    <el-dialog
      :title="$t('pages.editUserGameCouponRewardForm.dialog.title')"
      :visible="true"
      width="450px"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :modal-append-to-body="true"
      :append-to-body="true"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="formData"
        label-position="left"
        label-width="70px"
        style="width: 300px; margin-left:50px;"
      >

        <el-form-item :label="$t('pages.editUserGameCouponRewardForm.form.type')" prop="rewardType">
          <el-select v-model="formData.rewardType" :placeholder="$t('pages.editUserGameCouponRewardForm.placeholder.select')">
            <el-option
              v-for="item in currencyRewardReasons"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('pages.editUserGameCouponRewardForm.form.gameCoupon')" prop="coupon">
          <el-input
            v-model.trim="formData.coupon"
            :placeholder="$t('pages.editUserGameCouponRewardForm.placeholder.gameCouponQuantity')"
          />
        </el-form-item>

        <el-form-item :label="$t('pages.editUserGameCouponRewardForm.form.remarks')" prop="remarks">
          <el-input
            v-model.trim="formData.remarks"
            :placeholder="$t('pages.editUserGameCouponRewardForm.placeholder.remarks')"
          />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">
          {{ $t('pages.editUserGameCouponRewardForm.action.cancel') }}
        </el-button>
        <el-button
          v-loading="listLoading"
          type="primary"
          @click="handleSubmit()"
        >
          {{ $t('pages.editUserGameCouponRewardForm.action.submit') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { currencyRewardReasons } from '@/constant/type'
import { rewardGameCoupon } from '@/api/app-user'

export default {
  name: 'EditUserGameCouponRewardForm',
  props: {
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      listLoading: false,
      currencyRewardReasons,
      rules: {
        coupon: [
          {
            required: true,
            message: this.$t('pages.editUserGameCouponRewardForm.validation.enterAmount'),
            trigger: 'blur'
          },
          {
            pattern: /^\d{1,7}(\.\d{0,2})?$/,
            message: this.$t('pages.editUserGameCouponRewardForm.validation.amountRange'),
            trigger: 'blur'
          }
        ],
        rewardType: [
          {
            required: true,
            message: this.$t('pages.editUserGameCouponRewardForm.validation.selectType'),
            trigger: 'blur'
          }
        ]
      },
      formData: {
        coupon: '',
        rewardType: '',
        remarks: ''
      }
    }
  },
  methods: {
    handleSubmit() {
      const that = this
      that.$refs.dataForm.validate((valid) => {
        if (!valid) {
          console.error('error submit!!')
          return false
        }
        that.listLoading = true
        rewardGameCoupon(that.userId, that.formData.coupon, that.formData.remarks, that.formData.rewardType).then(res => {
          that.$emit('success', Object.assign({}, that.formData))
          that.handleClose()
        }).catch(err => {
          that.listLoading = false
          that.$emit('fial', err)
          that.handleClose()
        })
      })
    },
    handleClose() {
      this.$emit('close')
    }
  }
}
</script>
