<template>
  <div class="push-form">
    <el-form ref="form" :model="form" label-width="100px">
      <el-alert
        type="info"
        :closable="false"
      >
        <span>{{ $t('pages.inviteUserRule.message.expiredCommission', { days: form.days }) }}</span>
      </el-alert>
      <el-form-item :label="$t('pages.inviteUserRule.form.validDays')" prop="days">
        <el-input v-model="form.days" type="number" />
      </el-form-item>
      <el-alert
        type="info"
        :closable="false"
      >
        <span>{{ $t('pages.inviteUserRule.message.commissionRatio') }}</span>
      </el-alert>
      <el-form-item :label="$t('pages.inviteUserRule.form.proportion')" prop="proportion">
        <el-input v-model="form.proportion" type="number" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{ $t('pages.inviteUserRule.action.edit') }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { updateSysInviteUserConfig, querySysInviteUserConfig } from '@/api/sys'

export default {
  name: 'LotfunInviteUserRule',
  data() {
    return {
      form: {
        days: '',
        proportion: '',
        sysOrigin: 'LOTFUN'
      }
    }
  },
  created() {
    const that = this
    that.renderData()
  },
  methods: {
    renderData() {
      const that = this
      that.listLoading = true
      querySysInviteUserConfig('LOTFUN').then(res => {
        const { body } = res
        that.form = body
        that.listLoading = false
      })
    },
    onSubmit() {
      const that = this
      that.$refs.form.validate((valid) => {
        if (!valid) {
          console.error('error submit!!')
          return false
        }
        that.$confirm(that.$t('pages.inviteUserRule.confirm.update'), that.$t('pages.inviteUserRule.confirm.title'), {
          confirmButtonText: that.$t('pages.inviteUserRule.confirm.confirm'),
          cancelButtonText: that.$t('pages.inviteUserRule.confirm.cancel'),
          type: 'warning'
        }).then(() => {
          updateSysInviteUserConfig(that.form).then(res => {
            that.$message({
              type: 'success',
              message: that.$t('pages.inviteUserRule.message.success')
            })
            that.renderData()
          }).catch(er => {
            that.$opsMessage.success()
          })
        }).catch(() => {
          that.$message({
            type: 'info',
            message: that.$t('pages.inviteUserRule.message.cancelled')
          })
        })
      })
    }
  }
}
</script>
<style scoped lang="scss">
  .msg-text {
    color: #54c988;
    background-color: #f6f5f5;
  }
</style>
