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/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 {SystemChangeService} from '../system-change.service';
import {change_status, change_time_type} from '../system-change.constants';
import {ChangeCreateComponent} from '../modal/change-create/change-create.component';
import {ChangeVerityComponent} from '../modal/change-verity/change-verity.component';
import {TransforComponent} from '../../work/modal/transfor/transfor.component';

@Component({
    selector: 'smart-change-manage',
    templateUrl: './change-manage.component.html',
    styles: []
})
export class ChangeManageComponent implements OnInit {
    @ViewChild('smartChangeCreate') smartChangeCreate: ChangeCreateComponent;
    @ViewChild('smartChangeVerity') smartChangeVerity: ChangeVerityComponent;
    @ViewChild('smartTransfor') smartTransfor: TransforComponent;
    timeFormat = 'yyyy-MM-dd';

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

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

    obj = {
        search: '',  //填报人
        status: null,  //状态
        startTime: null,  //开始时间
        endTime: null //结束时间
    };

    isDownLoad = false;

    constructor(private workSer: WorkService, private message: NzMessageService,
                private systemChangeSer: SystemChangeService,
                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.systemChangeSer.sysChangePage(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;
        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.smartChangeCreate.showAddModal('新增变更');
    }

    //提交审核
    handleEditModal(data) {
        this.smartChangeCreate.showEditModal(data, '提交审核');
    }

    //删除
    deleteChange(data) {
        this.commonSer.confirmThing(`提示`, `确定删除变更?`, () => {
            const d = {
                ids: [data.id]
            };
            this.systemChangeSer.sysChangeDelete(d).subscribe(
                (res) => {
                    if (res.errCode == 10000) {
                        this.message.success('删除成功');
                        this.getList();
                    }
                }
            );
        });
    }

    //审核 || 查看
    showChangeModal(data) {
        this.smartChangeVerity.showModal(data.id, '审核');
    }

    //转派
    showTransforModal(data) {
        this.smartTransfor.showTransforModal('变更转派', data.id);
    }

    //下载文件
    downLoad() {
        this.isDownLoad = true;
        const d = {
            format: 'excel'
        };
        this.systemChangeSer.sysChangeExport(d).subscribe(
            (res) => {
                this.isDownLoad = false;
                this.commonSer.downloadFile('变更.xlsx', res);
            }
        );
    }
}
