import {Component, OnInit, ViewChild} from '@angular/core';
import {UploadComponent} from '../../work/modal/upload/upload.component';
import {pageSize} from '../../app.constants';
import {WorkService} from '../../work/work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {CommonService} from '../../shared/common/common.service';

@Component({
    selector: 'smart-common-problem',
    templateUrl: './common-problem.component.html',
    styles: []
})
export class CommonProblemComponent implements OnInit {
    @ViewChild('smartUpload') smartUpload: UploadComponent;

    dataSet = [];
    pageNum = 1;
    pageCount = pageSize;
    totalNum;
    loading = false;

    constructor(private workSer: WorkService, private message: NzMessageService,
                private commonSer: CommonService) {
    }

    ngOnInit() {
        this.getList();
    }

    change($event) {
        this.pageNum = $event;
        this.getList();
    }

    getList() {
        this.loading = true;
        const data = {
            pageNum: this.pageNum,
            pageCount: this.pageCount,
            obj:{
                flag:0
            }
        };
        this.workSer.knowledgedFindPage(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (res.data) {
                        this.dataSet = res.data.data;
                        this.totalNum = res.data.totalNum;
                    }
                } else {
                    this.message.info(res.errMsg);
                }
                this.loading = false;
            }
        );
    }

    //导入
    importFile() {
        this.smartUpload.showModal('上传常见问题文件');
    }

    //下载
    download(item) {
        this.workSer.knowledgedDownload(item.id).subscribe(
            (data) => {
                this.commonSer.downloadFile(item.name, data);
            }
        );
    }

}
