<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="请选择">
                <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==='3'">
              <el-checkbox-group v-model="item.VALUE">
                <el-checkbox v-for="optionItem in item.OPTIONS" :label="optionItem.label" :value="optionItem.value"
                             :key="optionItem.label"></el-checkbox>
              </el-checkbox-group>
            </template>
            <template v-else-if="item.INPUT_TYPE==='4'">
              <el-date-picker v-model="item.VALUE" type="year" placeholder="选择年">
              </el-date-picker>
            </template>
            <template v-else-if="item.INPUT_TYPE==='5'">
              <el-date-picker
                v-model="item.VALUE"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </template>
            <template v-else-if="item.INPUT_TYPE==='6'">
              <el-date-picker
                v-model="item.VALUE"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
            </template>
            <template v-else-if="item.INPUT_TYPE==='7'">
              <el-time-select v-model="item.VALUE"
                              :picker-options="{start: item.OPTIONS.start, end: item.OPTIONS.end, step: item.OPTIONS.step}"
                              placeholder="选择时间">
              </el-time-select>
            </template>
            <template v-else-if="item.INPUT_TYPE==='8'">
              <el-switch v-model="item.VALUE">
              </el-switch>
            </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_base',
  mounted () {
    this.getData()
  },
  data () {
    return {
      loading: true,
      submitButtonStat: false,
      configData: null,
    }
  },
  computed: {
    form: function () {
      let result = {}
      for (let i in this.configData) {
        result[i] = this.configData[i].VALUE
      }
      return result
    }
  },
  methods: {
    onSubmit () {
      this.submitButtonStat = true
      console.log(this.form)
      network.postData('config/base', this.form).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.submitButtonStat = false
        this.getData()
      }).catch(response => {
        this.submitButtonStat = false
        this.getData()
      })
    },
    getData () {
      network.getData('config/base').then(response => {
        this.configData = response
        this.loading = false
      })
    },
  }
}
</script>

<style scoped>

</style>