import {Component, OnInit, ViewChild} from '@angular/core';
import {WorkService} from '../work.service';
import {pageSize} from '../../app.constants';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {PlanModalComponent} from '../modal/plan-modal/plan-modal.component';
import {LookPlanComponent} from '../modal/look-plan/look-plan.component';

@Component({
    selector: 'smart-inspect-plan',
    templateUrl: './inspect-plan.component.html',
    styles: []
})
export class InspectPlanComponent implements OnInit {
    @ViewChild('smartPlanMoadl') smartPlanMoadl: PlanModalComponent;
    @ViewChild('smartLookPlan') smartLookPlan: LookPlanComponent;

    timeFormat = 'yyyy-MM-dd';
    planList;
    planTypeList;
    timeBegin;
    timeEnd;

    isLoading = false;
    pageCount = pageSize;
    pageNum = 1;
    totalNum;

    obj = {
        pageNum: '',
        pageCount: '',
        searchStr: '',
        typeId: null,
        status: null,
    };

    constructor(private workSer: WorkService, private message: NzMessageService,
                private modalSer: NzModalService) {
    }

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

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

    getList() {
        this.isLoading = true;
        const obj = {
            pageNum: this.pageNum,
            pageCount: this.pageCount,
            searchStr: this.obj.searchStr,
            typeId: this.obj.typeId,
            status: this.obj.status,
        };
        this.workSer.findPlanList(obj).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.planList = res.data.data;
                    this.totalNum = res.data.totalNum;
                }
                this.isLoading = false;
            }
        );
    }

    change(e) {
        this.pageNum = e;
        this.getList();
    }

    search(){
        this.pageNum = 1;
        this.getList();
    }

    //新增计划
    addPlan() {
        this.smartPlanMoadl.showAddModal();
    }

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

    //查看计划
    lookPlan(id,name){
        this.smartLookPlan.showModal(id,name);
    }

    //删除计划
    deletePlan(data) {
        this.modalSer.confirm({
                nzTitle: '删除',
                nzContent: '<b style="color: red;">确认删除该计划吗？</b>',
                nzOkText: '确定',
                nzOkType: 'danger',
                nzOnOk: () => {
                    const arr = {
                        ids: []
                    };
                    arr.ids.push(data.id);
                    this.workSer.deletePlan(arr).subscribe(
                        (res) => {
                            if (res.errCode == 10000) {
                                this.message.success('删除成功');
                                this.search();
                            }
                        }
                    );
                },
                nzCancelText: '取消',
                nzOnCancel: () => console.log('Cancel')
            }
        );
    }
}
