import request from '../../utils/request.js';

Page({
  data: {
    stats: {
      fakuan: {
        total: 0, total_jine: '0.00',
        daijiaona: 0, daijiaona_jine: '0.00',
        yijiaona: 0, yijiaona_jine: '0.00',
        shensuzhong: 0, shensuzhong_jine: '0.00',
        yibohui: 0, yibohui_jine: '0.00',
        shijidaozhang_jine: '0.00'
      },
      jifen: {
        total: 0, total_jifen: 0,
        daichuli: 0, daichuli_jifen: 0,
        yichuli: 0, yichuli_jifen: 0
      }
    },
    activeTab: 'fakuan',
    activeFakuanStatus: 1,
    activeJifenStatus: 0,
    searchDingdan: '',
    trigger: { fakuan: 0, jifen: 0 }
  },

  onLoad() {
    this.fetchTongji();
  },

  onShow() {
    this.fetchTongji();
  },

  async fetchTongji() {
    try {
      const res = await request({ url: '/yonghu/sjcftjhq', method: 'POST' });
      if (res.statusCode === 200 && res.data.code === 0) {
        this.setData({ stats: res.data.data });
      }
    } catch (e) {
      console.error('获取商家处罚统计失败', e);
    }
  },

  switchMainTab(e) {
    const tab = e.currentTarget.dataset.tab;
    if (tab === this.data.activeTab) return;
    this.setData({ activeTab: tab });
  },

  switchFakuanStatus(e) {
    const status = Number(e.currentTarget.dataset.status);
    if (status === this.data.activeFakuanStatus) return;
    this.setData({ activeFakuanStatus: status });
    this.bumpTrigger('fakuan');
  },

  switchJifenStatus(e) {
    const status = Number(e.currentTarget.dataset.status);
    if (status === this.data.activeJifenStatus) return;
    this.setData({ activeJifenStatus: status });
    this.bumpTrigger('jifen');
  },

  onSearchInput(e) {
    this.setData({ searchDingdan: e.detail.value });
  },

  onSearchConfirm() {
    this.bumpTrigger(this.data.activeTab);
  },

  clearSearch() {
    this.setData({ searchDingdan: '' });
    this.bumpTrigger(this.data.activeTab);
  },

  bumpTrigger(tab) {
    const key = `trigger.${tab}`;
    this.setData({ [key]: this.data.trigger[tab] + 1 });
  },

  onPenaltyChanged() {
    this.fetchTongji();
    this.bumpTrigger('fakuan');
  }
});
