import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {WorkService} from '../../work.service';
import {NzMessageService, UploadFile} from 'ng-zorro-antd';

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

    planList;
    planId;
    title;
    isVisiable = false;
    validateForm: FormGroup;
    fileList: UploadFile[] = [];

    constructor(private workSer: WorkService, private message: NzMessageService,
                private fb: FormBuilder) {
    }

    ngOnInit() {
        this.initForm();
        this.getPlanType();
    }

    initForm() {
        this.validateForm = this.fb.group({
            title: [null],
            typeId: [null],
            startTime: [null],
            endTime: [null],
            cycleType: [null],
            cycleNum: [null],
            number: [null],
            workload: [null],
            description: [null],
        });
    }

    getPlanType() {
        const data = {
            type: 1
        };
        this.workSer.findByType(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.planList = res.data;
                }
            }
        );
    }

    beforeUpload = (file: UploadFile): boolean => {
        const isLt5M = file.size / 1024 < 5000;
        if (!isLt5M) {
            this.message.error(' 文件必须小于5M!');
        } else {
            this.fileList.push(file);
        }
        return false;
    };

    showAddModal() {
        this.title = '添加计划';
        this.isVisiable = true;
    }

    showEditModal(id) {
        this.title = '编辑计划';
        this.planId = id;
        this.isVisiable = true;
    }

    handEditleOk() {
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if (this.validateForm.invalid) {
            return false;
        }
        if (this.title == '添加计划') {
            this.create();
        }
        if (this.title = '编辑计划') {
            this.update();
        }
    }

    create() {
        let formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
        formData.append('json', JSON.stringify(this.validateForm.value));
        this.workSer.createPlan(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.initForm();
                    this.isVisiable = false;
                    this.message.success('新增计划成功');
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    update() {
        let formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
        formData.append('json', JSON.stringify(this.validateForm.value));
        formData.append('id', this.planId);
        this.workSer.createPlan(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('修改计划成功');
                    this.initForm();
                    this.isVisiable = false;
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    handleEditCancel() {
        this.isVisiable = false;
    }

}
