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-management-system',
    templateUrl: './management-system.component.html',
    styles: []
})
export class ManagementSystemComponent 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: 2
            }
        };
        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);
    }

}
