<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-form ref="form" :model="form" label-width="250px" :label-position="labelPosition">
        <template v-for="item in configData">
          <div v-if="item.SORT==='1'" class="hr-tip"><span>{{ $t('config.memberAbout') }}</span></div>
          <div v-if="item.SORT==='13'" class="hr-tip"><span>{{ $t('config.homeAbout') }}</span></div>
          <div v-if="item.SORT==='16'" class="hr-tip"><span>{{ $t('config.withdrawalAbout') }}</span></div>
          <div v-if="item.SORT==='41'" class="hr-tip"><span>{{ $t('config.otherAbout') }}</span></div>
          <el-form-item :key="item.CONFIG_NAME" :label="item.TITLE">
            <template v-if="(item.INPUT_TYPE).toString()==='2'">
              <el-select v-model="item.VALUE" placeholder="请选择">
                <el-option v-for="optionItem in item.OPTIONS" :key="optionItem.label" :label="optionItem.label" :value="optionItem.value" />
              </el-select>
            </template>
            <template v-else-if="(item.INPUT_TYPE).toString()==='3'">
              <el-checkbox-group v-model="item.VALUE">
                <el-checkbox v-for="optionItem in item.OPTIONS" :key="optionItem.label" :label="optionItem.label" :value="optionItem.value" />
              </el-checkbox-group>
            </template>
            <template v-else-if="(item.INPUT_TYPE).toString()==='4'">
              <el-date-picker v-model="item.VALUE" type="year" placeholder="选择年" />
            </template>
            <template v-else-if="(item.INPUT_TYPE).toString()==='5'">
              <el-date-picker v-model="item.VALUE" type="datetime" placeholder="Select date" />
            </template>
            <template v-else-if="(item.INPUT_TYPE).toString()==='6'">
              <el-date-picker v-model="item.VALUE" type="date" placeholder="Select date" />
            </template>
            <template v-else-if="(item.INPUT_TYPE).toString()==='7'">
              <el-time-select v-model="item.VALUE" :picker-options="{start: item.OPTIONS.start, end: item.OPTIONS.end, step: item.OPTIONS.step}" placeholder="选择时间" />
            </template>
            <template v-else-if="(item.INPUT_TYPE).toString()==='8'">
              <el-switch v-model="item.VALUE" />
            </template>
            <template v-else-if="(item.INPUT_TYPE).toString()==='9'">
              <el-input
                v-model="item.VALUE"
                type="textarea"
                :rows="4"
                placeholder="请输入内容"
              />
            </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" :loading="submitButtonStat" @click="onSubmit">{{ $t('table.confirm') }}</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

import { getScreenWidth } from '@/utils'
import { fetchOtherConfig, updateOtherConfig } from '@/api/config'

export default {
  name: 'OtherConfig',
  data() {
    return {
      loading: true,
      submitButtonStat: false,
      configData: null,
      screenWidth: getScreenWidth() > 500 ? '500px' : getScreenWidth() + 'px',
      labelPosition: getScreenWidth() >= 500 ? 'right' : 'top'
    }
  },
  computed: {
    form: function() {
      const result = {}
      for (const i in this.configData) {
        result[i] = this.configData[i].VALUE
      }
      return result
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.loading = true
      fetchOtherConfig().then(response => {
        this.configData = response.data

        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)
      })
    },
    onSubmit() {
      this.loading = true
      updateOtherConfig(this.form).then(response => {
        this.$message({
          message: response.data,
          type: 'success'
        })

        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)

        this.fetchData()
      }).catch(error => {
        this.$message({
          message: error,
          type: 'warning'
        })

        this.loading = false
        this.fetchData()
      })
    }
  }
}
</script>

<style scoped>
.app-main {
	padding: 15px;
}
.app-container {
	padding: 0;
}
.white-box {
	padding: 15px;
}
.form-page {
	width: 100%;
}
  .hr-tip {
    font-size: 12px;
    position: relative;
    text-align: center;
    height: 30px;
    line-height: 30px;
    color: #999;
    margin-bottom: 20px;
  }

  .hr-tip:before {
    content: '';
    display: block;
    position: absolute;
    left: 0;
    right: 0;
    top: 14px;
    border-bottom: 1px dashed #ddd;
    height: 1px;
  }

  .hr-tip span {
    display: inline-block;
    background: #fff;
    position: relative;
    padding: 0 10px;
  }
</style>
