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

@Component({
    selector: 'smart-create-company',
    templateUrl: './create-company.component.html',
    styles: [
        `
            :host ::ng-deep .ant-input-disabled{
                color: rgba(0,0,0,.65);
                background-color: #fff;
            }
        `
    ]
})
export class CreateCompanyComponent implements OnInit {
    @ViewChild('smartTrackInfo') smartTrackInfo:TrackInfoComponent;

    @Output() done = new EventEmitter<any>();
    tabNum = 0;    //tabs面板的序列号

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

    cID;

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

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

    initForm() {
        this.validateForm = this.fb.group({
            name: [null, [Validators.required, Validators.minLength(2), Validators.maxLength(50)]],
            principal: [null],
            principalId: [null],
            type: ["1", [Validators.required]],
            phone: [null],
            phone2: [null],
            description: [null],
        });
    }

    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();
    }

    beforeUpload = (file: UploadFile): boolean => {
        const isExcel = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || 'application/octet-stream';
        if (!isExcel) {
            this.message.error('请上传正确的文件!');
        } else {
            this.fileList[0] = file;
        }
        return false;
    };

    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.isOkLoading = true;
        if (this.title == '新增运维公司') {
            this.create();
        }
        if (this.title == '编辑运维公司') {
            this.update();
        }
    }

    create() {
        this.projectSer.addMaintainCompany(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.initForm();
                    this.done.emit();
                    this.message.success('新增运维公司成功');
                }
                this.isOkLoading = false;
            }
        );
    }

    update() {
        this.validateForm.addControl('id', new FormControl(this.cID));
        this.projectSer.updateMaintainCompany(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.initForm();
                    this.done.emit();
                    this.message.success('更新信息成功');
                }
                this.isOkLoading = false;
            }
        );
    }

    //tabs切换
    tabsChange(num) {
        this.tabNum = num;
    }

}
