<template>
  <div>
    <el-dialog
      title="添加微信红包奖品"
      :visible.sync="dialogVisible"
      :before-close="close"
      width="500px"
    >
      <el-form
        :model="packetForm"
        :rules="packetRules"
        ref="packetForm"
        label-width="100px"
      >
        <el-form-item label="金额" prop="quantity">
          <el-input
            v-model="packetForm.quantity"
            size="small"
            placeholder="请输入微信红包金额"
            style="width: 200px"
          />
        </el-form-item>
      </el-form>
      <div class="dialog-btn">
        <el-button size="small" @click="close"> 取 消 </el-button>
        <div class="ge"></div>
        <el-button type="primary" size="small" @click="confirmRedPkg">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { publicFileGetUrl } from "@/api/common";
import { accMul, accDiv } from "@/utils/util";
export default{
  name: "RedPkgAdd",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      packetForm: { quantity: "" },
      packetRules: {
        quantity: [
          { required: true, message: "请输入金额", trigger: "blur" },
          {
            pattern:
              /^([1-9]\d*(\.\d{1,2})?|([0](\.([0][1-9]|[1-9]\d{0,1}))))$/,
            message: "请输入合法的金额数字，最多两位小数",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    // 确认输入金额
    confirmRedPkg() {
      let redPacket = {
        prizeType: "red_pkg",
        quantity: 1,
        title: `微信红包 ${this.packetForm.quantity} 元`,
        picUrl: publicFileGetUrl + "static/redPkg.png",
        value: accMul(this.packetForm.quantity, 100),
        sortWeight: 100
      };
      this.$refs["packetForm"].validate((valid) => {
        if (valid) {
          this.$emit("confirmRedPkg", redPacket);
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("close");
    },
  },
};
</script>
<style lang="scss" scoped>
.dialog-btn {
  display: flex;
  align-content: center;
  justify-content: flex-end;
  padding: 40px 0 0;
  .ge {
    width: 40px;
  }
}
</style>
