<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="SDKAppID"
                label-width="150px"
                prop="tencent_rtc_app_id"
              >
                <el-input
                  v-model="form.tencent_rtc_app_id"
                  clearable
                  :placeholder="__('请输入推流域名')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('请输入密钥')"
                label-width="150px"
                prop="tencent_rtc_secret_key"
              >
                <el-input
                  v-model="form.tencent_rtc_secret_key"
                  clearable
                  :placeholder="__('请输入密钥')"
                  :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: 'Rtc',
    components: {},
    data() {
      return {
        form: {
          tencent_rtc_app_id: undefined,
          tencent_rtc_secret_key: undefined,
        },
        rules: {
          tencent_rtc_app_id: [
            {
              required: true,
              message: '请输入推流域名',
              trigger: 'blur',
            },
          ],
          tencent_rtc_secret_key: [
            {
              required: true,
              message: '请输入密钥',
              trigger: 'blur',
            },
          ],
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      async fetchData() {
        const { data } = await getList()
        this.form = {
          tencent_rtc_app_id: data.tencent_rtc_app_id,
          tencent_rtc_secret_key: data.tencent_rtc_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>
