const { customerApi } = require('../../utils/api'); Page({ data: { customers: [], page: 1, hasMore: true, loading: false, showAddModal: false, newCustomer: { name: '', company: '', country: '', phone: '', whatsapp_id: '', email: '', }, }, onLoad() { this.loadCustomers(); }, onReachBottom() { if (this.data.hasMore && !this.data.loading) { this.setData({ page: this.data.page + 1 }); this.loadCustomers(true); } }, async loadCustomers(isAppend = false) { if (this.data.loading) return; this.setData({ loading: true }); try { const result = await customerApi.list(this.data.page); this.setData({ customers: isAppend ? [...this.data.customers, ...result.items] : result.items, hasMore: result.items.length >= result.size, loading: false, }); } catch (err) { wx.showToast({ title: err.message || '加载失败', icon: 'none' }); this.setData({ loading: false }); } }, showAdd() { this.setData({ showAddModal: true }); }, hideAdd() { this.setData({ showAddModal: false }); }, onInput(e) { const field = e.currentTarget.dataset.field; const value = e.detail.value; this.setData({ [`newCustomer.${field}`]: value, }); }, async addCustomer() { const { newCustomer } = this.data; if (!newCustomer.name) { wx.showToast({ title: '请输入客户名称', icon: 'none' }); return; } try { await customerApi.create(newCustomer); wx.showToast({ title: '添加成功', icon: 'success' }); this.hideAdd(); this.setData({ page: 1, customers: [] }); this.loadCustomers(); } catch (err) { wx.showToast({ title: err.message || '添加失败', icon: 'none' }); } }, async deleteCustomer(e) { const id = e.currentTarget.dataset.id; wx.showModal({ title: '确认删除', content: '确定要删除该客户吗?', success: async (res) => { if (res.confirm) { try { await customerApi.delete(id); wx.showToast({ title: '已删除', icon: 'success' }); this.setData({ page: 1, customers: [] }); this.loadCustomers(); } catch (err) { wx.showToast({ title: '删除失败', icon: 'none' }); } } }, }); }, viewDetail(e) { const id = e.currentTarget.dataset.id; wx.navigateTo({ url: `/pages/customers/detail?id=${id}` }); }, getStatusClass(status) { const map = { lead: 'text-primary', negotiating: 'text-warning', closed: 'text-success', }; return map[status] || ''; }, onPullDownRefresh() { this.setData({ page: 1, customers: [] }); this.loadCustomers(); wx.stopPullDownRefresh(); }, });