Skip to content
food-apply.ts 4.98 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component} from '@angular/core';
import {ActionSheetController, IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {ServeService} from "../../serve.service";
import {CommonService} from "../../../../provide/common.service";
import {DatePipe} from "@angular/common";
import {PersonMulComponent} from "../../../../components/person-mul/person-mul";


@Component({
    selector: 'page-food-apply',
    templateUrl: 'food-apply.html',
})
export class FoodApplyPage {

    obj = {
        orderDate: null,
wangqinghua's avatar
wangqinghua committed
        typeList: [],
wangqinghua's avatar
wangqinghua committed
        typeText: '请选择',
        peopleCount: '',
        standard: '',
        standardText: '请选择',
wangqinghua's avatar
wangqinghua committed
        amount: '',
wangqinghua's avatar
wangqinghua committed
        remark: '',
    };

    checkObj = {
        orderDate: false,
wangqinghua's avatar
wangqinghua committed
        typeList: false,
wangqinghua's avatar
wangqinghua committed
        peopleCount: false,
        standard: false,
    };

    personList = [];
    applyId; //是否编辑
    type;

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

    ionViewDidLoad() {
        this.initParams();
        this.applyId = this.navParams.get('id');
        this.type = this.navParams.get('type');
        console.log(this.type);
        if (this.applyId) {
            this.meetDetail();
        }
    }

    //初始化参数
    initParams() {
        const data = this.navParams.get('data');
        console.log(data);
        if (data) {

        } else {
            this.obj.orderDate = this.datePipe.transform(new Date(), 'yyyy-MM-ddTHH:mm');
        }
    }

wangqinghua's avatar
wangqinghua committed
    //用餐详情
wangqinghua's avatar
wangqinghua committed
    meetDetail() {
        this.serveSer.detailMeals(this.applyId).subscribe(
            (res) => {
                if (res) {
wangqinghua's avatar
wangqinghua committed
                    this.obj.orderDate = res.data.orderDate;
                    this.obj.standard = res.data.standard;
wangqinghua's avatar
wangqinghua committed
                    this.obj.standardText = res.data.amount +"";
wangqinghua's avatar
wangqinghua committed
                    this.obj.peopleCount = res.data.peopleCount;
                    this.obj.amount = res.data.amount;
                    this.obj.typeList = res.data.typeList;
                    this.obj.remark = res.data.remark;
wangqinghua's avatar
wangqinghua committed
                }
            }
        )
    }

    // 选择时间点
wangqinghua's avatar
wangqinghua committed
    chooseTime(type) {
wangqinghua's avatar
wangqinghua committed
        const index = this.obj.typeList.indexOf(type);
        if (index === -1) {
            this.obj.typeList.push(type);
        } else {
            this.obj.typeList.splice(index, 0);
wangqinghua's avatar
wangqinghua committed
        }
wangqinghua's avatar
wangqinghua committed
    }


    //选择标准
    chooseStandard() {
        const buttonsArr = [
            {
                text: '15元',
                handler: () => {
                    this.obj.standard = '1';
                    this.obj.standardText = '15元';
                    this.obj.amount = '15';
                }
            },
            {
                text: '20元',
                handler: () => {
                    this.obj.standard = '2';
                    this.obj.standardText = '20元';
                    this.obj.amount = '20';
                }
            },
            {
                text: '其他',
                handler: () => {
                    this.obj.standard = '3';
                }
            }, {
                text: '取消',
                role: 'cancel',
                handler: () => {
                    console.log('Cancel clicked');
                }
            }
        ];
        const actionSheet = this.actionSheetCtrl.create({
            cssClass: 'cameraAction',
            buttons: buttonsArr
        });
        actionSheet.present();
    }


    //提交申请
    submit() {
        //校验
        let checkBool = false;
        for (let i in this.checkObj) {
            if (!this.obj[i]) {
                checkBool = true;
                this.checkObj[i] = true;
            } else {
                this.checkObj[i] = false;
            }
        }

        if (checkBool) {
            this.commonSer.toast("请输入必填项!");
            return false;
        }

        let carUser = [];
        this.personList.forEach(e => {
            carUser.push(e.id);
        });
wangqinghua's avatar
wangqinghua committed
        const data =<any> {
            orderDate: this.datePipe.transform(this.obj.orderDate, 'yyyy-MM-dd'),
            typeList: this.obj.typeList,
wangqinghua's avatar
wangqinghua committed
            peopleCount: this.obj.peopleCount,
            standard: this.obj.peopleCount,
            amount: this.obj.amount,
            remark: this.obj.remark,
        };
wangqinghua's avatar
wangqinghua committed
        if(this.applyId) data.id = this.applyId;
wangqinghua's avatar
wangqinghua committed
        this.commonSer.alert('确认提交?', () => {
            this.serveSer.saveMeals(data).subscribe(
                (res) => {
                    if (res.errcode == 1000) {
                        this.commonSer.toast('提交申请成功');
                        this.navCtrl.pop();
                    } else {
                        this.commonSer.toast(res.errmsg);
                    }
                }
            )
        });
    }

}