<template>
  <div>
    <el-row gutter="24">
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ __('多语言翻译设置') }}</span>
          </div>
          <div>
            <el-form
              ref="autoForm"
              label-width="100px"
              :model="autoForm"
              :rules="autoRules"
              size="medium"
            >
              <el-form-item
                :label="__('启用多语言翻译设置')"
                label-width="150px"
                prop="auto_translate_enable"
              >
                <el-radio-group
                  v-model="autoForm.auto_translate_enable"
                  size="medium"
                >
                  <el-radio
                    v-for="(item, index) in auto_translate_enableOptions"
                    :key="index"
                    :disabled="item.disabled"
                    :label="__(item.value)"
                  >
                    {{ __(item.label) }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item
                :label="__('翻译渠道')"
                label-width="150px"
                prop="translate_channel"
              >
                <el-radio-group
                  v-model="autoForm.translate_channel"
                  size="medium"
                >
                  <el-radio
                    v-for="(item, index) in translate_channelOptions"
                    :key="index"
                    :disabled="item.disabled"
                    :label="item.value"
                  >
                    {{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-form>
          </div>
          <div>
            <el-button class="form-sub-btn" icon="el-icon-s-tools" type="primary" @click="autoEdit">
              {{ __('修改') }}
            </el-button>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ __('百度翻译') }}</span>
          </div>
          <div>
            <el-form
              ref="baiduForm"
              label-width="100px"
              :model="baiduForm"
              :rules="baiduRules"
              size="medium"
            >
              <el-form-item label="App Id" prop="baidu_translate_app_id">
                <el-input
                  v-model="baiduForm.baidu_translate_app_id"
                  clearable
                  :placeholder="__('请输入百度翻译 App Id')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item :label="__('密钥')" prop="baidu_translate_app_key">
                <el-input
                  v-model="baiduForm.baidu_translate_app_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="baiduEdit">
              {{ __('修改') }}
            </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: 'Translate',
    components: {},
    data() {
      return {
        autoForm: {
          auto_translate_enable: undefined,
          translate_channel: undefined,
        },
        autoRules: {
          auto_translate_enable: [
            {
              required: true,
              message: this.__('启用多语言翻译设置不能为空'),
              trigger: 'change',
            },
          ],
          translate_channel: [
            {
              required: true,
              message: this.__('翻译渠道不能为空'),
              trigger: 'change',
            },
          ],
        },
        auto_translate_enableOptions: [
          {
            label: this.__('是'),
            value: '1',
          },
          {
            label: this.__('否'),
            value: '2',
          },
        ],
        translate_channelOptions: [
          {
            label: this.__('百度'),
            value: '1',
          },
        ],
        baiduForm: {
          baidu_translate_app_id: undefined,
          baidu_translate_app_key: undefined,
        },
        baiduRules: {
          baidu_translate_app_id: [
            {
              required: true,
              message: this.__('请输入百度翻译 App Id'),
              trigger: 'blur',
            },
          ],
          baidu_translate_app_key: [
            {
              required: true,
              message: this.__('请输入百度翻译 密钥'),
              trigger: 'blur',
            },
          ],
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      async fetchData() {
        const { data } = await getList()
        this.autoFormInit(data)
        this.baiduFormInit(data)
      },
      autoFormInit(data) {
        this.autoForm = {
          auto_translate_enable: data.auto_translate_enable,
          translate_channel: data.translate_channel,
        }
      },
      baiduFormInit(data) {
        this.baiduForm = {
          baidu_translate_app_id: data.baidu_translate_app_id,
          baidu_translate_app_key: data.baidu_translate_app_key,
        }
      },
      autoEdit() {
        this.$refs['autoForm'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit({
              param: JSON.stringify(this.autoForm),
            })

            if (status == 200) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
          } else {
            return false
          }
        })
      },
      baiduEdit() {
        this.$refs['baiduForm'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit({
              param: JSON.stringify(this.baiduForm),
            })

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