import {Component, OnInit, ViewChild} from '@angular/core';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {DatePipe} from '@angular/common';
import {InfomationService} from '../infomation.service';
import {UploadComponent} from '../../work/modal/upload/upload.component';
import {pageSize} from '../../app.constants';
import {CommonService} from '../../shared/common/common.service';
import {asset_type} from '../information.constans';
import {UploadAssetComponent} from '../modal/upload-asset/upload-asset.component';
import {CreatePropertyComponent} from '../modal/create-property/create-property.component';
import {MonitorService} from '../../monitor/monitor.service';
import {WorkService} from '../../work/work.service';

@Component({
    selector: 'smart-asset-manage',
    templateUrl: './asset-manage.component.html',
    styles: []
})
export class AssetManageComponent implements OnInit {
    @ViewChild('smartUploadAsset') smartUploadAsset: UploadAssetComponent;
    @ViewChild('smartCreateProperty') smartCreateProperty: CreatePropertyComponent;

    allChecked = false;
    disabledButton = true;
    indeterminate = false;

    dataSet;
    typeList = asset_type;

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

    obj = {
        type: null,
        classification: null,
        no: null,
        name: null,
        attributionProject: '',
        contacts: '',
    };
    selectList = [];
    modal = {
        isVisible: false,
        title: '',
        modalType: null,
    };

    constructor(private infoSer: InfomationService, private message: NzMessageService,
                private monitorSer: MonitorService,
                private workSer: WorkService,
                private router: Router, private commonSer: CommonService, private datePipe: DatePipe) {
    }

    ngOnInit() {
        this.getList();
    }

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

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

    //获取列表
    getList() {
        this.isLoading = true;
        const obj = {
            pageCount: this.pageCount,
            pageNum: this.pageNum,
            obj: this.obj,
        };
        this.infoSer.propertyPage(obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.dataSet = res.data.data;
                    this.totalNum = res.data.totalNum;
                }
                this.isLoading = false;
            }
        );
    }

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

    //查看详情
    lookEvent(item) {
        this.router.navigate(['app/main/handleDetail'], {
            queryParams: {
                eventId: item.id
            }
        });
    }

    //处理事件
    goToHandle(item) {
        this.router.navigate(['app/main/handleEvent'], {
            queryParams: {
                eventId: item.id
            }
        });
    }

    //新增事件
    showAddModal() {
        this.smartCreateProperty.showAddModal('新增资产');
    }

    //编辑事件
    showEditModal(item) {
        this.smartCreateProperty.showEditModal('编辑资产', item.id);
    }

    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();
    }

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

    //删除资产
    deleteHandle(data) {
        const arr = {
            ids: []
        };
        arr.ids.push(data.id);
        this.commonSer.confirmThing('删除', '确定删除该资产', () => {
            this.infoSer.propertyDelete(arr).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.getList();
                        this.deleteMonitor(data.id);
                    }
                }
            );
        });
    }

    deleteMonitor(id) {
        this.monitorSer.assetDelete(id, null).subscribe(
            (res) => {

            }
        );
    }

    //转派
    showTransforModal(item) {
        // this.smartTransfor.showTransforModal('事件转派', item.id);
    }

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

    //上传资产
    showUploadModal() {
        this.smartUploadAsset.showModal('导入资产');
    }

    //导出资产
    downLoad() {
        this.isDownload = true;
        this.infoSer.propertyExport().subscribe(
            (res) => {
                this.isDownload = false;
                this.commonSer.downloadFile('资产管理.xlsx', res);
            }
        );
    }

    //模版下载
    handleOk() {
        this.isDownload = true;
        if (!this.modal.modalType) {
            this.message.info('请选择需要下载的模版');
            return;
        }
        const item = this.typeList.filter(e => e.value == this.modal.modalType);
        console.log(item);
        this.workSer.dowloadTemplateByProperty(item[0].value).subscribe(
            (res) => {
                this.isDownload = false;
                this.commonSer.downloadFile(`${item[0].label}.xlsx`, res);
            }
        );
    }
}
