define(['jquery', 'bootstrap', 'backend', 'backend/forecast/components'], function ($, undefined, Backend, components) { 'use strict'; return { index: function () { new Vue({ el: '#forecast-app', components: {'forecast-list': components.list, 'forecast-create': components.create, 'forecast-detail': components.detail}, data: { rows: [], granaries: [], total: 0, page: 1, gid: '', site: '', siteOptions: Config.granarySites || [], loading: false, saving: false, creating: false, viewing: false, selected: null, loadSequence: 0 }, created: function () { this.load(); }, methods: { api: function (action, data, method) { var self = this; return new Promise(function (resolve, reject) { $.ajax({url: 'forecast/' + action, type: method || 'GET', data: data || {}, dataType: 'json', timeout: 45000, success: function (res) { if (res.code !== 200) { self.$message.error(res.msg || '操作失败,请刷新后重试'); reject(new Error(res.msg)); } else resolve(res); }, error: function () { self.$message.error('请求失败或登录已失效,请刷新查看记录后重试'); reject(new Error('request failed')); } }); }); }, load: function () { var self = this, sequence = ++this.loadSequence; this.loading = true; return this.api('index', {gid: this.gid, site: this.site, page: this.page}).then(function (res) { if (sequence !== self.loadSequence) return; self.rows = res.list; self.total = res.total; self.granaries = res.granaries; }).catch(function () {}).then(function () { if (sequence === self.loadSequence) self.loading = false; }); }, siteChanged: function () { this.gid = ''; this.page = 1; this.load(); }, filterChanged: function () { this.page = 1; this.load(); }, changePage: function (page) { this.page = page; this.load(); }, newRecord: function () { this.creating = true; }, closeCreate: function (done) { if (!this.saving) { this.creating = false; if (typeof done === 'function') done(); } }, closeDetail: function (done) { var self = this; if (this.saving) return; this.$refs.detail.confirmDiscard().then(function () { self.viewing = false; if (typeof done === 'function') done(); }).catch(function () {}); }, openRecord: function (id) { var self = this; this.api('detail', {id: id}).then(function (res) { self.selected = res.data; self.viewing = true; }).catch(function () {}); }, write: function (action, data, callback) { if (this.saving) return; var self = this; this.saving = true; this.api(action, data, 'POST').then(function (res) { self.selected = res.data; if (action !== 'actual' && res.data.status === 'failed') self.$message.warning('温湿度已保存,算法暂不可用,可稍后重新生成'); else self.$message.success(action === 'actual' ? '实测值已保存' : res.data.status === 'ready' ? '预测已生成并保存' : '记录已保存'); if (callback) callback(); self.load(); }).catch(function () {}).then(function () { self.saving = false; }); }, createRecord: function (form) { var self = this; this.write('add', form, function () { self.creating = false; self.viewing = true; }); }, saveActual: function (form) { this.write('actual', form); }, retry: function (id) { var self = this; this.$refs.detail.confirmDiscard().then(function () { self.write('retry', {id: id}); }).catch(function () {}); } } }); } }; });