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';
import {CommonService} from '../../shared/common/common.service';

@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 = 100000;
    pageNum = 1;
    totalNum;

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

    modal = {
        isVisible: false,
        isOkLoading: false,
        planId: '',
        processResult: '0',
        processRemark: ''
    };

    constructor(private workSer: WorkService, private message: NzMessageService,
                private commonSer: CommonService,
                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 = this.planList.length;
                }
                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')
            }
        );
    }

    //处理计划
    dealPlan(id) {
        this.modal.planId = id;
        this.modal.isVisible = true;
    }

    //取消
    handleCancel() {
        this.modal.isVisible = false;
        this.modal.planId = '';
        this.modal.processResult = '';
        this.modal.processRemark = '';
    }

    //确定
    handleOk() {
        if (!this.modal.processRemark) {
            this.message.error('请输入必填项');
            return
        }
        const data = {
            planId: this.modal.planId,
            processResult: this.modal.processResult,
            processRemark: this.modal.processRemark,
        };
        this.modal.isOkLoading = true;
        this.workSer.processPlan(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('处理成功');
                    this.handleCancel();
                    this.search();
                }
                this.modal.isOkLoading = false;
            }
        );
    }
}
