import {Component, OnInit, ViewChild} from '@angular/core';
import {CostModalComponent} from '../../project-manager/modal/cost-modal/cost-modal.component';
import {opr_cost_type, opr_time_type, project_status} from '../../project-manager/project.constants';
import {pageSize} from '../../app.constants';
import {WorkService} from '../work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ProjectService} from '../../project-manager/project.service';
import {Router} from '@angular/router';
import {CommonService} from '../../shared/common/common.service';
import {DatePipe} from '@angular/common';
import {opr_time_plan_type, plan_type} from '../work.constants';
import {PlanConfigComponent} from '../modal/plan-config/plan-config.component';

@Component({
    selector: 'smart-inspect-plan-config',
    templateUrl: './inspect-plan-config.component.html',
    styles: []
})
export class InspectPlanConfigComponent implements OnInit {
    @ViewChild('smartPlanConfig') smartPlanConfig: PlanConfigComponent;
    timeFormat = 'yyyy-MM-dd';

    statusList = project_status;
    timeType = [{label: '全部', value: null}, ...opr_time_plan_type];
    time = {
        startTime: '',
        endTime: '',
        type: null
    };

    page = {
        list: [],
        isLoading: false,
        pageCount: pageSize,
        pageNum: 1,
        totalNum: null
    };

    obj = {
        people: '',  //填报人
        name: '',  //费用名称
        type: null,  //费用类型
        startTime: null,  //开始时间
        endTime: null, //结束时间
        typeId: null
    };
    planTypeList;

    constructor(private workSer: WorkService, private message: NzMessageService, private projectSer: ProjectService,
                private router: Router, private commonSer: CommonService, private datePipe: DatePipe) {
    }

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

    //获取列表
    getList() {
        this.page.isLoading = true;
        const obj = {
            pageNum: this.page.pageNum,
            pageCount: this.page.pageCount
        };
        this.workSer.findPlanList(obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.page.list = res.data.data;
                    this.page.totalNum = res.data.totalNum;
                    this.page.isLoading = false;
                }
            }
        );
    }

    //获取计划分类
    getPlanType() {
        const data = {
            type: 1
        };
        this.workSer.findByType(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.planTypeList = res.data;
                }
            }
        );
    }

    //翻页
    change(e) {
        if (e > 0) {
            this.page.pageNum = e;
            this.getList();
        }
    }

    changeTimeType() {
        if (this.time.type == '99') return;
        this.obj.startTime = this.commonSer.getTimeByType(this.time.type).startTime;
        this.obj.endTime = this.commonSer.getTimeByType(this.time.type).endTime;
        this.page.pageNum = 1;
        this.getList();
    }

    //搜索
    search() {
        this.page.pageNum = 1;
        this.getList();
    }

    handleOpenModal() {
        this.smartPlanConfig.showAddModal();
    }

    //编辑计划
    editPlan(id) {
        this.smartPlanConfig.showEditModal(id);
    }

    //删除计划
    deletePlan(data) {
        this.commonSer.confirmThing('删除', '确认删除该计划吗？', () => {
            const arr = {
                ids: []
            };
            arr.ids.push(data.id);
            this.workSer.deletePlan(arr).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.search();
                    }
                }
            );
        });
    }
}
