const util = require('../../utils/util') const form = require('../../utils/formValidation') const app = getApp() Page({ /** * 页面的初始数据 */ data: { isEdit: false, storeList: [], agentList:[], userInfo: {}, values: { storeId: '', storeName: '', schemeName: '', schemeBillEntryList: [] }, id:'' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var that = this if (options && options.id) { that.setData({ isEdit: true, id:options.id }) that.bindScheme() } wx.setNavigationBarTitle({ title: that.data.isEdit ? '方案编辑' : '新增方案' }) this.setData({ userInfo: app.globalData.userInfo }) this.getAllAgentList(); if(this.data.userInfo.mgvmRole==3){ this.setData({ ['values.agentId']: this.data.userInfo.id, ['values.agentName']: this.data.userInfo.username, }) } this.bindStoreList() }, bindStorePickerChange: function (e) { var item = this.data.storeList[e.detail.value] this.setData({ ['values.storeId']: item.id, ['values.storeName']: item.storeName }) }, bindAgentPickerChange: function (e) { var item = this.data.agentList[e.detail.value] this.setData({ ['values.agentId']: item.id, ['values.agentName']: item.username }) this.bindStoreList() }, formSubmit: function (e) { console.log(this.data.values.schemeBillEntryList) var that = this //表单规则 let rules = [{ name: "schemeName", rule: ["required"], //可使用区间,此处主要测试功能 msg: ["请输入方案名称"] },{ name: "storeName", rule: ["required"], //可使用区间,此处主要测试功能 msg: ["请输入门店"] }]; if(this.data.userInfo.mgvmRole!=3){ var agentRule = { name: "agentName", rule: ["required"], msg: ["请输入代理商"] } rules.push(agentRule) }else{ this.setData({ ['values.agentId']: this.data.userInfo.id, ['values.agentName']: this.data.userInfo.username, }) } //进行表单检查 let formData = e.detail.value; let checkRes = form.validation(formData, rules); let schemeBillEntryList = this.data.values.schemeBillEntryList; var schemeProduct = []; var productCount = 0; for (let i = 0; i < schemeBillEntryList.length; i++) { if(schemeBillEntryList[i].productId!=null&&schemeBillEntryList[i].productId!=""){ schemeProduct.push(schemeBillEntryList[i]) productCount+=1; if(schemeBillEntryList[i].areaDivide<0||schemeBillEntryList[i].storeDivide==null){ util.toast('请输入代理分成') return; } if(schemeBillEntryList[i].areaDivide<0||schemeBillEntryList[i].areaDivide>100){ util.toast('代理分成必须是0-100的分成') return; } if(schemeBillEntryList[i].areaDivide<0||schemeBillEntryList[i].storeDivide==null){ util.toast('请输入门店分成') return; } if(schemeBillEntryList[i].storeDivide<0||schemeBillEntryList[i].storeDivide>100){ util.toast('门店分成必须是0-100的分成') return; } if(schemeBillEntryList[i].storeDivide+schemeBillEntryList[i].areaDivide<100|| schemeBillEntryList[i].storeDivide+schemeBillEntryList[i].areaDivide>100){ util.toast('分成之和应为100%') return; } } } return if(productCount==0){ util.toast('请选择至少一个商品!') return; } if (!checkRes) { if(!that.data.isEdit){ this.setData({ ["values.schemeBillEntryList"]: schemeProduct }) util.request(util.api.addScheme, that.data.values, "POST", false, true, app.globalData.token).then((res) => { if (res.code == 200) { // wx.navigateBack() wx.navigateTo({ url: '/pages/scheme/index', }) } }).catch((res) => {}) }else{ util.request(util.api.editScheme, that.data.values, "POST", false, true, app.globalData.token).then((res) => { if (res.code == 200) { wx.showToast({ title: '编辑成功!', }) setTimeout(() => { wx.navigateBack() }, 500); // wx.navigateBack() } }).catch((res) => {}) } } else { wx.showToast({ title: checkRes, icon: "none" }); } }, formReset: function (e) { console.log("清空数据") }, inputedit: function (e) { let _this = this; let dataset = e.currentTarget.dataset; let value = e.detail.value; let name = dataset.name; _this.data[name] = value; _this.setData({ [name]: _this.data[name] }); }, inputeditRow: function (e) { let that = this; let dataset = e.currentTarget.dataset; let value = e.detail.value; let name = dataset.name; var list = that.data.values.schemeBillEntryList var index = dataset.index; var itemProduct = dataset.item value = parseFloat(value) if(!value){ value = 0.00; } console.log(name) if(name=="storeDivide"){ itemProduct.storeDivide = value }else if(name=="areaDivide"){ itemProduct.areaDivide = value } list.splice(index,1,itemProduct) that.setData({ ["values.schemeBillEntryList"]:list }) console.log(this.data.values.schemeBillEntryList) }, bindStoreList() { var that = this util.request(util.api.storeList, {agentId:that.data.values.agentId}, "GET", false, true, app.globalData.token).then((res) => { if (res.code == 200) { that.setData({ storeList: res.result }) } }).catch((res) => {}) }, //代理商 getAllAgentList() { var that = this util.request(util.api.allAgentList, {}, "GET", false, true,app.globalData.token).then((res) => { if (res.code == 200) { that.setData({ agentList: res.result }) } }).catch((res) => {}) }, toSelect(e) { wx.navigateTo({ url: '/pages/scheme/select?index=' + e.currentTarget.dataset.index, }) }, bindScheme(){ var that = this util.request(util.api.queryBySchemeId, { id: that.data.id }, "GET", true, true, app.globalData.token).then((res) => { if (res.code == 200) { that.setData({ values: res.result, }) } }).catch((res) => {}) }, deleteScheme(e){ let that = this; util.request(util.api.deleteSchemeById, {'id':that.data.id}, "POST", true, true, app.globalData.token).then((res) => { if(res.code == 200){ wx.navigateTo({ url: '/pages/scheme/index', }) } }).catch((res) => {}) }, //添加格子 addGrid(){ var schemeBillEntryList =this.data.values.schemeBillEntryList; var product = {productName:'请选择商品',gridNum:schemeBillEntryList.length+1}; schemeBillEntryList.push(product) this.setData({ ["values.schemeBillEntryList"]: schemeBillEntryList }) } })