<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <div>
            <el-form
              ref="form"
              label-width="100px"
              :model="form"
              :rules="rules"
              size="medium"
            >
              <el-form-item
                :label="__('置顶金额')"
                label-width="150px"
                prop="stick_money"
              >
                <el-input
                  v-model="form.stick_money"
                  clearable
                  :placeholder="__('请输入置顶金额')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('置顶天数(天)')"
                label-width="150px"
                prop="stick_day"
              >
                <el-input
                  v-model="form.stick_day"
                  clearable
                  :placeholder="__('请输入置顶天数(天)')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('可置顶人数')"
                label-width="150px"
                prop="stick_num"
              >
                <el-input
                  v-model="form.stick_num"
                  clearable
                  :placeholder="__('请输入可置顶人数')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
            </el-form>
          </div>
          <div>
            <el-button
              class="form-sub-btn"
              icon="el-icon-s-tools"
              type="primary"
              @click="handleEdit"
            >
              {{ __('修改') }}
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doEdit } from '@/api/base/config'

  export default {
    name: 'Message',
    components: {},
    data() {
      return {
        form: {
          stick_money: undefined,
          stick_day: undefined,
          stick_num: undefined,
        },
        rules: {
          stick_money: [
            {
              required: true,
              message: '请输入置顶金额',
              trigger: 'blur',
            },
          ],
          stick_day: [
            {
              required: true,
              message: '请输入置顶天数(天)',
              trigger: 'blur',
            },
          ],
          stick_num: [
            {
              required: true,
              message: '请输入可置顶人数',
              trigger: 'blur',
            },
          ],
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      async fetchData() {
        const { data } = await getList()
        this.form = {
          stick_money: data.stick_money,
          stick_day: data.stick_day,
          stick_num: data.stick_num,
        }
      },
      handleEdit() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit({
              param: JSON.stringify(this.form),
            })

            if (status == 200) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
