import {Component, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {WorkService} from '../../work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {LinkInventoryComponent} from '../../modal/link-inventory/link-inventory.component';
import {DatePipe} from '@angular/common';
import {TransforComponent} from '../../modal/transfor/transfor.component';

@Component({
    selector: 'smart-handle-event',
    templateUrl: './handle-event.component.html',
    styles: [
        `
           :host ::ng-deep .ant-row{
               margin: 20px 0;
           }
           :host ::ng-deep .ant-col-4{
               text-align: right;
           }
            h2{
                font-weight: 400;
            }
        `
    ]
})
export class HandleEventComponent implements OnInit {
    @ViewChild('smartLinkInventory') smartLinkInventory:LinkInventoryComponent;
    @ViewChild('smartTransfor') smartTransfor:TransforComponent;

    eventId;
    event;
    transforsList;
    linkList;
    format;

    obj = {
        operateStatus:null,
        operateResult:null,
    };
    date;
    time;

    constructor(private routrInfo: ActivatedRoute, private workSer: WorkService,
                private message: NzMessageService,private datePipe:DatePipe) {
        this.routrInfo.queryParams.subscribe(
            (res) => {
                this.eventId = res.eventId;
            }
        );
    }

    ngOnInit() {
        this.getDetail();
        this.findTransferAndInventory();
    }

    //事件详情
    getDetail() {
        this.workSer.findByNo(this.eventId).subscribe(
            (res) => {
                this.event = res.data;
            }
        );
    }

    //查询事件流转信息
    findTransferAndInventory(){
        this.workSer.findTransferAndInventory(this.eventId).subscribe(
            (res)=>{
                    this.transforsList = res.data.transfers;
                    this.transforsList.forEach(e=>{
                        e.operatorInfo = e.operatorInfo.replace(/\"/g,"").replace("{","").replace("}","").split(":");
                    })
            }
        )
    }

    selectPerson(){

    }

    //选择关联弹窗
    showInventoryModal(){
        this.smartLinkInventory.showModal("添加关联资产");
    }

    //选择管理返回值
    setLink(e){
        console.log(e);
        this.linkList = e;
    }

    //保存处理
    save(){
        const data = {
            id:this.eventId,
            operateStatus:Number(this.obj.operateStatus),
            inventoryId:this.linkList.map(e=>{
                return e.id;
            }),
            operateResult:this.obj.operateResult,
            completeTimeStr:this.datePipe.transform(this.date,"yyyy-MM-dd") + " " + this.datePipe.transform(this.time,"HH:mm:ss")
        }
        this.workSer.deal(data).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.message.success("事件处理成功");
                }else{
                    this.message.error(res.errMsg);
                }
            }
        )
    }

    //转派
    showTransforModal(){
        this.smartTransfor.showTransforModal("事件转派",this.eventId);
    }

    //转派成功
    success(e){

    }

}
