import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {SelectPersonComponent} from '../../../modal/select-person/select-person.component';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {WorkService} from '../../../work/work.service';
import {NzMessageService, UploadFile} from 'ng-zorro-antd';
import {DatePipe} from '@angular/common';
import {ProjectService} from '../../project.service';
import {ProjectModalComponent} from '../project-modal/project-modal.component';
import {LocalStorageService} from 'ngx-webstorage';

@Component({
    selector: 'smart-cost-modal',
    templateUrl: './cost-modal.component.html',
    styles: [
        `
            :host ::ng-deep .ant-input-disabled{
                color: rgba(0,0,0,.65);
                background-color: #fff;
            }
        `
    ]
})
export class CostModalComponent implements OnInit {
    @ViewChild('smartProjectModal') smartProjectModal: ProjectModalComponent;
    @Output() done = new EventEmitter<any>();

    title;
    isVisiable = false;
    isDisabled = false;
    fileList = [];

    obj = {
        projectName: '',
        projectId: '',
        people: '',
        total: 0,
        purchasenum: 0,
        purchasemoney: 0,
        outsourcingnum: 0,
        outsourcingmoney: 0,
        othernum: 0,
        othermoney: 0,
        costdescribe: '',
        time:null
    };

    constructor(private workSer: WorkService, private message: NzMessageService,
                private fb: FormBuilder, private datePipe: DatePipe,
                private projectSer: ProjectService,
                private $localStorage: LocalStorageService) {
    }

    ngOnInit() {
        this.obj.people = this.$localStorage.retrieve('userInfo').loginName;
        this.initForm();
    }

    initForm() {
        this.fileList = [];
        this.isDisabled = false;
    }

    showAddModal() {
        this.title = '添加费用记录';
        this.isVisiable = true;
    }

    handEditleOk() {
        this.create();
    }

    handleCancle() {
        this.isVisiable = false;
    }

    create() {
        this.obj.time = new Date().getTime();
        this.projectSer.addCost(this.obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.initForm();
                    this.isVisiable = false;
                    this.done.emit();
                    this.message.success('新增费用记录成功');
                }
            }
        );
    }

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

    setProject(e) {
        this.obj.projectName = e.name;
        this.obj.projectId = e.id;
        console.log(e);
    }

    countTotal(){
        this.obj.total = this.obj.purchasemoney * this.obj.purchasenum +
            this.obj.outsourcingnum * this.obj.outsourcingmoney +
            this.obj.othernum * this.obj.othermoney;
    }

}
