// 规则组数据 store（demo 用，刷新后重置为初始种子数据）
import { reactive } from 'vue'

const now = () => {
  const d = new Date()
  const pad = (n) => String(n).padStart(2, '0')
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
}

const state = reactive({
  list: [
    {
      id: 'RG20260401001',
      name: '层层礼-借记卡新客圈选',
      type: 'debitCard',
      logic: 'AND',
      description: '2025/9/30 前开户 + MSC 属于卓越/运筹 + TRB < 50万',
      status: 'online',
      auditStatus: '审核通过',
      creator: '张运营',
      createdAt: '2026-04-01 09:15:30',
      updater: '王审核',
      updatedAt: '2026-04-05 11:20:18',
      rules: [
        {
          id: 'r1',
          metaCode: '001',
          ruleName: '层层礼开户时间',
          config: { accountType: 'I', operator: '<', date: '2025-09-30' }
        },
        {
          id: 'r2',
          metaCode: '010',
          ruleName: '层层礼,MSC',
          config: { operator: 'in', values: ['premier', 'advance'] }
        },
        {
          id: 'r3',
          metaCode: '050',
          ruleName: 'TRB值不含家庭账户余额(连续3个月达标)',
          config: { month: '2025-12', nm: { n: 3, m: 6 }, operator: '<', amount: 500000 }
        },
        {
          id: 'r4',
          metaCode: '051',
          ruleName: '2025年12月月末余额（连续3个月达标）',
          config: { month: '2025-12', nm: { n: 3, m: 6 }, operator: '<', amount: 500000 }
        }
      ]
    },
    {
      id: 'RG20260410002',
      name: '信用卡-高净值持卡客户',
      type: 'creditCard',
      logic: 'AND',
      description: 'MSC 属于卓越/尊尚 且 月末余额 ≥ 100 万',
      status: 'online',
      auditStatus: '审核通过',
      creator: '李产品',
      createdAt: '2026-04-10 16:55:02',
      updater: '李产品',
      updatedAt: '2026-04-10 16:55:02',
      rules: [
        {
          id: 'r1',
          metaCode: '010',
          ruleName: '信用卡MSC',
          config: { operator: 'in', values: ['premier', 'excellence'] }
        },
        {
          id: 'r2',
          metaCode: '051',
          ruleName: '月末余额 ≥ 100万',
          config: { month: '2026-03', nm: { n: 3, m: 6 }, operator: '>=', amount: 1000000 }
        }
      ]
    },
    {
      id: 'RG20260420003',
      name: '借记卡-跨境候选客群',
      type: 'debitCard',
      logic: 'OR',
      description: 'TRB ≥ 50 万 或 月末余额 ≥ 50 万',
      status: 'draft',
      auditStatus: '新增待审核',
      creator: '赵市场',
      createdAt: '2026-04-20 11:48:33',
      updater: '赵市场',
      updatedAt: '2026-04-25 09:30:01',
      rules: [
        {
          id: 'r1',
          metaCode: '050',
          ruleName: 'TRB ≥ 50万',
          config: { month: '2026-04', nm: { n: 1, m: 1 }, operator: '>=', amount: 500000 }
        }
      ]
    }
  ]
})

let seq = state.list.length + 1

function genId() {
  const d = new Date()
  const pad = (n) => String(n).padStart(2, '0')
  const stamp = `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}`
  return `RG${stamp}${String(seq++).padStart(3, '0')}`
}

export default {
  get list() {
    return state.list
  },
  query({ id = '', name = '', type = '', auditStatus = '' } = {}) {
    return state.list.filter((t) => {
      if (id && !t.id.includes(id)) return false
      if (name && !t.name.includes(name)) return false
      if (type && t.type !== type) return false
      if (auditStatus && t.auditStatus !== auditStatus) return false
      return true
    })
  },
  getById(id) {
    return state.list.find((t) => t.id === id)
  },
  create(payload) {
    const record = {
      id: genId(),
      name: payload.name,
      type: payload.type,
      logic: payload.logic || 'AND',
      description: payload.description || '',
      status: 'draft',
      auditStatus: '新增待审核',
      creator: '当前用户',
      createdAt: now(),
      updater: '当前用户',
      updatedAt: now(),
      rules: payload.rules || []
    }
    state.list.unshift(record)
    return record
  },
  update(id, payload) {
    const target = state.list.find((t) => t.id === id)
    if (!target) return null
    target.name = payload.name
    target.type = payload.type
    target.logic = payload.logic || 'AND'
    target.description = payload.description || ''
    target.rules = payload.rules || []
    target.updater = '当前用户'
    target.updatedAt = now()
    return target
  },
  remove(id) {
    const idx = state.list.findIndex((t) => t.id === id)
    if (idx >= 0) state.list.splice(idx, 1)
  },
  publish(id) {
    const target = state.list.find((t) => t.id === id)
    if (!target) return
    target.status = 'online'
    target.auditStatus = '审核通过'
    target.updater = '当前用户'
    target.updatedAt = now()
  }
}
