<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-form ref="form" :model="form" label-width="250px" class="form-page">
        <el-form-item label="是否自动提现">
          <el-switch v-model="form.isAutoWithdraw" @change="handleAutoWithdraw"></el-switch>
        </el-form-item>
        <el-form-item label="是否开启复销短信通知">
          <el-switch v-model="form.allowReconsumeSms" @change="handleAllowReconsumeSms"></el-switch>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import network from '@/utils/network'

export default {
  name: 'config_base',
  mounted () {
    this.getData()
  },
  data () {
    return {
      loading: true,
      smsFee: 0,
      form: {
        isAutoWithdraw: 0,
        allowReconsumeSms: 0,
      },
    }
  },
  methods: {
    handleAutoWithdraw(){
      network.postData('config/auto-withdraw', {isAutoWithdraw:this.form.isAutoWithdraw}).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.getData()
      }).catch(response => {
        this.getData()
      })
    },
    handleAllowReconsumeSms(val){
      let title = '确定关闭复销短信提醒？'
      if(val===true){
        title=`开启复销短信提醒，每月扣除${this.smsFee}元。是否继续？`
      }
      this.$confirm(title, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$message({
          message: '修改中',
          type: 'info'
        })
        network.postData('config/allow-reconsume-sms', {allowReconsumeSms:this.form.allowReconsumeSms}).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.getData()
        }).catch(response => {
          this.getData()
        })
      }).catch(() => {
        this.getData()
      });
    },
    getData () {
      network.getData('config/base').then(response => {
        this.form.isAutoWithdraw = response.IS_AUTO_WITHDRAW
        this.form.allowReconsumeSms = response.ALLOW_RECONSUME_SMS
        this.smsFee = response.smsFee
        this.loading = false
      })
    },
  }
}
</script>

<style scoped>

</style>