<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="formRules"
      size="mini"
      label-suffix=":"
      label-width="150px"
    >
      <el-form-item label="每日签到金额" prop="signPrice">
        <el-input v-model="formData.signPrice" placeholder="请输入每日签到金额" style="width: 200px;">
          <template #append>
            <div>元</div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="连续签到天数" prop="signDay">
        <el-input v-model="formData.signDay" placeholder="请输入连续签到天数" style="width: 200px;">
          <template #append>
            <div>天</div>
          </template>
        </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSubmit">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { saveBeeSignConfig, updateBeeSignConfig, getByIdBeeSignConfig } from '@/api/signInManagement/transactionSignIn'

export default {
  name: 'EditModal',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: ''
      },
      visible: false,
      formData: {
        signConfigId: '',
        signPrice: '',
        signDay: '',
        createTime: ''
      },
      formRules: {
        signPrice: [
          { required: true, message: '请输入每日签到金额' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        signDay: [
          { required: true, message: '请输入连续签到天数' },
          { pattern: /^(0|[1-9][0-9]*)$/, message: '请输入正整数' }
        ]
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    initList() {
    },
    handleOpen(params = {}) {
      this.modalOptions.title = params.signConfigId ? '编辑消费金签到配置' : '添加消费金签到配置'
      this.formData = Object.assign(this.$options.data().formData, params)
      this.visible = true
      this.initList()
      if (params.signConfigId) {
        this.getInfo(params.signConfigId)
      } else {
        this.$refs.formData && this.$refs.formData.resetFields()
      }
    },
    async getInfo(id) {
      const loading = this.$loading({ text: '加载中' })
      try {
        const res = await getByIdBeeSignConfig({ id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          signConfigId: res.data.signConfigId || '',
          signPrice: res.data.signPrice || '',
          signDay: res.data.signDay || '',
          createTime: res.data.createTime || ''
        })
        this.$nextTick(() => {
          this.$refs.formData && this.$refs.formData.validate()
        })
      } finally {
        loading.close()
      }
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({ text: '加载中' })
          try {
            const { ...otps } = this.formData
            const params = {
              ...otps
            }
            this.formData.signConfigId ? await updateBeeSignConfig(params) : await saveBeeSignConfig(params)
            loading.close()
            this.$message({ message: `${this.formData.signConfigId ? '编辑' : '添加'}成功!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>

