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


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) {
  }

  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);
    }

    slideChanged() {
        //getActiveIndex()获得当前页面的index
        let index = this.contentSlides.getActiveIndex();
        //s切换菜单
        // this.swiper.slideTo(index, 300);
    }


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

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

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

    //设为过期
    overDue(){
        this.appService.ObserverHttpGetAdd('/wisdomgroup/modules/question/submit/',{id:this.temp.id})
            .subscribe((res)=>{
                console.log(res);
            })
    }

    //发布
    release(){
        this.appService.ObserverHttpGetAdd('/wisdomgroup/modules/question/submit/',this.temp.id)
            .subscribe((res)=>{
                this.selectPageMenu(this.swiperIndex);
            })
    }

    //结果
    result(){
        this.showOp = false;
      this.navCtrl.push('ResultPage');
    }

    //复制
    copy(){

    }

    //删除
    delete(){
      console.log(this.temp);
        this.appService.ObserverHttpGet('/wisdomgroup/modules/question/delete',{id:this.temp.id})
            .subscribe((res)=>{
                console.log(res);
                this.selectPageMenu(0);
                this.showOp = false;
            })
    }

    cancle(){
      this.showOp = false;
    }

}
