import {Component, OnInit} from '@angular/core';
import {WorkService} from '../../work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ActivatedRoute} from '@angular/router';
import {CommonService} from '../../../shared/common/common.service';
import {Location} from '@angular/common';
import {LocalStorageService} from 'ngx-webstorage';

@Component({
    selector: 'smart-handle-detail',
    templateUrl: './handle-detail.component.html',
    styles: [
            `
            :host ::ng-deep .ant-row {
                margin: 20px 0;
            }

            :host ::ng-deep .ant-col-4 {
                text-align: right;
            }

            h2 {
                font-weight: 400;
            }

            .anticon-star {
                color: #ea5151;
            }
        `
    ]
})
export class HandleDetailComponent implements OnInit {

    eventId;
    event;
    transforsList;
    obj = {
        confirmStatus: '',
        score: 0,
        suggest: ''
    };
    userName;

    starList = ['anticon-star-o', 'anticon-star-o', 'anticon-star-o', 'anticon-star-o', 'anticon-star-o'];

    constructor(private workSer: WorkService, private message: NzMessageService,
                private location: Location,
                private $localStorage: LocalStorageService,
                private routrInfo: ActivatedRoute, private commonSer: CommonService) {
        this.routrInfo.queryParams.subscribe(
            (res) => {
                this.eventId = res.eventId;
            }
        );
    }

    ngOnInit() {
        this.userName = this.$localStorage.retrieve('userInfo').userName;
        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(':');
                });
            }
        );
    }

    //anticon-star-o:空心  anticon-star:实心
    checkStar(number) {
        console.log(number);
        this.obj.score = number + 1;
        for (let i = 0; i < this.starList.length; i++) {
            if (i < number + 1) {
                this.starList[i] = 'anticon-star';
            } else {
                this.starList[i] = 'anticon-star-o';
            }
        }
    }

    //评价
    //确认完成
    done() {
        console.log(this.obj);
        if (this.obj.score == 0 || !this.obj.confirmStatus) {
            this.message.info('请输入必填项');
            return;
        }
        const data = {
            id: this.eventId,
            confirmStatus: this.obj.confirmStatus,
            score: this.obj.score,
            suggest: this.obj.suggest,
        };
        this.workSer.confirmEvent(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.location.back();
                    this.message.success('事件确认成功');
                }
            }
        );
    }

    //下载文件
    downLoad() {
        this.workSer.downloadTemplate('event', this.eventId).subscribe(
            (data) => {
                this.commonSer.downloadFile(this.event.fileName, data);
            }
        );
    }

}
