<template>
  <div class="app-config-setting">
    <!-- 顶部提示信息 -->
    <div class="setting-hint">
      <el-alert :closable="false" type="info" class="custom-alert">
        <template #title>
          <div class="alert-content">
            <el-icon class="info-icon"><InfoFilled /></el-icon>
            <span>在此配置移动端APP的展示信息，分为多个端进行独立配置。</span>
          </div>
        </template>
      </el-alert>
    </div>

    <!-- 配置表单区 -->
    <div class="setting-body">
      <el-tabs v-model="activeTab" type="border-card" class="premium-tabs">
        <!-- 履约守护配置 -->
        <el-tab-pane label="履约守护" name="fulfiller">
          <el-form ref="fulfillerFormRef" :model="fulfillerForm" :rules="fulfillerRules" label-width="160px" label-position="right" class="premium-setting-form">
            <!-- 登录图标 -->
            <el-form-item label="登录页图标：" prop="loginIcon">
              <image-upload v-model="fulfillerForm.loginIcon" :limit="1" :can-delete="false" />
              <div class="form-tip">建议尺寸：200x200，支持 png/jpg 格式</div>
            </el-form-item>

            <!-- 登录页背景图 -->
            <el-form-item label="登录页背景：" prop="loginBackground">
              <image-upload v-model="fulfillerForm.loginBackground" :limit="1" :can-delete="false" />
              <div class="form-tip">建议尺寸：1080x1920（9:16），支持 jpg/png 格式</div>
            </el-form-item>

            <!-- 保存按钮 -->
            <el-form-item class="action-item">
              <el-button type="primary" class="save-btn" :loading="buttonLoading" @click="submitFulfillerForm">保存修改</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>

        <!-- 好萌友配置 -->
        <el-tab-pane label="好萌友" name="merchant">
          <el-form ref="merchantFormRef" :model="merchantForm" label-width="160px" label-position="right" class="premium-setting-form">
            <!-- 首页轮播图 -->
            <el-form-item label="首页轮播图：" prop="homeBanner">
              <image-upload v-model="merchantForm.homeBanner" :limit="10" />
              <div class="form-tip">建议尺寸：750x320，支持多张，用于商户端首页轮播展示</div>
            </el-form-item>

            <!-- 保存按钮 -->
            <el-form-item class="action-item">
              <el-button type="primary" class="save-btn" :loading="buttonLoading" @click="submitMerchantForm">保存修改</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script setup name="AppConfig" lang="ts">
// @Author: Antigravity
import { getAppSetting, updateAppSetting } from '@/api/system/appSetting';
import { AppSettingForm } from '@/api/system/appSetting/types';
import { InfoFilled } from '@element-plus/icons-vue';
import ImageUpload from '@/components/ImageUpload/index.vue';
import { ref, onMounted, getCurrentInstance, ComponentInternalInstance } from 'vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const buttonLoading = ref(false);
const activeTab = ref('fulfiller');
const fulfillerFormRef = ref<any>();
const merchantFormRef = ref<any>();

const fulfillerForm = ref<any>({
  id: 1,
  loginIcon: undefined,
  loginBackground: undefined
});

const merchantForm = ref<any>({
  id: 2,
  homeBanner: undefined
});

const fulfillerRules = {
  loginIcon: [{ required: true, message: "登录页图标不能为空", trigger: "change" }],
  loginBackground: [{ required: true, message: "登录页背景不能为空", trigger: "change" }],
};

/** 加载配置 */
const loadConfig = async () => {
  try {
    // 加载履约守护配置 (ID 1)
    const res1 = await getAppSetting(1);
    if (res1.data) {
      fulfillerForm.value.loginIcon = res1.data.loginIcon ? String(res1.data.loginIcon) : undefined;
      fulfillerForm.value.loginBackground = res1.data.loginBackground ? String(res1.data.loginBackground) : undefined;
    }
    
    // 加载好萌友配置 (ID 2)
    const res2 = await getAppSetting(2);
    if (res2.data) {
      merchantForm.value.homeBanner = res2.data.homeBanner ? String(res2.data.homeBanner) : undefined;
    }
  } catch (error) {
    console.error('加载APP配置失败', error);
  }
};

/** 提交履约守护配置 */
const submitFulfillerForm = () => {
  fulfillerFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      try {
        await updateAppSetting(fulfillerForm.value);
        proxy?.$modal.msgSuccess("履约守护配置保存成功");
        loadConfig();
      } finally {
        buttonLoading.value = false;
      }
    }
  });
};

/** 提交好萌友配置 */
const submitMerchantForm = async () => {
  buttonLoading.value = true;
  try {
    await updateAppSetting(merchantForm.value);
    proxy?.$modal.msgSuccess("好萌友配置保存成功");
    loadConfig();
  } finally {
    buttonLoading.value = false;
  }
};

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

<style scoped lang="scss">
.app-config-setting {
  padding: 8px 0;
}

.setting-hint {
  margin-bottom: 24px;

  .custom-alert {
    background-color: #e8f4ff;
    border: none;
    border-radius: 8px;
    padding: 12px 16px;

    .alert-content {
      display: flex;
      align-items: center;
      gap: 10px;
      color: #1890ff;
      font-size: 14px;
      line-height: 1.5;

      .info-icon {
        font-size: 18px;
      }
    }
  }
}

.setting-body {
  padding: 0 20px;
}

.premium-tabs {
  border: none;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
  border-radius: 12px;
  overflow: hidden;

  :deep(.el-tabs__header) {
    background-color: #f8fafc;
    border-bottom: 1px solid #e5e6eb;
  }

  :deep(.el-tabs__item.is-active) {
    background-color: #fff;
    border-right-color: #e5e6eb;
    border-left-color: #e5e6eb;
  }
}

.premium-setting-form {
  padding: 24px 0;
  
  :deep(.el-form-item__label) {
    font-weight: 500;
    color: #4e5969;
    padding-right: 24px;
  }

  .form-tip {
    font-size: 13px;
    color: #86909c;
    margin-top: 8px;
    line-height: 1.4;
    display: block;
    width: 100%;
  }

  .action-item {
    margin-top: 40px;
  }

  .save-btn {
    padding: 12px 32px;
    height: auto;
    font-size: 15px;
    font-weight: 500;
    border-radius: 6px;
    box-shadow: 0 4px 10px rgba(64, 158, 255, 0.2);
  }
}
</style>
