import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {NzMessageService, UploadFile} from 'ng-zorro-antd';
import {opr_company_type} from '../../../project-manager/project.constants';
import {SystemService} from '../../../system/system.service';
import {ProjectService} from '../../../project-manager/project.service';
import {ProjectModalComponent} from '../../../project-manager/modal/project-modal/project-modal.component';

@Component({
    selector: 'smart-violation-notification',
    templateUrl: './violation-notification.component.html',
    styles: []
})
export class ViolationNotificationComponent implements OnInit {

    @ViewChild('smartProjectModal') smartProjectModal: ProjectModalComponent;
    @Output() done = new EventEmitter<any>();

    title;
    isVisible = false;
    isOkLoading = false;
    validateForm: FormGroup;
    fileList: UploadFile[] = [];
    opr_company_type = opr_company_type;

    cID;
    userList;

    constructor(private fb: FormBuilder, private systemSer: SystemService,
                private message: NzMessageService, private projectSer: ProjectService) {
    }

    ngOnInit(): void {
        this.initForm();
        this.getUser();
    }

    initForm() {
        this.validateForm = this.fb.group({
            title: [null],
            projectId: [null],
            projectName: [null],
            content: [null],
            users: [[]],
        });
    }

    //获取人员
    getUser() {
        const data = {
            'pageNum': '1',
            'pageSize': '1000'
        };
        this.systemSer.user(data).subscribe(
            (res) => {
                this.userList = res.data.data;
            }
        );
    }

    showAddModal(title) {
        this.isVisible = true;
        this.title = title;
    }

    showEditModal(data, title) {
        this.isVisible = true;
        data.type += '';
        this.title = title;
        this.cID = data.id;
        console.log(data);
        this.validateForm.patchValue(data);
    }

    handleCancel() {
        this.isVisible = false;
        this.initForm();
    }

    handleOk() {
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if (this.validateForm.invalid) {
            return false;
        }
        this.done.emit(this.validateForm.value);
        this.isVisible = false;
        this.initForm();
    }

    // 选择的项目
    showProjectmodal() {
        this.smartProjectModal.showModal();
    }

    //获取选择的项目
    getProject(e) {
        const d = {
            projectId: e.id,
            projectName: e.name
        };
        this.validateForm.patchValue(d);
    }

}
