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;

    obj = <any>{
        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.isDisabled = false;
        this.obj.people = this.$localStorage.retrieve('userInfo').userName;
    }

    initForm() {
        this.isDisabled = false;
        this.obj = {
            projectName: null,
            projectId: null,
            total: 0,
            people: this.$localStorage.retrieve('userInfo').userName,
            purchasenum: 0,
            purchasemoney: 0,
            outsourcingnum: 0,
            outsourcingmoney: 0,
            othernum: 0,
            othermoney: 0,
            costdescribe: '',
            time: null
        };
        console.log(this.obj);
    }

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

    showLookModal(data) {
        this.title = '费用记录';
        this.isVisiable = true;
        for (let k in data) {
            this.obj[k] = data[k];
        }
    }

    handEditleOk() {
        if (!this.obj.projectName || !this.obj.projectId) {
            this.message.info('请输入必选项!');
            return;
        }
        this.create();
    }

    handleCancle() {
        this.isVisiable = false;
        this.initForm();
    }

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

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

}
