import {Component, ViewChild} from '@angular/core';
import {AlertController, IonicPage, NavController, NavParams, Slides} from 'ionic-angular';
import {DesicrPage} from "../item/desicr/desicr";
import {EditPage} from "../item/edit/edit";
import {DetailPage} from "../item/detail/detail";
import {AppService} from "../../../../service/appHttpService";
import {SurveyResultPage} from "../../mySurvey/survey-result/survey-result";

declare var Swiper;

@IonicPage()
@Component({
    selector: 'page-list',
    templateUrl: 'list.html',
})
export class ListPage {
    @ViewChild('contentSlides') contentSlides: Slides;
    contentList = <any>[
        {
            datalist: []
        }
    ];
    menuList = [
        {name: '草稿箱'},
        {name: '已发布'},
        {name: '已过期'}
    ];
    showOp = false;
    swiperIndex;
    swiper;
    temp;   //选中的item

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

    ionViewDidEnter() {
        this.initSwiper();
        this.selectPageMenu(0);
        this.swiperIndex = 0;
    }


    initSwiper() {
        this.swiper = new Swiper('.pageMenuSlides .swiper-container', {
            //设置slider容器能够同时显示的slides数量(
            slidesPreView: 3,
            //slide之间的距离（单位px）
            spaceBetween: 0,
            //断点设定：根据屏幕宽度设置某参数为不同的值
            breakpoints: {
                1024: {
                    slidesPerView: 3,
                    spaceBetween: 0
                },
                768: {
                    slidesPerView: 3,
                    spaceBetween: 0
                },
                640: {
                    slidesPerView: 3,
                    spaceBetween: 0
                },
                320: {
                    slidesPerView: 3,
                    spaceBetween: 0
                }
            }
        });
    }

    selectPageMenu(index) {
        const data = {
            state: index + 1
        };
        this.appService.ObserverHttpGet('/wisdomgroup/modules/question/findByState', data)
            .subscribe(
                (res: Response) => {
                    this.contentList = res.json();
                }
            );
        this.swiperIndex = index;
        this.showOp = false;
        //切换页面
        // this.contentSlides.slideTo(index);
    }


    //显示问卷操作
    showOpra(item) {
        this.temp = item;
        this.showOp = true;
    }

    //创建
    create() {
        this.navCtrl.push('DesicrPage');
    }

    //编辑
    edit() {
        this.showOp = false;
        this.navCtrl.push('EditPage', {temp: this.temp});
    }

    //查看
    look(){
        this.showOp = false;
        this.navCtrl.push('DetailPage',{temp:this.temp});
    }

    //设为过期
    overDue() {
        this.appService.alert('确定将该问卷设为过期吗',(res)=>{
            this.appService.ObserverHttpGetAdd('/wisdomgroup/modules/question/outdate/', this.temp.id)
            .subscribe((res) => {
                this.appService.popToastView('问卷状态已更新','middle',1000);
                this.selectPageMenu(this.swiperIndex);
            })
        })
    }

    //发布
    release() {
        this.appService.alert('确定发布该问卷吗',
            (res) => {
                this.appService.ObserverHttpGetAdd('/wisdomgroup/modules/question/submit/', this.temp.id)
                    .subscribe((res) => {
                        this.appService.popToastView('发布成功','middle',1000);
                        this.selectPageMenu(this.swiperIndex);
                    })
            }
        )
    }

    //取消发布
    cancel(){
            // 0  清除  1  不清除

        let alert = this.alertCtrl.create();
        alert.setTitle('确定取消发布该问卷吗?');

        alert.addInput({
            type: 'checkbox',
            label: '清空该问卷答题数据',
            value: '0',
            checked: false
        });


        alert.addButton('取消');
        alert.addButton({
            text: '确定',
            handler: res => {
                let data = {isdelete:''};
                if(res.length == 0 ){
                    data.isdelete = '1';
                }else{
                    data.isdelete = '0';
                }
                this.appService.ObserverHttpForm('/wisdomgroup/modules/question/cancel/', this.temp.id,data)
                    .subscribe((res) => {
                        this.appService.popToastView('取消发布成功','middle',1000);
                        this.selectPageMenu(this.swiperIndex);
                    })
            }
        });
        alert.present();


    }

    //结果
    result() {
        this.showOp = false;
        const data = {
            id:this.temp.id
        }
        this.navCtrl.push('SurveyResultPage',{item:data});
    }

    //复制
    copy() {
        this.appService.ObserverHttpGetOption('/wisdomgroup/modules/question/copyQuestion', {id: this.temp.id})
            .subscribe((res) => {
                this.selectPageMenu(this.swiperIndex);
            })
    }

    //删除
    delete() {
        this.appService.alert('确定删除该问卷吗',(res)=>{
            this.appService.ObserverHttpGet('/wisdomgroup/modules/question/delete', {id: this.temp.id})
                .subscribe((res) => {
                    this.appService.popToastView('删除成功','middle',1000);
                    this.selectPageMenu(0);
                    this.showOp = false;
                })
        })
    }

    cancle() {
        this.showOp = false;
    }

}
