<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-form ref="form" :model="form" label-width="250px" class="form-page">
        <template v-for="item in configData">
          <el-form-item :label="item.TITLE" :key="item.CONFIG_NAME">
            <template v-if="item.INPUT_TYPE==='2'">
              <el-select v-model="item.VALUE" placeholder="请选择" multiple>
                <el-option v-for="optionItem in item.OPTIONS" :label="optionItem.label" :value="optionItem.value"
                           :key="optionItem.label"></el-option>
              </el-select>
            </template>
            <template v-else-if="item.INPUT_TYPE==='8'">
              <el-switch v-model="item.VALUE">
              </el-switch>
            </template>
            <template v-else-if="item.INPUT_TYPE==='9'">
              <el-input
                  type="textarea"
                  :rows="4"
                  placeholder="请输入内容"
                  v-model="item.VALUE">
              </el-input>
            </template>
            <template v-else>
              <el-input v-model="item.VALUE" placeholder="请输入内容">
                <template v-if="!!item.UNIT" slot="append">{{item.UNIT}}</template>
              </el-input>
            </template>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="submitButtonStat">更新</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

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

export default {
  name: 'config_sms',
  mounted () {
    this.getData()
  },
  data () {
    return {
      configData: null,
      loading: true,
      submitButtonStat: false
    }
  },
  computed: {
    form: function () {
      let result = {}
      for (let i in this.configData) {
        result[i] = this.configData[i].VALUE
      }
      return result
    }
  },
  methods: {
    onSubmit () {
      this.submitButtonStat = true
      let date = this.form.calcYear
      let dateObj = new Date(date)
      network.postData('config/sms', {
        smsOpen: this.form.smsOpen,
        smsFee: this.form.smsFee,
        smsWallet: this.form.smsWallet,
        smsContent: this.form.smsContent,
        smsEmpOpen: this.form.smsEmpOpen,
        smsEmp: this.form.smsEmp,
        smsEmpContent: this.form.smsEmpContent,
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.submitButtonStat = false
        this.getData()
      }).catch(response => {
        this.submitButtonStat = false
        this.getData()
      })
    },
    getData () {
      network.getData('config/sms').then(response => {
        this.configData = response
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>

</style>