import {Component, OnInit, ViewChild} from '@angular/core';
import {TopologyService} from '../topology.service';
import {NzMessageService, NzModalService, NzMode} from 'ng-zorro-antd';
import {IconComponent} from '../model/icon/icon.component';
import {imgUrl, pageSize} from '../../app.constants';

@Component({
    selector: 'smart-topology-img',
    templateUrl: './topology-img.component.html',
    styles: []
})
export class TopologyImgComponent implements OnInit {
    @ViewChild('smartIcon') smartIcon:IconComponent;

    imgUrl = imgUrl;
    iconList;

    pageNum = 1;
    pageCount = pageSize;
    totalNum;
    constructor(private topologySer: TopologyService,private message:NzMessageService,
                private modalSer:NzModalService) {
    }

    ngOnInit() {
        this.getList();
    }

    getList() {
        const data = {
            pageNum:this.pageNum,
            pageCount:this.pageCount
        }
        this.topologySer.iconFindAll(data).subscribe(
            (res) => {
                if(res.errCode == 10000){
                    this.iconList = res.data.data;
                    this.totalNum = res.data.totalNum;
                }else{
                    this.message.error(res.errMsg);
                }
            }
        );
    }

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

    //添加图标
    addIcon(){
        this.smartIcon.showAddModal("添加图标");
    }

    //更换图标
    editIcon(item){
        this.smartIcon.showEditModal("更换图标",item.id);
    }

    //删除图片
    deleteIcon(item){
        this.modalSer.confirm({
                nzTitle: '删除',
                nzContent: '<b style="color: red;">确认删除该图标吗？</b>',
                nzOkText: '确定',
                nzOkType: 'danger',
                nzOnOk: () => {
                    const arr = {
                        ids: []
                    };
                    arr.ids.push(item.id);
                    this.topologySer.iconDelete(arr).subscribe(
                        (res) => {
                            if (res.errCode == 10000) {
                                this.message.success('删除成功');
                                this.getList();
                            } else {
                                this.message.error(res.errMsg);
                            }
                        }
                    );
                },
                nzCancelText: '取消',
                nzOnCancel: () => console.log('Cancel')
            }
        );
    }

    //设为默认
    setDefault(item){
        this.modalSer.confirm({
                nzTitle: '默认',
                nzContent: '<b style="color: red;">确认将该图标设为默认图标吗？</b>',
                nzOkText: '确定',
                nzOkType: 'danger',
                nzOnOk: () => {
                    console.log(item);
                    this.topologySer.iconDefault(item.id).subscribe(
                        (res) => {
                            if (res.errCode == 10000) {
                                this.message.success('默认成功');
                                this.getList();
                            } else {
                                this.message.error(res.errMsg);
                            }
                        }
                    );
                },
                nzCancelText: '取消',
                nzOnCancel: () => console.log('Cancel')
            }
        );
    }
}
