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) {
        if($event > 0){
            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;
                    }
                }
                this.loading = false;
            }
        );
    }

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

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

    //删除
    deleteFile(item) {
        this.commonSer.confirmThing('删除', '确认删除?', () => {
            const data = {
                ids: [item.id],
            };
            this.workSer.knowledgedDelete(data).subscribe(
                (res) => {
                    this.message.success('删除成功!');
                    this.getList();
                }
            );
        });
    }

    //预览
    preview(item){
        this.commonSer.previewFile(item.view);
    }

}
