import {Component, OnInit, ViewChild} from '@angular/core';
import {ChangeCreateComponent} from '../../system-change/modal/change-create/change-create.component';
import {ChangeVerityComponent} from '../../system-change/modal/change-verity/change-verity.component';
import {project_status} from '../../project-manager/project.constants';
import {change_status, change_time_type} from '../../system-change/system-change.constants';
import {pageSize} from '../../app.constants';
import {WorkService} from '../../work/work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {SystemChangeService} from '../../system-change/system-change.service';
import {Router} from '@angular/router';
import {CommonService} from '../../shared/common/common.service';
import {DatePipe} from '@angular/common';
import {OverviewService} from '../overview.service';
import {msg_status, time_type} from '../overview.constans';

@Component({
    selector: 'smart-work-reminder',
    templateUrl: './work-reminder.component.html',
    styles: []
})
export class WorkReminderComponent implements OnInit {
    timeFormat = 'yyyy-MM-dd';

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

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

    obj = {
        keyword: '',  //填报人
        updateSearch: null,  //费用类型
        status: null,  //费用类型
        createTimeStart: null,  //开始时间
        createTimeEnd: null //结束时间
    };

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

    ngOnInit() {
        this.getList();
    }

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

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

    changeTimeType() {
        if (this.time.type == '99') return;
        if (!this.time.type) {
            this.obj.createTimeStart = null;
            this.obj.createTimeEnd = null;
        } else {
            this.obj.createTimeStart = new Date(this.commonSer.getTimeByType(this.time.type).startTime).getTime();
            this.obj.createTimeEnd = new Date(this.commonSer.getTimeByType(this.time.type).endTime).getTime();
        }
        this.page.pageNum = 1;
        this.getList();
    }

    //搜索
    search() {
        if (this.time.type == '99') {
            this.obj.createTimeStart = new Date(this.time.startTime).getTime();
            this.obj.createTimeEnd = new Date(this.time.endTime).getTime();
        }
        this.page.pageNum = 1;
        this.getList();
    }

    //删除
    deleteChange(data) {
        this.commonSer.confirmThing(`提示`, `确定删除变更?`, () => {

        });
    }

    //设置已读
    setReady(data, type) {
        const d = {
            id: data.id
        };
        this.overviewSer.updateReadStatus(d).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.getList();
                    if (type == 'not') this.message.success('已读');
                    if (type == 'go') this.router.navigate([`/app/main/${data.url}`]);
                }
            }
        );
    }
}
