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: 100000,
        pageNum: 1,
        totalNum: null
    };

    obj = {
        searchStr: '',  //模糊查询字符
        status: '',  //计划状态: 0=未开始, 1=进行中, 2=结束
        typeId: null,  //计划类型ID
        nextExecutionStart: null,  //开始时间
        nextExecutionEnd: 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,
            searchStr: this.obj.searchStr,
            typeId: this.obj.typeId,
            status: this.obj.status,
            nextExecutionStart: this.obj.nextExecutionStart,  //开始时间
            nextExecutionEnd: this.obj.nextExecutionEnd, //结束时间
        };
        this.workSer.findPlanList(obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.page.list = res.data.data;
                    this.page.totalNum = this.page.list.length;
                }
                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(e) {
        if (e == '99') return;
        this.time.type = e;
        this.obj.nextExecutionStart = this.commonSer.getTimeByType(this.time.type).startTime;
        this.obj.nextExecutionEnd = this.commonSer.getTimeByType(this.time.type).endTime;
        this.page.pageNum = 1;
        this.getList();
    }

    //搜索
    search() {
        this.page.pageNum = 1;
        if (this.time.type == '99') {
            this.obj.nextExecutionStart = this.datePipe.transform(this.time.startTime, 'yyyy-MM-dd HH:mm:ss');
            this.obj.nextExecutionEnd = this.datePipe.transform(this.time.endTime, 'yyyy-MM-dd HH:mm:ss');
        }
        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();
                    }
                }
            );
        });
    }
}
