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

const app = getApp();
const MEIYE = 10;

Component({
  properties: {
    status: { type: Number, value: 0, observer: 'reload' },
    trigger: { type: Number, value: 0, observer: 'reload' }
  },

  data: {
    list: [],
    page: 1,
    hasMore: true,
    loading: false,
    loadingMore: false,
    ossImageUrl: app.globalData.ossImageUrl || '',
    showDetail: false,
    detailItem: {}
  },

  lifetimes: {
    attached() { this.reload(); }
  },

  methods: {
    reload() {
      this.setData({ page: 1, list: [], hasMore: true });
      this.loadData();
    },

    mapItem(item) {
      const n = Number(item.sqzhuangtai);
      let display_status = '未知', status_class = 'zhuangtai-weizhi';
      if (n === 0) { display_status = '待处理'; status_class = 'zhuangtai-daijiaona'; }
      else if (n === 1) { display_status = '已处罚'; status_class = 'zhuangtai-yijiaona'; }
      else if (n === 2) { display_status = '已撤销'; status_class = 'zhuangtai-yibohui'; }
      else if (n === 3) { display_status = '申诉中'; status_class = 'zhuangtai-shensuzhong'; }

      return {
        ...item,
        display_status,
        status_class,
        display_time: item.CreateTime || item.create_time || '--',
        full_zhengju: (item.zhengju_tupian || []).map(u => this.fullUrl(u)),
        full_shensu: (item.shensu_tupian || []).map(u => this.fullUrl(u))
      };
    },

    fullUrl(url) {
      if (!url) return '';
      if (url.startsWith('http')) return url;
      return this.data.ossImageUrl + url;
    },

    async loadData(isLoadMore = false) {
      if (this.data.loading || this.data.loadingMore) return;
      if (isLoadMore && !this.data.hasMore) return;
      if (isLoadMore) this.setData({ loadingMore: true });
      else this.setData({ loading: true });

      try {
        const res = await request({
          url: '/yonghu/sjcfjlhq',
          method: 'POST',
          data: { page: this.data.page, page_size: MEIYE, zhuangtai: this.data.status }
        });
        if (res.statusCode === 200 && res.data.code === 0) {
          const data = res.data.data;
          const rows = (data.list || []).map(item => this.mapItem(item));
          const newList = isLoadMore ? this.data.list.concat(rows) : rows;
          this.setData({
            list: newList,
            hasMore: data.has_more === true,
            page: this.data.page + 1,
            loading: false,
            loadingMore: false
          });
        } else {
          this.setData({ loading: false, loadingMore: false, hasMore: false });
        }
      } catch (e) {
        this.setData({ loading: false, loadingMore: false });
      }
    },

    loadMore() {
      if (!this.data.hasMore || this.data.loadingMore) return;
      this.loadData(true);
    },

    openDetail(e) {
      this.setData({ showDetail: true, detailItem: e.currentTarget.dataset.item });
    },

    closeDetail() {
      this.setData({ showDetail: false, detailItem: {} });
    },

    goOrder(e) {
      const id = e.currentTarget.dataset.id;
      if (!id) return;
      wx.navigateTo({ url: `/pages/merchant-order-detail/merchant-order-detail?dingdan_id=${id}` });
    },

    previewImage(e) {
      const url = e.currentTarget.dataset.url;
      wx.previewImage({ current: url, urls: [url] });
    }
  }
});
