import {Component, OnInit, ViewChild} from '@angular/core';
import {pageSize} from '../../app.constants';
import {WorkService} from '../work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {EventComponent} from './Event/event.component';
import {CommonService} from '../../shared/common/common.service';
import {SelectPersonComponent} from '../../modal/select-person/select-person.component';
import {TransforComponent} from '../modal/transfor/transfor.component';
import {DatePipe} from '@angular/common';

@Component({
    selector: 'smart-work-handle',
    templateUrl: './work-handle.component.html',
    styles: []
})
export class WorkHandleComponent implements OnInit {
    @ViewChild('smartEvent') smartEvent: EventComponent;
    @ViewChild('smartTransfor') smartTransfor: TransforComponent;
    @ViewChild('smartSelectPerson') smartSelectPerson: SelectPersonComponent;

    timeFormat = 'yyyy-MM-dd';
    timeType = '1';
    eventList;
    eventTypeList;
    startTime;
    endTime;
    pageCount = pageSize;
    pageNum = 1;
    totalNum;

    obj = {
        pageCount: null,
        pageNum: null,
        searchStr: '',
        type: null,
        status: null,
        source: null,
        startTime: '',
        endTime: '',
    };

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

    ngOnInit() {
        this.getList();
        this.getEventType();
    }

    //获取事件分类
    getEventType() {
        const data = {
            type: 0
        };
        this.workSer.findParamsByType(data).subscribe(
            (res) => {
                this.eventTypeList = res.data;
            }
        );
    }

    //获取列表
    getList() {
        const obj = {
            pageCount: this.pageCount,
            pageNum: this.pageNum,
            searchStr: this.obj.searchStr,
            type: this.obj.type,
            status: this.obj.status,
            source: this.obj.source,
            startTime: this.obj.startTime,
            endTime: this.obj.endTime,
        };
        this.workSer.find(obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.eventList = res.data.data;
                    this.totalNum = res.data.totalNum;
                }
            }
        );
    }

    //翻页
    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.smartEvent.showAddModal('新增事件');
    }

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

    //删除事件
    deleteHandle(data) {
        const arr = {
            ids: []
        };
        arr.ids.push(data.id);
        this.commonSer.confirmThing('删除', '确定删除该事件', () => {
            this.workSer.deleteEvent(arr).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.getList();
                    } else {
                        this.message.error(res.errMsg);
                    }
                }
            );
        });
    }

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

    //搜索
    search() {
        this.obj.startTime = this.datePipe.transform(this.startTime, 'yyyy-MM-dd HH:mm:ss');
        this.obj.endTime = this.datePipe.transform(this.endTime, 'yyyy-MM-dd HH:mm:ss');

        this.getList();
    }
}
