import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { MainService } from 'src/app/services/main.service';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-preview',
  templateUrl: './preview.component.html',
  styleUrls: ['./preview.component.less']
})
export class PreviewComponent implements OnInit {
  validateForm: FormGroup;
  qm:any = {};
  loading:boolean = false;
  failure:boolean = false;
  listOfData:any[] = [];
  success:boolean = false;
  isM:boolean = false;
	pastStatus:boolean = false;
	gdid:string = '';
	qrCodeIndex:string = '';
  constructor(
    private route: ActivatedRoute,
    private mainService:MainService,
    private fb: FormBuilder,
    private message: NzMessageService,
  ) {}

  ngOnInit() {
    this.isM = this.isMobile();
    this.initForm();
    if(this.isM){
      document.documentElement.style.fontSize = '13.33333vw';
    }else{
      document.documentElement.style.fontSize = '100px';
    }
		console.log(234,this.route.snapshot.queryParams)
		this.gdid = this.route.snapshot.queryParams.gdId;
		this.qrCodeIndex = this.route.snapshot.queryParams.qrCodeIndex
		if(this.gdid){
			this.verifyPast(this.gdid)
		}
    this.getList();
  }
  // 初始化Form
  initForm(){
    this.validateForm = this.fb.group({});
  }
  // 提交
  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.invalid) {
      return;
    }
    console.log(this.validateForm.value);
    const postData:any ={
      qmId: this.qm.id,
      answerList: Object.entries(this.validateForm.value).map(v=>({
        managementId: this.qm.id,
        infoId: v[0],
        answer: v[1],
        hosId: this.qm.hosId,
      }))
    };
		if(this.gdid){
			postData.gdId = this.gdid
		}else{
			delete postData.gdId
		}
    const mid = this.message.loading('loading..', { nzDuration: 0 }).messageId;
    this.mainService.surveyCommit(postData).subscribe((res:any)=>{
      this.message.remove(mid);
      if (res.status == 200) {
        this.message.create('success', `提交成功`);
        this.success = true;
      }else{
        this.message.create('error', res.msg || `提交失败`);
      }
    })
  }
  // 判断是否是移动端
  isMobile(){
      const reg = /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i;
      return reg.test(navigator.userAgent);
  }
	// 验证工单问卷调查二维码是否过期
	verifyPast(gdid){
		this.mainService.verifyPastApi({
			gdId:gdid,
			qrCodeIndex:this.qrCodeIndex
		}).subscribe((res:any)=>{
		  if (res.status == 200) {
		    this.pastStatus = false;
		  }else{
				this.pastStatus = true;
		    // this.message.create('error',res.info,{ nzDuration: 5000 });
		  }
		})
	}
  // 问卷数据
  getList() {
    let token = this.route.snapshot.queryParams.token;
    if(token){
      let postData = {};
      this.loading = true;
      this.mainService
      .managerQuestionnairePreview(postData,token)
      .subscribe((data:any) => {
        this.loading = false;
        if (data.status == 200) {
          this.qm = data.data.qm;
          let list = data.data.qi.map((v,i)=>({...v,propName:v.id,radioItems:[v.option1,v.option2,v.option3,v.option4,v.option5,v.option6,v.option7,v.option8,v.option9,v.option10]}));
          list.forEach(v=>{
            v.radioItems = v.radioItems.filter(Boolean);
            this.validateForm.addControl(v.propName,new FormControl(null, Validators.required));
          })
          this.listOfData = list;
        }else{
          this.failure = true;
        }
      });
    }else{
      this.failure = true;
    }
  }
}
