import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {Response} from '@angular/http';
import {AppGlobal, AppService} from '../../service/appHttpService';
import {AnnouncementViewPage} from '../announcementView/announcementView';

@IonicPage()
@Component({
    selector: 'page-announcement',
    templateUrl: 'announcement.html'
})

export class AnnouncementPage {

    allNotice = [];

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public appService: AppService) {

    }

    ionViewWillEnter() {
        //加载通知公告
        this.getAllNoticeAboutMe();
        this.addRecodings();
    }

    goView(id, isRead) {
        if (!isRead) {
            this.createRecording(id);
        }
        this.navCtrl.push("AnnouncementViewPage", {
            id: id
        });
    }

    //获取所有关于我的通知公告
    getAllNoticeAboutMe() {
        this.appService.ObserverHttpPost("/wisdomgroup/modules/notice/getAllNotice", null)
            .subscribe((res: Response) => {
                    let data = res.json();
                    data.forEach(event=>{
                        let src = event.noticeContent.split('src="');
                        event.noticeContent = src.join('src="' + AppGlobal.pictureNotice);
                        // let reg = new RegExp('\\<img.*?\\>');
                        // let result = event.noticeContent.replace(reg,'[图片]');
                        // event.noticeContent = result;
                    });

                    this.allNotice = data;

                }, error => {
                }
            );
    }

    //如果是未读状态，增加阅读记录
    createRecording(id) {
        this.appService.ObserverHttpPost("/wisdomgroup/modules/notice/createRecording", {id: id})
            .subscribe((res: Response) => {

                }, error => {
                }
            );
    }

    goBack() {
        this.navCtrl.popToRoot();
    }

    //增加阅读记录
    addRecodings() {
        this.appService.ObserverHttpGet("/wisdomgroup/modules/notice/addRecodings", null)
            .subscribe((res: Response) => {

                }, error => {
                }
            );
    }
}
