<template>
  <a-page-header :title="getPageTitle()" @back="() => $router.go(-1)">
    <a-card :bordered="false" :body-style="{ padding: '24px' }">
      <!-- 设置标签页 -->
      <a-tabs v-model:activeKey="activeTab" type="card" class="setting-tabs">
        <a-tab-pane key="basic" tab="基础设置">
          <a-form
            :model="basicSettings"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 18 }"
            layout="horizontal"
          >
            <!-- 是否开启分销功能 -->
            <a-form-item label="是否开启分销功能">
              <a-radio-group v-model:value="basicSettings.enableDistribution">
                <a-radio :value="true">开启</a-radio>
                <a-radio :value="false">关闭</a-radio>
              </a-radio-group>
              <div class="setting-desc">开启后用户可以申请成为分销商</div>
            </a-form-item>

            <!-- 分销层级 -->
            <a-form-item label="分销层级">
              <a-radio-group v-model:value="basicSettings.distributionLevel">
                <a-radio :value="1">一级</a-radio>
                <a-radio :value="2">二级</a-radio>
                <a-radio :value="3">三级</a-radio>
              </a-radio-group>
              <div class="setting-desc">设置分销商推荐层级关系</div>
            </a-form-item>

            <!-- 分销商内购 -->
            <a-form-item label="分销商内购">
              <a-radio-group v-model:value="basicSettings.dealerSelfBuy">
                <a-radio :value="true">开启</a-radio>
                <a-radio :value="false">关闭</a-radio>
              </a-radio-group>
              <div class="setting-desc"
                >分销商自己购买是否获得佣金，开启一般佣金</div
              >
            </a-form-item>
          </a-form>
        </a-tab-pane>

        <a-tab-pane key="commission" tab="分销条件">
          <a-form
            :model="commissionSettings"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 18 }"
            layout="horizontal"
          >
            <!-- 申请方式 -->
            <a-form-item label="申请方式">
              <a-radio-group v-model:value="commissionSettings.applyType">
                <a-radio :value="10">需后台审核</a-radio>
                <a-radio :value="20">无需审核</a-radio>
              </a-radio-group>
              <div class="setting-desc">设置用户申请分销商的审核方式</div>
            </a-form-item>

            <!-- 佣金结算 -->
            <a-form-item label="佣金结算">
              <a-radio-group v-model:value="commissionSettings.settlementType">
                <a-radio :value="10">订单完成</a-radio>
                <a-radio :value="20">订单确认收货</a-radio>
              </a-radio-group>
              <div class="setting-desc">设置佣金何时结算到分销商账户</div>
            </a-form-item>

            <!-- 最低提现金额 -->
            <a-form-item label="最低提现金额">
              <a-input-number
                v-model:value="commissionSettings.minWithdrawAmount"
                :min="0"
                :precision="2"
                style="width: 200px"
              >
                <template #addonAfter>元</template>
              </a-input-number>
              <div class="setting-desc">分销商申请提现的最低金额限制</div>
            </a-form-item>
          </a-form>
        </a-tab-pane>

        <a-tab-pane key="withdraw" tab="提现设置">
          <a-form
            :model="withdrawSettings"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 18 }"
            layout="horizontal"
          >
            <!-- 提现方式 -->
            <a-form-item label="提现方式">
              <a-checkbox-group
                v-model:value="withdrawSettings.withdrawMethods"
              >
                <a-checkbox :value="10">微信</a-checkbox>
                <a-checkbox :value="20">支付宝</a-checkbox>
                <a-checkbox :value="30">银行卡</a-checkbox>
              </a-checkbox-group>
              <div class="setting-desc">设置支持的提现方式</div>
            </a-form-item>

            <!-- 提现手续费 -->
            <a-form-item label="提现手续费">
              <a-input-number
                v-model:value="withdrawSettings.withdrawFeeRate"
                :min="0"
                :max="100"
                :precision="2"
                style="width: 200px"
              >
                <template #addonAfter>%</template>
              </a-input-number>
              <div class="setting-desc">提现时收取的手续费比例</div>
            </a-form-item>

            <!-- 提现审核 -->
            <a-form-item label="提现审核">
              <a-radio-group v-model:value="withdrawSettings.withdrawAudit">
                <a-radio :value="true">需要审核</a-radio>
                <a-radio :value="false">无需审核</a-radio>
              </a-radio-group>
              <div class="setting-desc">设置提现申请是否需要人工审核</div>
            </a-form-item>
          </a-form>
        </a-tab-pane>

        <a-tab-pane key="agreement" tab="协议">
          <a-form
            :model="agreementSettings"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 18 }"
            layout="horizontal"
          >
            <!-- 分销商协议 -->
            <a-form-item label="分销商协议">
              <a-textarea
                v-model:value="agreementSettings.dealerAgreement"
                :rows="10"
                placeholder="请输入分销商协议内容..."
              />
              <div class="setting-desc">用户申请分销商时需要同意的协议内容</div>
            </a-form-item>
          </a-form>
        </a-tab-pane>

        <a-tab-pane key="notification" tab="自定义文字">
          <a-form
            :model="notificationSettings"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 18 }"
            layout="horizontal"
          >
            <!-- 申请成功提示 -->
            <a-form-item label="申请成功提示">
              <a-textarea
                v-model:value="notificationSettings.applySuccessText"
                :rows="3"
                placeholder="请输入申请成功后的提示文字..."
              />
            </a-form-item>

            <!-- 申请失败提示 -->
            <a-form-item label="申请失败提示">
              <a-textarea
                v-model:value="notificationSettings.applyFailText"
                :rows="3"
                placeholder="请输入申请失败后的提示文字..."
              />
            </a-form-item>

            <!-- 提现成功提示 -->
            <a-form-item label="提现成功提示">
              <a-textarea
                v-model:value="notificationSettings.withdrawSuccessText"
                :rows="3"
                placeholder="请输入提现成功后的提示文字..."
              />
            </a-form-item>
          </a-form>
        </a-tab-pane>

        <a-tab-pane key="page" tab="页面设置">
          <a-form
            :model="pageSettings"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 18 }"
            layout="horizontal"
          >
            <!-- 分销中心标题 -->
            <a-form-item label="分销中心标题">
              <a-input
                v-model:value="pageSettings.centerTitle"
                placeholder="请输入分销中心页面标题"
              />
            </a-form-item>

            <!-- 分销中心背景图 -->
            <a-form-item label="分销中心背景图">
              <a-upload
                v-model:file-list="pageSettings.backgroundImages"
                list-type="picture-card"
                :max-count="1"
                @preview="handlePreview"
              >
                <div v-if="pageSettings.backgroundImages.length < 1">
                  <PlusOutlined />
                  <div style="margin-top: 8px">上传</div>
                </div>
              </a-upload>
            </a-form-item>
          </a-form>
        </a-tab-pane>
      </a-tabs>

      <!-- 保存按钮 -->
      <div class="setting-footer">
        <a-button
          type="primary"
          size="large"
          @click="saveSettings"
          :loading="saving"
        >
          保存设置
        </a-button>
      </div>
    </a-card>
  </a-page-header>
</template>

<script lang="ts" setup>
  import { ref, reactive, onMounted } from 'vue';
  import { message } from 'ant-design-vue';
  import { PlusOutlined } from '@ant-design/icons-vue';
  import { getPageTitle } from '@/utils/common';
  import {
    updateShopDealerSetting,
    getShopDealerSetting
  } from '@/api/shop/shopDealerSetting';

  // 当前激活的标签页
  const activeTab = ref('basic');
  // 保存状态
  const saving = ref(false);

  // 基础设置
  const basicSettings = reactive({
    enableDistribution: true,
    distributionLevel: 3,
    dealerSelfBuy: false
  });

  // 分销条件设置
  const commissionSettings = reactive({
    applyType: 10,
    settlementType: 10,
    minWithdrawAmount: 100
  });

  // 提现设置
  const withdrawSettings = reactive({
    withdrawMethods: [10, 20, 30],
    withdrawFeeRate: 0,
    withdrawAudit: true
  });

  // 协议设置
  const agreementSettings = reactive({
    dealerAgreement: '分销商协议内容...'
  });

  // 通知设置
  const notificationSettings = reactive({
    applySuccessText: '恭喜您成功成为分销商！',
    applyFailText: '很抱歉，您的申请未通过审核。',
    withdrawSuccessText: '提现申请已提交，请耐心等待处理。'
  });

  // 页面设置
  const pageSettings = reactive({
    centerTitle: '分销中心',
    backgroundImages: []
  });

  /* 图片预览 */
  const handlePreview = (file: any) => {
    console.log('预览图片:', file);
  };

  /* 加载设置 */
  const loadSettings = async () => {
    try {
      // 这里应该调用API获取设置数据
      // const settings = await getShopDealerSetting();
      // 然后将数据分配到各个设置对象中
      console.log('加载设置数据');
    } catch (error) {
      console.error('加载设置失败:', error);
      message.error('加载设置失败');
    }
  };

  /* 保存设置 */
  const saveSettings = async () => {
    saving.value = true;
    try {
      // 收集所有设置数据
      const allSettings = {
        basic: basicSettings,
        commission: commissionSettings,
        withdraw: withdrawSettings,
        agreement: agreementSettings,
        notification: notificationSettings,
        page: pageSettings
      };

      console.log('保存设置:', allSettings);

      // 这里应该调用API保存设置
      // await updateShopDealerSetting(allSettings);

      // 模拟保存
      await new Promise((resolve) => setTimeout(resolve, 1000));

      message.success('设置保存成功');
    } catch (error) {
      console.error('保存设置失败:', error);
      message.error('保存设置失败');
    } finally {
      saving.value = false;
    }
  };

  // 页面加载时获取设置数据
  onMounted(() => {
    loadSettings();
  });
</script>

<script lang="ts">
  export default {
    name: 'ShopDealerSetting'
  };
</script>

<style lang="less" scoped>
  .dealer-setting-container {
    max-width: 1200px;
    margin: 0 auto;
  }

  .setting-tabs {
    :deep(.ant-tabs-card > .ant-tabs-nav .ant-tabs-tab) {
      border-radius: 6px 6px 0 0;
      background: #fafafa;
      border: 1px solid #d9d9d9;
      margin-right: 8px;

      &.ant-tabs-tab-active {
        background: #fff;
        border-bottom-color: #fff;
      }
    }

    :deep(.ant-tabs-content-holder) {
      background: #fff;
      border: 1px solid #d9d9d9;
      border-radius: 0 6px 6px 6px;
      padding: 24px;
      min-height: 500px;
    }
  }

  .setting-desc {
    color: #999;
    font-size: 12px;
    margin-top: 4px;
    line-height: 1.4;
  }

  .setting-footer {
    text-align: center;
    margin-top: 32px;
    padding-top: 24px;
    border-top: 1px solid #f0f0f0;
  }

  :deep(.ant-form-item-label > label) {
    font-weight: 500;
  }

  :deep(.ant-radio-group) {
    .ant-radio-wrapper {
      margin-right: 16px;
    }
  }

  :deep(.ant-checkbox-group) {
    .ant-checkbox-wrapper {
      margin-right: 16px;
      margin-bottom: 8px;
    }
  }

  :deep(.ant-upload-select-picture-card) {
    width: 120px;
    height: 120px;
  }

  :deep(.ant-upload-list-picture-card .ant-upload-list-item) {
    width: 120px;
    height: 120px;
  }
</style>
