import {Component, OnInit, ViewChild} from '@angular/core';
import {PlanModalComponent} from '../modal/plan-modal/plan-modal.component';
import {LookPlanComponent} from '../modal/look-plan/look-plan.component';
import {pageSize, SERVER_API_URL} from '../../app.constants';
import {WorkService} from '../work.service';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {CommonService} from '../../shared/common/common.service';
import {SparePartsComponent} from '../modal/spare-parts/spare-parts.component';
import {UploadComponent} from '../modal/upload/upload.component';
import {SystemService} from '../../system/system.service';

@Component({
    selector: 'smart-spare-parts-management',
    templateUrl: './spare-parts-management.component.html',
    styles: []
})
export class SparePartsManagementComponent implements OnInit {
    @ViewChild('smartSpareParts') smartSpareParts: SparePartsComponent;
    @ViewChild('smartLookPlan') smartLookPlan: LookPlanComponent;
    @ViewChild('smartUpload') smartUpload: UploadComponent;

    dataSet;

    isLoading = false;
    pageCount = pageSize;
    pageNum = 1;
    totalNum;

    obj = {
        keyword: '',
    };

    constructor(private workSer: WorkService, private message: NzMessageService,
                private modalSer: NzModalService,
                private systemSer:SystemService,
                private commonSer: CommonService) {
    }

    ngOnInit() {
        this.getList();
    }

    getList() {
        this.isLoading = true;
        const obj = {
            pageNum: this.pageNum,
            pageCount: this.pageCount,
            obj: this.obj,
        };
        this.workSer.selectComponentsList(obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.dataSet = res.data.data;
                    this.totalNum = res.data.totalNum;
                }
                this.isLoading = false;
            }
        );
    }

    //翻页
    change(e) {
        if (e > 0) {
            this.pageNum = e;
            this.getList();
        }
    }

    search() {
        this.pageNum = 1;
        this.getList();
    }

    //入库登记
    addData() {
        this.smartSpareParts.showAddModal('入库登记');
    }

    //编辑计划
    editData(id) {
        this.smartSpareParts.showEditModa('编辑信息',id);
    }

    //查看计划
    lookPlan(id, name) {
        this.smartLookPlan.showModal(id, name);
    }

    //删除
    deleteData(data) {
        this.commonSer.confirmThing('提示', `确定删除该备件`, () => {
            const d = {
                id:data.id
            }
            this.workSer.deleteComponents(d).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.search();
                    }
                }
            );
        });
    }

    //下载模版
    downLoad(){
        this.systemSer.downloadTemplate("components",SERVER_API_URL + "/api/template/download/").subscribe(
            (data)=>{
                this.commonSer.downloadFile('备件管理模版.xlsx',data);
            }
        )
    }

    //上传文件
    uploadFile(){
        this.smartUpload.showModal('导入备件管理数据');
    }

}
