import {Component, OnInit, ViewChild} from '@angular/core';
import {WorkService} from '../../work.service';
import {ActivatedRoute, Router} from '@angular/router';
import {CommonService} from '../../../shared/common/common.service';
import {NzMessageService} from 'ng-zorro-antd';
import {AssetsComponent} from '../../modal/assets/assets.component';
import {UploadComponent} from '../../modal/upload/upload.component';
import {SERVER_API_URL} from '../../../app.constants';
import {SystemService} from '../../../system/system.service';

@Component({
    selector: 'smart-child-assets',
    templateUrl: './child-assets.component.html',
    styles: []
})
export class ChildAssetsComponent implements OnInit {
    @ViewChild('smartAssets') smartAssets:AssetsComponent;
    @ViewChild('smartUpload') smartUpload:UploadComponent;

    childrenList;
    childName;
    isLoading = false;
    isDownload = false;

    allChecked = false;
    selectList = [];
    disabledButton = true;
    indeterminate = false;

    obj = {
        inventoryTypeid:null,
        name:null,
        inventorycount:null,
        mac:null,
        person:null,
    };

    page = {
        pageNum: 1,
        totalNum:null,
        pageCount:10,
    }

    constructor(private workSer: WorkService, private routerInfo: ActivatedRoute,private router:Router,
               private message:NzMessageService,private systemSer:SystemService,
                private commonSer:CommonService) {
        this.routerInfo.queryParams.subscribe(
            (res) => {
                this.obj.inventoryTypeid = res.id;
                this.childName = res.name
            }
        );
    }

    checkAll(value: boolean): void {
        this.childrenList.forEach(data => {
            data.checked = value;
            this.selectList.push(data);
        })
        this.refreshStatus();
    }

    currentPageDataChange($event: Array<{ checked: boolean }>): void {
        this.childrenList = $event;
    }

    selectItem(item, e) {
        if (e) {
            this.selectList.push(item);
        } else {
            this.selectList.forEach((value, index) => {
                if (value.id == item.id) {
                    this.selectList.splice(index, 1);
                }
            });
        }
        this.refreshStatus();
    }

    refreshStatus(): void {
        const allChecked = this.childrenList.every(value => value.checked === true);
        const allUnChecked = this.childrenList.every(value => !value.checked);
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
    }

    ngOnInit() {
        this.getList();
    }

    getList() {
        this.isLoading = true;
        const data = {
            pageCount:"10",
            pageNum:this.page.pageNum,
            obj:this.obj
        }
        this.workSer.findInventoryPage(data).subscribe(
            (res) => {
                this.childrenList = res.data.data;
                this.isLoading = false;
                this.page.totalNum = res.data.totalNum;
            }
        );
    }

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

    change(e){
        if(e > 0){
            this.page.pageNum = e;
            this.getList();
        }
    }

    //删除资产--单个
    deleteInVentory(item){
        this.isLoading = true;
        const data = {
            inventoryIds:[]
        };
        data.inventoryIds.push(item.id);
        this.commonSer.confirmThing("删除","确定删除该资产？",()=>{
            this.workSer.deleteInventory(data).subscribe(
                (res)=>{
                    if(res.errCode == 10000){
                        this.getList();
                        this.message.success("删除资产成功");
                    }
                    this.isLoading = false;
                }
            )
        })
    }

    //批量删除
    batchDeleteInventory(){
        if(this.selectList.length == 0){
            this.message.warning("请选择需要删除的资产");
            return false;
        }
        this.isLoading = true;
        const data = {
            inventoryIds:this.selectList.map(e=>{
                return e.id;
            })
        };
        this.commonSer.confirmThing("批量删除","确定删除选择的资产？",()=>{
            this.workSer.deleteInventory(data).subscribe(
                (res)=>{
                    if(res.errCode == 10000){
                        this.getList();
                        this.message.success("删除资产成功");
                    }
                    this.isLoading = false;
                }
            )
        })
    }

    //添加资产
    showAddModal() {
       this.smartAssets.showAddModal("添加资产");
    }

    //编辑资产
    showEditModal(id){
        this.smartAssets.showEditModal("编辑资产",id);
    }

    //查看详情
    goToDetail(data){
        this.router.navigate(['app/main/assetsDetail'],{
            queryParams:{
                invertoryId:data.id
            }
        })
    }

    //导入资产
    showUploadModal(){
        this.smartUpload.showModal("上传资产文件");
    }

    //下载模版
    downLoad(){
        this.systemSer.downloadTemplate("inventory",SERVER_API_URL + "/api/template/download/").subscribe(
            (data)=>{
                this.commonSer.downloadFile("资产模版.xlsx",data);
            }
        )
    }

    //导出资产
    downloadInventory(){
        let title;
        title = this.selectList.length == 0? "确定导出所有的资产？":"确定导出所选择的资产";
        this.commonSer.confirmThing("导出",title,()=>{
            this.isDownload = true;
            const data = {
                type:this.obj.inventoryTypeid,
                ids: this.selectList.map(e=>{
                    return e.id;
                }),
                query_name:null
            };
            this.workSer.getExportUrl(data).subscribe(
                (res)=>{
                    this.isDownload = false;
                    this.workSer.downloadTemplate("inventory",res.data).subscribe(
                        (data)=>{
                            this.commonSer.downloadFile("文件.xlsx",data)
                        }
                    )
                }
            )
        });
    }
}
