import { Component, OnInit } from '@angular/core'
import { CommonService } from '../common.service'
import { Options } from 'selenium-webdriver/chrome'
@Component({
  selector: 'app-ask',
  templateUrl: './ask.component.html',
  styleUrls: ['./ask.component.css']
})
export class AskComponent implements OnInit {
  allQues: Array<any>;
  curPage: number;
  totalPage: number;
  curQues: string;
  curPageData: Object;
  allAnswers: Array<any>;
  isShowChildAgeBtn: boolean;
  hasMate: boolean;
  //获取省份
  provinceList: Array<any>;
  nextBtn:boolean;
  // 暂存问题也信息
  questionInfo:any;
  constructor(private commonService: CommonService) {}

  ngOnInit() {
    this.curPage = 0
    this.totalPage = 17
    this.curPageData = []
    this.allAnswers = []
    this.isShowChildAgeBtn = true
    this.hasMate = true
    this.surveyInfo()
  }

  surveyInfo() {
    this.commonService.surveyInfo().then(res => {
      if (res['success']) {
        this.allQues = res['data']['survey'].pages
        this.curPageData = this.allQues[this.curPage]
      }
    })
    this.commonService.provinceqry({ insurerId: 11 }).then(res => {
      if (res['success']) {
        this.provinceList = res['data'].provinceList
      }
    })
  }
  reducePage() {
    if (this.curPage > 0) {
      const page2Answer = this.allAnswers[1]
      if(page2Answer){
        const optionId = page2Answer.questions[0].options[0].optionId
        if (this.curPage == 5 || this.curPage == 6) {
          if (optionId == 1 || optionId == 3) {
            this.curPage = this.curPage - 2
            this.setCurPageData()
            return
          }
        }

        if (this.curPage == 13) {
          if (optionId == 1) {
            this.curPage = this.curPage - 3
            this.setCurPageData()
            return
          }
          if (optionId == 2) {
            this.curPage = this.curPage - 2
            this.setCurPageData()
            return
          }
        }
        if (this.curPage == 12) {
          if (optionId == 3) {
            this.curPage = this.curPage - 2
            this.setCurPageData()
            return
          }
        }
      }

      this.curPage--;
      this.setCurPageData()
    } else {
      return
    }
  }
  filterItems() {
    return this.allAnswers.filter(item => {
      return item != null
    })
  }
  addPage() {
    if(this.nextBtn===false){
      return;
    }
    if(this.questionInfo){
      this.allAnswers[this.curPage] = this.questionInfo;
      this.questionInfo = null
    }
    if (this.curPage < 16) {
      //判断当前页是否有答案
      if (
        this.curPage == 0 ||
        this.allAnswers[this.curPage] ||
        this.curPage == 5 ||
        this.curPage == 9
      ) {
        this.curPage++
        this.setCurPageData()
      }
    } else {
      this.curPage = 0
      return
    }

    const answerParam = {
      survey: {
        customerId: this.commonService.getQueryString('customerId'),
        orderId: this.commonService.getQueryString('orderId'),
        pages: this.filterItems()
      }
    }
    console.log(answerParam)
    if (this.curPage == 16) {
      this.commonService.saveCustomerAnwers(answerParam).then(res => {
        
      })
    }
  }
  getAllAnswer(e) {
    this.questionInfo = e;
    if(this.curPage == 6 || this.curPage == 8){
      const page2Answer = this.allAnswers[1]
      const optionId = page2Answer.questions[0].options[0].optionId
      if(optionId==1 || optionId==3){
        if(e['questions'].length>0){
          this.nextBtn = true
        }
      }
    }
    if(this.curPageData['questions'].length == e['questions'].length){
      this.nextBtn = true
    }
  }
  setCurPageData() {
    const thePateData = this.allQues[this.curPage]
    this.curPageData = thePateData
    const page2Answer = this.allAnswers[1]
    if (this.curPage == 2) {
      this.filterPage2(
        page2Answer.questions[0].options[0].optionId,
        thePateData
      )
    }
    //第4页
    if (this.curPage == 4) {
      this.filterPage5(
        page2Answer.questions[0].options[0].optionId,
        thePateData
      )
    }
    if (this.curPage == 11) {
      this.filterPage12(
        page2Answer.questions[0].options[0].optionId,
        thePateData
      )
    }
    if (this.curPage == 12) {
      this.filterPage13(
        page2Answer.questions[0].options[0].optionId,
        thePateData
      )
    }
    if (this.curPage == 13) {
      this.filterPage14(
        page2Answer.questions[0].options[0].optionId,
        this.allQues[this.curPage]
      )
    }
    if (this.curPage == 14) {
      this.filterPage15(
        page2Answer.questions[0].options[0].optionId,
        this.allQues[this.curPage]
      )
    }
    if (this.curPage < 16) {
      //判断当前页是否有答案
      if (
        this.curPage == 0 ||
        this.allAnswers[this.curPage] ||
        this.curPage == 5 ||
        this.curPage == 9
      ) {
        this.nextBtn = true;
      }else{
        this.nextBtn = false;
      }
    }
  }

  filterPage2(optionId, thePateData) {
    //判断第二页选择家庭1单身2二人3独立带娃4多口
    if (optionId == 1 || optionId == 2) {
      this.isShowChildAgeBtn = false
    } else {
      this.isShowChildAgeBtn = true
    }
    //判断页面是否有配偶选项
    if (optionId == 1 || optionId == 3) {
      this.hasMate = false
    } else {
      this.hasMate = true
    }
    //单身贵族->您的年龄
    if (optionId == 1) {
      this.curPageData = {
        ...thePateData,
        questions: thePateData.questions.filter(question => {
          return question.questionId == 2
        })
      }
      return
    }
    //二人世界->您的年龄&配偶年龄
    if (optionId == 2) {
      this.curPageData = {
        ...thePateData,
        questions: thePateData.questions.filter(question => {
          return question.questionId <= 3
        })
      }
      return
    }
    //单亲->您的年龄&孩子年龄
    if (optionId == 3) {
      this.curPageData = {
        ...thePateData,
        questions: thePateData.questions.filter(question => {
          return question.questionId == 2 || question.questionId == 4
        })
      }
    }
  }

  filterPage5(optionId, thePateData) {
    //page5判断是否显示配偶工作页
    //1单身 3单亲
    if (optionId == 1 || optionId == 3) {
      this.curPage++
      this.curPageData = this.allQues[this.curPage]
    }
  }

  //pege12判断是否显示配偶健康
  filterPage12(optionId, thePateData) {
    if (optionId == 1 || optionId == 3) {
      this.curPage++
      this.curPageData = this.allQues[this.curPage]
    }
  }

  //page13判断是否显示孩子健康
  filterPage13(optionId, thePateData) {
    //单身或二人世界
    if (optionId == 1 || optionId == 2) {
      this.curPage++
    }
  }

  //page14判断是否住院option内容
  filterPage14(optionId, thePateData) {
    //如果单身或者单亲选择相中没有配偶选项
    //传一个optionType告诉子页面是单选还是多选1表示单选2表示多选
    if (optionId == 1) {
      this.curPageData = {
        ...thePateData,
        questions: [
          {
            ...thePateData.questions[0],
            optionType: 1,
            options: thePateData.questions[0]['options'].filter(option => {
              return option.optionId == 106 || option.optionId == 107
            })
          }
        ]
      }
    }
    if (optionId == 2) {
      this.curPageData = {
        ...thePateData,
        questions: [
          {
            ...thePateData.questions[0],
            optionType: 2,
            options: thePateData.questions[0]['options'].filter(option => {
              return (
                option.optionId == 108 ||
                option.optionId == 109 ||
                option.optionId == 111
              )
            })
          }
        ]
      }
    }
    if (optionId == 3) {
      this.curPageData = {
        ...thePateData,
        questions: [
          {
            ...thePateData.questions[0],
            optionType: 2,
            options: thePateData.questions[0]['options'].filter(option => {
              return (
                option.optionId == 108 ||
                option.optionId == 110 ||
                option.optionId == 111
              )
            })
          }
        ]
      }
    }
    if (optionId == 4) {
      this.curPageData = {
        ...thePateData,
        questions: [
          {
            ...thePateData.questions[0],
            optionType: 2,
            options: thePateData.questions[0]['options'].filter(option => {
              return (
                option.optionId == 108 ||
                option.optionId == 109 ||
                option.optionId == 110 ||
                option.optionId == 111
              )
            })
          }
        ]
      }
    }
  }

  //判断是否吸烟option内容
  filterPage15(optionId, thePateData) {
    //如果单身或者单亲选择相中没有配偶选项
    //传一个optionType告诉子页面是单选还是多选1表示单选2表示多选
    if (optionId == 1) {
      this.curPageData = {
        ...thePateData,
        questions: [
          {
            ...thePateData.questions[0],
            optionType: 1,
            options: thePateData.questions[0]['options'].filter(option => {
              return option.optionId == 112 || option.optionId == 113
            })
          }
        ]
      }
    }
    if (optionId == 2) {
      this.curPageData = {
        ...thePateData,
        questions: [
          {
            ...thePateData.questions[0],
            optionType: 2,
            options: thePateData.questions[0]['options'].filter(option => {
              return (
                option.optionId == 114 ||
                option.optionId == 115 ||
                option.optionId == 117
              )
            })
          }
        ]
      }
    }
    if (optionId == 3) {
      this.curPageData = {
        ...thePateData,
        questions: [
          {
            ...thePateData.questions[0],
            optionType: 2,
            options: thePateData.questions[0]['options'].filter(option => {
              return (
                option.optionId == 114 ||
                option.optionId == 116 ||
                option.optionId == 117
              )
            })
          }
        ]
      }
    }
    if (optionId == 4) {
      this.curPageData = {
        ...thePateData,
        questions: [
          {
            ...thePateData.questions[0],
            optionType: 2,
            options: thePateData.questions[0]['options'].filter(option => {
              return (
                option.optionId == 114 ||
                option.optionId == 115 ||
                option.optionId == 116 ||
                option.optionId == 117
              )
            })
          }
        ]
      }
    }
  }
}
