import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { CommonCompModule } from '../../../../services/common.modules';
import { TextbookComponent } from '../../../../app/textbook/textbook.component';

import Parse from 'parse';
@Component({
  selector: 'app-export-file',
  templateUrl: './export-file.component.html',
  styleUrls: ['./export-file.component.scss'],
  imports: [CommonCompModule,TextbookComponent],
  standalone: true,
})
export class ExportFileComponent implements OnInit {
  @ViewChild('textbook') textbook: ElementRef | any;
  @Output() change: EventEmitter<any> = new EventEmitter<any>();
  @Input('eduProcess') eduProcess?: Parse.Object;

  starCount: number = 0;
  otherCount: number = 0;
  allCount: number = 0;
  tbookId: any;
  filterObj: any = {
    isCheck: false,
    status: ['400'],
    btns: {},
  };

  constructor(private router: Router, private activeRoute: ActivatedRoute) {}

  ngOnInit() {
    this.activeRoute.paramMap.subscribe(async (params) => {
      let id = params.get('id');
      this.tbookId = id;
      // let query = new Parse.Query('EduProcess');
      // query.include('branch', 'department');
      // query.equalTo('objectId', id);
      // this.eduProcess = await query.first();
      console.log(id);
      let query = new Parse.Query('EduTextbook');
      query.notEqualTo('isDeleted', true);
      query.equalTo('recommend', true);
      query.equalTo('eduProcess', id);
      this.allCount = await query.count();
      query.containedIn('approval', [
        '首届全国教材建设奖优秀教材（高等教育类）教材',
        '否',
      ]);
      this.otherCount = await query.count();
    });
  }
  close() {
    history.back();
  }
  next() {
    this.change.emit('next');
  }
  onDownload() {
    Parse.Cloud.run('tbookExportReport', { processId: this.tbookId }).then(
      (data) => {
        console.log(data);
        let url = data.zipUrl;
        url = url.replaceAll("http://","https://");
        const a = document.createElement('a'); // 创建一个&lt;a&gt;元素
        a.href = url; // 设置链接的href属性为要下载的文件的URL
        a.download = '报送流程'; // 设置下载文件的名称
        document.body.appendChild(a); // 将&lt;a&gt;元素添加到文档中
        a.click(); // 模拟点击&lt;a&gt;元素
        document.body.removeChild(a); // 下载后移除&lt;a&gt;元素
      }
    );
  }
}
