Skip to content
goods-cart.ts 3.87 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component} from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {ServeService} from "../../serve.service";
import {GoodsOrderPage} from "../goods-order/goods-order";
import {SpcesComponent} from "../../../../components/spces/spces";
import {CommonService} from "../../../../provide/common.service";


@Component({
    selector: 'page-goods-cart',
    templateUrl: 'goods-cart.html',
})
export class GoodsCartPage {
    cartList = [];
    footerView = false;
    chooseObj = [];

    checkAll;

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private serveSer: ServeService, private modalCtrl: ModalController,
                private commonSer: CommonService) {
    }

    ionViewDidEnter() {
        this.footerView = true;
        this.getCart();
    }

    ionViewWillLeave() {
        this.footerView = false;
    }

    changeCheck(goods) {
        const data = {
            id: goods.id,
            amount: goods.amount,
            brand: goods.brand,
            officeId: goods.officeId,
            model: goods.model,
            specs1: goods.specs1,
            specs2: goods.specs2,
            specs3: goods.specs3,
            specs4: goods.specs4,
            specs5: goods.specs5,
        };
        let get = 0;
        let number;
        this.chooseObj.forEach((e, index) => {
            if (e.id == goods.id) {
                get = 1;
                number = index;
            }
        });
        if (get == 0) this.chooseObj.push(data);
        if (get == 1) this.chooseObj.splice(number, 1);
    }

    //获取购物车列表
    getCart() {
        const data = {
            P_pageNumber: '1',
            P_pageSize: '100',
            status: 1
        };
        this.serveSer.shoppingCar(data).subscribe(
            (res) => {
                this.cartList = res.list;
                this.cartList.forEach(e => {
                    e.checkbox = false;
                })
            }
        )
    }

    //选择规格 数量
    choose(item) {
wangqinghua's avatar
wangqinghua committed
        const data = {
            amount:item.amount,
            specs1:item.specs1,
            specs2:item.specs2,
            specs3:item.specs3,
            specs4:item.specs4,
            specs5:item.specs5,
        }
        let modal = this.modalCtrl.create(SpcesComponent, {goodsId: item.officeId,data:data});
wangqinghua's avatar
wangqinghua committed
        modal.onDidDismiss(res => {
            if (res) {
wangqinghua's avatar
wangqinghua committed
                const d = res;
wangqinghua's avatar
wangqinghua committed
                d.id = item.id;
wangqinghua's avatar
wangqinghua committed
                this.updateCart(d);
wangqinghua's avatar
wangqinghua committed
            }
        });
        modal.present();
    }

wangqinghua's avatar
wangqinghua committed
    updateCart(data){
        this.serveSer.saveOrderSuppy(data).subscribe(
            (res)=>{
                this.getCart();
            }
        )
    }

wangqinghua's avatar
wangqinghua committed
    //全选
    selectAll(e) {
        this.chooseObj = [];
        if (e == false) {
            this.cartList.forEach(e => {
                e.checkbox = false;
            })
        } else {
            this.cartList.forEach(e => {
                e.checkbox = true;
                this.chooseObj.push(e);
            })
        }
    }

    //提交订单
    submit() {
        if (this.chooseObj.length == 0) {
            this.commonSer.toast('请选择申请物品')
            return false;
        }
        const data = {
            "status": 2,
            "orderSuppies": this.chooseObj
        };
        this.commonSer.alert('确定申请所选物品?', () => {
            this.serveSer.saveOrder(data).subscribe(
                (res) => {
                    if (res.errcode == 1000) {
                        this.commonSer.toast("提交申请成功");
                        this.getCart();
                    } else {
                        this.commonSer.toast(res.errmsg);
                    }
                }
            )
        });
    }

    //前往订单
    goOrder() {
        this.navCtrl.push(GoodsOrderPage);
    }

}