// pages/addOrder/addOrder.js
import { centToYuan } from '../../unit/priceUtil'
import { requestCustomerList } from '../../http/purchasCustomerApi'
import { goodsGroup } from '../../http/goodsApi'
import {requestAddOrder,requestDetailOrder,requestEditOrder } from '../../http/orderListApi'
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
var dayjs = require('dayjs')
Page({
/**
* 页面的初始数据
*/
data: {
CustomerPopup:false,
CustomerData:[],
customerObjTarget:{},
TargetPopup:false,
vanDateTimeVal: new Date().getTime(),
SendTimeVal:"",
OrdersGoods:[],
TotalPriceVal:0,
DetailOrderID:"",
TargetData:[
{lable:"教师",value:"教师"},
{lable:"学生",value:"学生"}
],
customerSubTarget:"",
showTime:false,
formatter(type, value) {
if (type === 'year') {
return `${value}年`;
}
if (type === 'month') {
return `${value}月`;
}
return value;
},
minDate: new Date().getTime(),
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
if(options.MyOrderID){
this.getDetails(options.MyOrderID)
return false
}
if(JSON.stringify(options) == "{}"){
}else{
let detailInfo=JSON.parse(decodeURIComponent(options.detInfo));
console.log(detailInfo)
this.setData({
customerObjTarget:detailInfo.basicInfo,
SendTimeVal:detailInfo.basicInfo.SendTimeVal,
customerSubTarget:detailInfo.basicInfo.customerSubTarget,
OrdersGoods:detailInfo.FromChoseGoods,
DetailOrderID:detailInfo.basicInfo.DetailOrderID,
})
this.totalPriceFun()
}
},
getDetails(DelItem){
var that =this;
let conditionData={
keyword:"",
agreementId:"",
pageIndex: 1,
pageSize: 9999
}
goodsGroup(conditionData).then((res) => {
var groupData = res.data.list;
console.log(groupData)
requestDetailOrder(groupData,DelItem).then((res) => {
function FindItem(manyList,lessList){
var itemOBJ=[]
manyList.map((item,index)=>{
lessList.map((each,index1)=>{
if(item.piId==each.productInfo.id){
itemOBJ.push({...item, SelNum:each.productNum})
}
})
})
return itemOBJ
}
let resultData = res.data;
console.log(resultData.detailList)
console.log(FindItem(groupData,resultData.detailList))
var newDate= dayjs(resultData.supplyDate).format('YYYY-MM-DD')
this.setData({
DetailOrderID:resultData.orderId,
OrdersGoods:FindItem(groupData,resultData.detailList),
customerObjTarget:{
customerId:resultData.customerId,
ciCustomerName:resultData.customerName,
DetailOrderID:resultData.orderId,
},
customerSubTarget:resultData.customerSubTarget,
SendTimeVal:newDate,
})
// this.totalPriceFun()
})
})
},
onClickIcon(){
this.setData({
TargetPopup:true,
})
},
onCancelTarget(){
this.setData({
TargetPopup:false,
})
},
onConfirmonTarget(event){
let itemData = event.detail;
this.setData({
customerSubTarget:itemData.value,
TargetPopup:false,
})
},
clickTimeFun(){
this.setData({
showTime:true,
})
},
TimeClose(){
this.setData({
showTime:false,
})
},
clickGoChose(){
const newData = JSON.stringify(
{
basicInfo:{
...this.data.customerObjTarget,
SendTimeVal:this.data.SendTimeVal,
customerSubTarget:this.data.customerSubTarget,
},
SeledGoods:this.data.OrdersGoods
}
);
wx.redirectTo({ url: `/pages/ChoseGoods/ChoseGoods?detInfo=`+ encodeURIComponent(newData) });
},
timeConfirm(val) {
let TimeVal = dayjs(val.detail).format('YYYY-MM-DD');
this.setData({
SendTimeVal:TimeVal,
showTime:false,
})
return
},
clickBuyerFun(){
this.setData({
CustomerPopup:true,
})
},
ChangePrice(event) {
let that =this;
let indexNum = event.currentTarget.dataset.itemIndex;
this.data.OrdersGoods[indexNum].latestSaleUnitPrice = event.detail;
setTimeout(function () {
that.setData({
OrdersGoods:that.data.OrdersGoods,
})
}, 1600);
// this.totalPriceFun()
},
ChangeNum(event){
let that =this;
let indexNum = event.currentTarget.dataset.itemIndex;
this.data.OrdersGoods[indexNum].SelNum = event.detail;
setTimeout(function () {
that.setData({
OrdersGoods:that.data.OrdersGoods,
})
}, 1600);
// this.totalPriceFun()
},
DelItem(event){
let indexNum = event.currentTarget.dataset.itemIndex;
let that =this;
Dialog.confirm({
title: '标题',
message: '弹窗内容',
})
.then(() => {
that.delEachOrder(indexNum)
})
.catch(() => {
// on cancel
});
},
delEachOrder(indexNum){
this.data.OrdersGoods.splice(indexNum, 1);
this.setData({
OrdersGoods:this.data.OrdersGoods,
})
// this.totalPriceFun()
},
totalPriceFun () {
var sum1 = 0;
for (var i in this.data.OrdersGoods) {
sum1 += this.data.OrdersGoods[i].latestSaleUnitPrice * this.data.OrdersGoods[i].SelNum;
}
this.setData({
TotalPriceVal:sum1,
});
},
onConfirmCustomer(event){
let itemObj=event.detail.value;
let NewOptions = itemObj.customerSubTargets.split(",").filter(ele => ele !== '');
this.setData({
TargetData:NewOptions,
customerObjTarget:itemObj,
CustomerPopup:false
})
},
onReady() {
},
onCancelCustomer(){
this.setData({
CustomerPopup:false,
})
},
loadCustomerFun(){
var that =this;
requestCustomerList().then((res) => {
console.log(res)
that.setData({
CustomerData:res.data.list,
})
})
},
SubmitAddOrders(){
let postOrders= this.data.OrdersGoods.map((item) => {
return {
productId:item.piId,
productNum:item.SelNum,
unitPrice:item.latestSaleUnitPrice,
};
});
let postdata={
items:postOrders,
agreementId:"",
customerId:this.data.customerObjTarget.customerId,
customerName:this.data.customerObjTarget.ciCustomerName,
customerSubTarget:this.data.customerSubTarget,
supplyDate:this.data.SendTimeVal,
supplyType:10,
}
console.log(postdata)
console.log(this.data.DetailOrderID)
if(this.data.DetailOrderID){
requestEditOrder({...postdata,orderId:this.data.DetailOrderID}).then((res) => {
Toast.success('编辑成功');
setTimeout(function () {
wx.navigateBack({ delta: 1 });
}, 1600);
})
}else{
requestAddOrder(postdata,).then((res) => {
Toast.success('新增成功');
setTimeout(function () {
wx.navigateBack({ delta: 1 });
}, 1600);
})
}
},
SubmitBack() {
wx.navigateBack({ delta: 1 });
},
onShow() {
this.loadCustomerFun()
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})
网友评论