<template>
  <div class="edit-user-gold-coin-reward-form">
    <el-dialog
      :title="$t('pages.editUserGoldCoinRewardForm.dialog.title')"
      :visible="true"
      width="450px"
      :before-close="handleClose"
      :close-on-click-modal="false"
    >
      <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.editUserGoldCoinRewardForm.form.type')" prop="rewardType">
          <el-select
            v-model="formData.rewardType"
            :placeholder="$t('pages.editUserGoldCoinRewardForm.placeholder.select')"
            style="width: 100%;"
          >
            <el-option
              v-for="item in currencyRewardReasons"
              :key="item.value"
              :label="getRewardTypeLabel(item.value)"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <!-- 金币数量 输入框 -->
        <el-form-item :label="$t('pages.userBaseInfo.labels.gold')" prop="candy">
          <el-input
            v-model.trim="formData.candy"
            :placeholder="$t('pages.editUserGoldCoinRewardForm.placeholder.goldQuantity')"
          />
        </el-form-item>

        <!-- 美金 输入框 -->
        <el-form-item
          v-if="formData.rewardType == 3 || formData.rewardType == 4"
          :label="$t('pages.editUserGoldCoinRewardForm.form.usd')"
          prop="usdCandy"
        >
          <el-input
            v-model.trim="formData.usdCandy"
            :placeholder="$t('pages.editUserGoldCoinRewardForm.placeholder.usdQuantity')"
          />
        </el-form-item>

        <!-- 备注 输入框 -->
        <el-form-item :label="$t('pages.editUserGoldCoinRewardForm.form.remarks')" prop="remarks">
          <el-input
            v-model.trim="formData.remarks"
            :placeholder="$t('pages.editUserGoldCoinRewardForm.placeholder.remarks')"
          />
        </el-form-item>
      </el-form>

      <!-- 操作按钮 -->
      <div slot="footer" class="dialog-footer">
        <!-- 取消按钮 -->
        <el-button @click="handleClose()">
          {{ $t('pages.editUserGoldCoinRewardForm.action.cancel') }}
        </el-button>

        <!-- 提交按钮 -->
        <el-button
          v-loading="listLoading"
          type="primary"
          @click="handleSubmit()"
        >
          {{ $t('pages.editUserGoldCoinRewardForm.action.submit') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { currencyRewardReasons } from "@/constant/type";
import { sendGold } from "@/api/app-user";

export default {
  name: "EditUserGoldCoinDeductionForm",
  props: {
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      listLoading: false,
      currencyRewardReasons,
      rules: {
        candy: [
          {
            required: true,
            message: this.$t("pages.propsSourceGroupForm.validation.requiredField"),
            trigger: "blur"
          },
          {
            pattern: /^\d{1,7}(\.\d{0,2})?$/,
            message: this.$t("pages.editUserGoldCoinRewardForm.validation.goldRange"),
            trigger: "blur"
          }
        ],
        rewardType: [
          {
            required: true,
            message: this.$t("pages.propsSourceGroupForm.validation.requiredField"),
            trigger: "blur"
          }
        ],
        usdCandy: [
          {
            required: true,
            message: this.$t("pages.propsSourceGroupForm.validation.requiredField"),
            trigger: "blur"
          }
        ]
      },
      formData: {
        candy: "",
        remarks: "",
        usdCandy: "",
        rewardType: ""
      }
    };
  },
  methods: {
    getRewardTypeLabel(value) {
      const keyMap = {
        1: "reward",
        2: "internal",
        3: "salary",
        4: "recharge",
        5: "other"
      };
      return this.$t(`pages.editUserGoldCoinRewardForm.rewardType.${keyMap[value]}`);
    },
    handleSubmit() {
      const that = this;
      that.$refs.dataForm.validate(valid => {
        if (!valid) {
          console.error("error submit!!");
          return false;
        }
        that.listLoading = true;
        sendGold({
          userId: that.userId,
          quantity: that.formData.candy,
          usdQuantity: that.formData.usdCandy,
          remarks: that.formData.remarks
            ? `${that.getSeletedCurrencyRewardReasonsName()}/${
                that.formData.remarks
              }`
            : that.getSeletedCurrencyRewardReasonsName(),
          type: that.formData.rewardType
        })
          .then(res => {
            that.$emit("success", Object.assign({}, that.formData));
            that.handleClose();
          })
          .catch(err => {
            that.listLoading = false;
            that.$emit("fial", err);
          });
      });
    },
    getSeletedCurrencyRewardReasonsName() {
      const that = this;
      const list = that.currencyRewardReasons.filter(
        item => item.value === that.formData.rewardType
      );
      return list.length > 0 ? list[0].name : "";
    },
    handleClose() {
      this.$emit("close");
    }
  }
};
</script>
