<template>
  <view class="marketing-checkin-config">
    <view class="config-card border-shadow">
      <view class="config-header">
        <text class="config-title">用户签到配置</text>
      </view>
      
      <view class="config-body">
        <!-- 加载遮罩 -->
        <view v-if="isLoading" class="loading-mask">
          <text class="loading-text">加载中...</text>
        </view>

        <view class="config-item">
          <view class="item-label">
            <text class="label-txt">签到开关：</text>
            <text class="label-desc">签到开关，商城是否开启签到功能，关闭后隐藏签到入口</text>
          </view>
          <view class="item-content">
            <view class="radio-group">
              <view class="radio-item" @click="config.is_open = true">
                <view class="radio-circle" :class="{ checked: config.is_open }"></view>
                <text class="radio-txt">开启</text>
              </view>
              <view class="radio-item ml-20" @click="config.is_open = false">
                <view class="radio-circle" :class="{ checked: !config.is_open }"></view>
                <text class="radio-txt">关闭</text>
              </view>
            </view>
          </view>
        </view>

        <view class="config-item">
          <view class="item-label">
            <text class="label-txt">签到模式：</text>
            <text class="label-desc">无限制，累积和连续签到不会清零；周循环，每周一会清理累积和连续的记录为0，重新开始计算；月循环，每月一号会清理累积和连续的记录为0，重新开始计算</text>
          </view>
          <view class="item-content">
            <view class="radio-group">
              <view class="radio-item" @click="config.mode = 'none'">
                <view class="radio-circle" :class="{ checked: config.mode === 'none' }"></view>
                <text class="radio-txt">无限制</text>
              </view>
              <view class="radio-item ml-20" @click="config.mode = 'week'">
                <view class="radio-circle" :class="{ checked: config.mode === 'week' }"></view>
                <text class="radio-txt">周循环</text>
              </view>
              <view class="radio-item ml-20" @click="config.mode = 'month'">
                <view class="radio-circle" :class="{ checked: config.mode === 'month' }"></view>
                <text class="radio-txt">月循环</text>
              </view>
            </view>
          </view>
        </view>

        <view class="config-item">
          <view class="item-label">
            <text class="label-txt">签到提醒：</text>
            <text class="label-desc">是否开启签到提醒，提醒方式为短信以及站内信</text>
          </view>
          <view class="item-content">
            <view class="radio-group">
              <view class="radio-item" @click="config.notice_enabled = true">
                <view class="radio-circle" :class="{ checked: config.notice_enabled }"></view>
                <text class="radio-txt">开启</text>
              </view>
              <view class="radio-item ml-20" @click="config.notice_enabled = false">
                <view class="radio-circle" :class="{ checked: !config.notice_enabled }"></view>
                <text class="radio-txt">关闭</text>
              </view>
            </view>
          </view>
        </view>

        <view class="config-item">
          <view class="item-label">
            <text class="label-txt">签到赠送积分：</text>
            <text class="label-desc">签到赠送积分，每日签到赠送的积分值</text>
          </view>
          <view class="item-content">
            <input class="config-input" type="number" v-model="config.integral" />
          </view>
        </view>

        <view class="config-item">
          <view class="item-label">
            <text class="label-txt">签到赠送经验：</text>
            <text class="label-desc">签到赠送用户经验值</text>
          </view>
          <view class="item-content">
            <input class="config-input" type="number" v-model="config.exp" />
          </view>
        </view>

        <view class="config-footer">
          <button class="btn-submit" @click="handleSave" :loading="isSaving">提交</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="uts">
import { reactive, onMounted, ref } from 'vue'
import { fetchCheckinConfig, saveCheckinConfig, CheckinConfig } from '@/services/admin/marketingService.uts'

const isLoading = ref(false)
const isSaving = ref(false)

const config = reactive({
  is_open: true,
  mode: 'none',
  notice_enabled: false,
  integral: 10,
  exp: 1
})

onMounted(() => {
  loadConfig()
})

async function loadConfig() {
  isLoading.value = true
  try {
    const res = await fetchCheckinConfig()
    if (res != null) {
      config.is_open = res.is_open
      config.mode = res.mode
      config.notice_enabled = res.notice_enabled
      config.integral = res.integral_reward
      config.exp = res.exp_reward
    }
  } catch (e) {
    uni.showToast({ title: '加载配置失败', icon: 'none' })
  } finally {
    isLoading.value = false
  }
}

async function handleSave() {
  if (isSaving.value) return
  
  isSaving.value = true
  const payload : CheckinConfig = {
    is_open: config.is_open,
    mode: config.mode,
    notice_enabled: config.notice_enabled,
    integral_reward: config.integral,
    exp_reward: config.exp
  }

  try {
    const success = await saveCheckinConfig(payload)
    if (success) {
      uni.showToast({ title: '保存成功', icon: 'success' })
    } else {
      uni.showToast({ title: '保存失败', icon: 'none' })
    }
  } catch (e) {
    uni.showToast({ title: '系统异常', icon: 'none' })
  } finally {
    isSaving.value = false
  }
}
</script>

<style scoped lang="scss">
.marketing-checkin-config {
  padding: 0px;
}

.border-shadow {
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.config-card { padding: 24px; position: relative; }

.config-header {
  border-bottom: 1px solid #e8eaec;
  padding-bottom: 16px;
  margin-bottom: 24px;
}

.config-title {
  font-size: 16px;
  font-weight: bold;
  color: #17233d;
  position: relative;
  padding-left: 12px;
}
.config-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 4px;
  bottom: 4px;
  width: 3px;
  background: #1890ff;
}

.config-item {
  display: flex;
  flex-direction: row;
  margin-bottom: 30px;
  align-items: flex-start;
}

.item-label { width: 220px; display: flex; flex-direction: column; }
.label-txt { font-size: 14px; color: #333; margin-bottom: 4px; }
.label-desc { font-size: 12px; color: #999; line-height: 1.5; padding-right: 20px; }

.item-content { flex: 1; }

.radio-group { display: flex; flex-direction: row; padding-top: 4px; flex-wrap: wrap; }
.radio-item { display: flex; flex-direction: row; align-items: center; cursor: pointer; margin-bottom: 10px; }
.radio-circle { width: 14px; height: 14px; border: 1px solid #dcdfe6; border-radius: 50%; margin-right: 6px; position: relative; }
.radio-circle.checked { border-color: #1890ff; }
.radio-circle.checked::after { content: ''; position: absolute; width: 8px; height: 8px; background: #1890ff; border-radius: 50%; top: 2px; left: 2px; }
.radio-txt { font-size: 14px; color: #606266; }
.ml-20 { margin-left: 20px; }

.config-input {
  width: 400px;
  height: 32px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 0 12px;
  font-size: 14px;
}

.config-footer {
  margin-top: 40px;
  padding-left: 220px;
}

.btn-submit {
  width: 80px;
  height: 32px;
  line-height: 32px;
  background: #1890ff;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
}

.loading-mask {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(255, 255, 255, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.loading-text { color: #1890ff; font-size: 14px; }
</style>
