
import { FormBuilder, Validators } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { BsModalService } from "ngx-bootstrap/modal";

import * as _ from 'lodash';
import { FlexTableComponent } from '../../../../shared/common/FlexTable.component';
import { OnInit, Component } from '@angular/core';

import { FrameRequest } from '../../../../shared/frame/frame-request';
import { AppComponent } from '../../../..';

const DATA_TYPE = "1";

@Component({
    selector: 'time_config',
    templateUrl: './time_config.component.html',
    styleUrls: ['./time_config.component.scss']
})

export class TimeConfigComponent extends FlexTableComponent implements OnInit {
    public query = {
        "year": "",
        "type": "",
    };

    QUERY_URL = "mk/mkCertTimeConfig/page.htm";
    DELETE_URL = "mk/mkCertTimeConfig/delete.htm";
    GET_URL = "mk/mkCertTimeConfig/get.htm";
    ADD_URL = "mk/mkCertTimeConfig/add.htm";
    EDIT_URL = "mk/mkCertTimeConfig/edit.htm";

    STATUS_OPEN_URL = "mk/mkCertTimeConfig/openStatus.htm";
    STATUS_CLOSe_URL = "mk/mkCertTimeConfig/closeStatus.htm";

    constructor(
        public bsModalService: BsModalService,
        fb: FormBuilder
    ) {
        super();
        this.item = {};
        this.formModel = fb.group({
            id: [null],
            'start_time': [null, Validators.compose([Validators.required])],
            'end_time': [null, Validators.compose([Validators.required])],
            'type': [null, Validators.compose([Validators.required])],
            'status': [null, Validators.compose([Validators.required])],
            
        });
    }

    async preSaveEvent(){
        if(!this.item.start_time){
            AppComponent.alert({ title: "信息提示", text: "请选择开始时间!" });
            return false;
        }
        if(!this.item.end_time){
            AppComponent.alert({ title: "信息提示", text: "请选择结束时间!" });
            return false;
        }

        let startTime = new Date(Date.parse(this.item.start_time));
        let endTime = new Date(Date.parse(this.item.end_time));
        if(startTime >= endTime){
            AppComponent.alert({ title: "信息提示", text: "开始时间不能大于等于结束时间!" });
            return false;
        }
        return true;
    }

    updateStatus(val){
        let checkedArray =  this.framePager.getCheckedArray();
        if(checkedArray.length == 0){
            AppComponent.alert({title:"信息提示",text:"请选择数据!"});
            return;
        }
        if(checkedArray.length > 1){
            AppComponent.alert({title:"信息提示",text:"只允许操作一条数据!"});
            return;
        }

        let url = (val ==1?this.STATUS_OPEN_URL:this.STATUS_CLOSe_URL);
        FrameRequest.requestEntity(url, {id: checkedArray[0].id}, (entity) => {
            AppComponent.alert({title:"信息提示",text:"状态更新成功!"});
           this.framePager.reload();
         })
    }


  
}


