import {Component, OnInit, ViewChild} from '@angular/core';
import {AlarmService} from '../alarm.service';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {MediaTypeComponent} from '../modal/media-type/media-type.component';
import {color} from '../../app.constants';

@Component({
    selector: 'smart-send-set',
    templateUrl: './send-set.component.html',
    styles: []
})
export class SendSetComponent implements OnInit {
    @ViewChild('smartMediaType') smartMediaType: MediaTypeComponent;

    color = color;
    isLoading = false;
    sendList = [];

    constructor(private alarmSer: AlarmService, private modalSer: NzModalService,
                private message: NzMessageService,private modalService:NzModalService) {
    }

    ngOnInit() {
        this.getList();
    }

    getList() {
        this.isLoading = true;
        this.alarmSer.mediaTypeFind({}).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.sendList = res.data;
                }
                this.isLoading = false;
            }
        );
    }

    //删除发送方式
    deleteSend(data) {
        this.modalSer.confirm({
                nzTitle: '删除',
                nzContent: '<b style="color: red;">确认删除该发送方式吗？</b>',
                nzOkText: '确定',
                nzOkType: 'danger',
                nzOnOk: () => {
                    const arr = {
                        ids: []
                    };
                    arr.ids.push(data.mediatypeid);
                    this.alarmSer.mediaTypeDelete(arr).subscribe(
                        (res) => {
                            if (res.errCode == 10000) {
                                this.message.info('删除成功');
                                this.getList();
                            } else {
                                this.message.info(res.errMsg);
                            }
                        }
                    );
                },
                nzCancelText: '取消',
                nzOnCancel: () => console.log('Cancel')
            }
        );
    }

    //编辑
    showEditModal(item) {
        this.smartMediaType.showEditModal(item, '编辑发送方式');
    }

    //新增发送方式
    showAddModal() {
        this.smartMediaType.showAddModal('新增发送方式');
    }

    //启用or禁用
    operationSend(title,status,item){
        this.modalService.confirm({
            nzTitle: title,
            nzContent: '<b style="color: red;">确定要' + title + '该发送方式吗</b>',
            nzOkText: '确定',
            nzOkType: 'danger',
            nzOnOk: () => {
                this.alarmSer.mediaTypeStatus(item.mediatypeid, status).subscribe(
                    (res) => {
                        if (res.errCode == 10000) {
                            this.message.info(title + '成功');
                            this.getList();
                        } else {
                            this.message.info(res.errMsg);
                        }
                    }
                );
            },
            nzCancelText: '取消',
            nzOnCancel: () => console.log('Cancel')
        });
    }
}
