<template>
  <div class="app-container">
    <!-- <el-card class="box-card"> -->
    <el-tabs v-model="activeTab" type="border-card">
      <el-tab-pane label="翻译配置" name="translation" lazy>
        <el-form
          ref="translationFormRef"
          :model="translationSettingData"
          :rules="translationRules"
          label-width="120px"
          style="max-width: 600px; margin-top: 20px"
        >
          <el-form-item label="翻译类型" prop="type">
            <el-select v-model="translationSettingData.type" placeholder="请选择翻译类型">
              <el-option label="默认配置" value="0"></el-option>
              <el-option label="百度翻译" value="1"></el-option>
              <!-- <el-option label="Google翻译" value="2"></el-option> -->
            </el-select>
          </el-form-item>
          <el-form-item v-if="translationSettingData.type === '1'" label="APP ID" prop="appId">
            <el-input v-model="translationSettingData.appId" placeholder="请输入APP ID"></el-input>
          </el-form-item>
          <el-form-item v-if="translationSettingData.type === '1'" label="密钥" prop="secretKey">
            <el-input v-model="translationSettingData.secretKey" :rows="4" placeholder="请输入密钥"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitTranslationForm">提交</el-button>
            <el-button @click="resetTranslationForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <!-- <el-tab-pane label="平台配置" name="platform" lazy>
        <el-form
          ref="platformFormRef"
          :model="platformSettingData"
          :rules="platformRules"
          label-width="120px"
          style="max-width: 600px; margin-top: 20px"
        >
          <el-form-item label="平台名称" prop="name">
            <el-input v-model="platformSettingData.name" placeholder="请输入平台名称"></el-input>
          </el-form-item>
          <el-form-item label="平台域名" prop="domain">
            <el-input v-model="platformSettingData.domain" placeholder="请输入平台域名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitPlatformForm">提交</el-button>
            <el-button @click="resetPlatformForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane> -->
    </el-tabs>
    <!-- </el-card> -->
  </div>
</template>

<script setup name="Setting" lang="ts">
import { listSetting, updateSetting } from '@/api/im/setting';
import { getCurrentInstance, ref, onMounted, ComponentInternalInstance } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const buttonLoading = ref(false);
const loading = ref(true);
const activeTab = ref('translation');

const translationSettingData = ref({
  type: '',
  appId: '',
  secretKey: ''
});

const platformSettingData = ref({
  name: '',
  domain: ''
});

// 翻译配置验证规则
const translationRules = {
  type: [{ required: true, message: '请选择翻译类型', trigger: 'change' }],
  appId: [
    { required: true, message: '请输入APP ID', trigger: 'blur' },
    { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
  ],
  secretKey: [
    { required: true, message: '请输入密钥', trigger: 'blur' },
    { min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
  ]
};

// 平台配置验证规则
const platformRules = {
  name: [
    { required: true, message: '请输入平台名称', trigger: 'blur' },
    { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
  ],
  domain: [
    { required: true, message: '请输入平台域名', trigger: 'blur' },
    { pattern: /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/, message: '请输入正确的域名格式', trigger: 'blur' }
  ]
};

/** 查询配置列表 */
const getList = async () => {
  loading.value = true;
  try {
    const res = await listSetting();
    //转为json对象
    // 更新翻译配置数据
    if (res.TRANSLATION_SETTING) {
      translationSettingData.value = JSON.parse(res.TRANSLATION_SETTING);
    }
    console.log('成功:', translationSettingData.value);
    // 更新平台配置数据
    if (res.PLATFORM_SETTING) {
      platformSettingData.value = JSON.parse(res.PLATFORM_SETTING);
    }
  } catch (error) {
    ElMessage.error('获取配置失败');
  } finally {
    loading.value = false;
  }
};

/** 提交翻译配置表单 */
const submitTranslationForm = async () => {
  const formRef = proxy.$refs.translationFormRef as (typeof import('element-plus'))['ElForm'];
  const isValid = await formRef.validate().catch(() => false);

  if (!isValid) return;

  try {
    buttonLoading.value = true;
    const params = {
      settingName: 'TRANSLATION_SETTING',
      type: translationSettingData.value.type,
      appId: translationSettingData.value.appId,
      secretKey: translationSettingData.value.secretKey
    };
    await updateSetting(params);
    ElMessage.success('翻译配置更新成功');
  } catch (error) {
    console.error('更新翻译配置失败:', error);
    ElMessage.error('更新翻译配置失败');
  } finally {
    buttonLoading.value = false;
  }
};

/** 提交平台配置表单 */
const submitPlatformForm = async () => {
  const formRef = proxy.$refs.platformFormRef as (typeof import('element-plus'))['ElForm'];
  const isValid = await formRef.validate().catch(() => false);

  if (!isValid) return;

  try {
    buttonLoading.value = true;
    const params = {
      PLATFORM_SETTING: platformSettingData.value
    };
    await updateSetting(params);
    ElMessage.success('平台配置更新成功');
  } catch (error) {
    console.error('更新平台配置失败:', error);
    ElMessage.error('更新平台配置失败');
  } finally {
    buttonLoading.value = false;
  }
};

/** 重置翻译配置表单 */
const resetTranslationForm = () => {
  const formRef = proxy.$refs.translationFormRef as (typeof import('element-plus'))['ElForm'];
  formRef.resetFields();
};

/** 重置平台配置表单 */
const resetPlatformForm = () => {
  const formRef = proxy.$refs.platformFormRef as (typeof import('element-plus'))['ElForm'];
  formRef.resetFields();
};

onMounted(() => {
  getList();
});
</script>
