<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="接口类型">{{apiType}}</el-form-item>
        <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="Select date">
              </el-date-picker>
            </template>
            <template v-else-if="item.INPUT_TYPE==='6'">
              <el-date-picker
                v-model="item.VALUE"
                type="date"
                placeholder="Select date">
              </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_api-edit',
  mounted () {
    this.getData()
  },
  data () {
    return {
      loading: true,
      submitButtonStat: false,
      configData: null,
      apiType: '',
    }
  },
  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 requestPath = ``
      if(this.$route.params.type === 'ocr'){
        requestPath = `config/ocr-api-edit/${this.$route.params.id}`
      } else if(this.$route.params.type === 'sms') {
        requestPath = `config/sms-api-edit/${this.$route.params.id}`
      }
      network.postData(requestPath, {config: this.form}).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.submitButtonStat = false
        this.getData()
        this.$router.go(-1)
      }).catch(response => {
        this.submitButtonStat = false
        this.getData()
      })
    },
    getData () {
      if(this.$route.params.type === 'ocr'){
        network.getData(`config/ocr-api-edit/${this.$route.params.id}`).then(response => {
          this.configData = response.CONFIGS
          this.apiType = response.apiType
          this.loading = false
        })
      } else if(this.$route.params.type === 'sms') {
        network.getData(`config/sms-api-edit/${this.$route.params.id}`).then(response => {
          this.configData = response.CONFIGS
          this.apiType = response.apiType
          this.loading = false
        })
      } else {
        this.loading = false
      }

    },
  }
}
</script>

<style scoped>

</style>
