<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ __('百度') }}</span>
          </div>
          <div>
            <el-form
              ref="form"
              label-width="100px"
              :model="form"
              :rules="rules"
              size="medium"
            >
              <el-form-item
                label="APP ID"
                label-width="150px"
                prop="baidu_app_id"
              >
                <el-input
                  v-model="form.baidu_app_id"
                  clearable
                  :placeholder="__('请输入百度 APP ID')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                label="AppKey"
                label-width="150px"
                prop="baidu_secret_id"
              >
                <el-input
                  v-model="form.baidu_secret_id"
                  clearable
                  :placeholder="__('请输入百度 AppKey')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                label="secretKey"
                label-width="150px"
                prop="baidu_secret_key"
              >
                <el-input
                  v-model="form.baidu_secret_key"
                  clearable
                  :placeholder="__('请输入百度 secretKey')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
            </el-form>
          </div>
          <div>
            <el-button
              class="form-sub-btn"
              icon="el-icon-s-tools"
              type="primary"
              @click="handleEdit"
            >
              {{ __('修改') }}
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doEdit } from '@/api/base/config'

  export default {
    name: 'Ai',
    components: {},
    data() {
      return {
        form: {
          baidu_app_id: undefined,
          baidu_secret_id: undefined,
          baidu_secret_key: undefined,
        },
        rules: {
          baidu_app_id: [
            {
              required: true,
              message: this.__('请输入百度 APP ID'),
              trigger: 'blur',
            },
          ],
          baidu_secret_id: [
            {
              required: true,
              message: this.__('请输入百度 AppKey'),
              trigger: 'blur',
            },
          ],
          baidu_secret_key: [
            {
              required: true,
              message: this.__('请输入百度 secretKey'),
              trigger: 'blur',
            },
          ],
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      async fetchData() {
        const { data } = await getList()
        this.form = {
          baidu_app_id: data.baidu_app_id,
          baidu_secret_id: data.baidu_secret_id,
          baidu_secret_key: data.baidu_secret_key,
        }
      },
      handleEdit() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit({
              param: JSON.stringify(this.form),
            })

            if (status == 200) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
