import './dateRange.scss';
import JDDateRangeTpl from './dateRangeTpl';
import BaseFormControl from "../../baseFormControl";
import DatePicker from "../../../datePicker";
import Message from '../../../message'
class JDDateRange extends BaseFormControl {
    constructor(config) {
        super();
        this.config = this._getConfig(config);
        this.render();
    }
    
    _getConfig(config) {
        return $.extend(true, {}, {
            domData: {},
            moduleData: {},
            minDate: ''
        }, config);
    }
    
    render() {
        let $DateRangeNode = template.render(JDDateRangeTpl, {
            startTime: this.config.moduleData.startTime,
            endTime: this.config.moduleData.endTime,
        });
        this.appendFormControl($DateRangeNode);
        let minStartTime = new Date();
        if (this.config.moduleData.startTime) {
            this.startTime = this.config.moduleData.startTime;
        }
        if (this.config.moduleData.endTime) {
            this.endTime = this.config.moduleData.endTime
        }
        this.startTimeInstance = new DatePicker({
            elem: this.element.find('.J_startTime'),
            eventElem: this.element.find('.cd-icons-date-rang'),
            positionElem: this.element.find('.cd-daterange-panel'),
            trigger: 'click',
            type: 'datetime',
            range: true,
            min: minStartTime.getTime(),
            limitRange: 30,
            done: (value) => {
                this.startTime = value.split(' ' + this.startTimeInstance._config.range + ' ')[0];
                this.endTime = value.split(' ' + this.startTimeInstance._config.range + ' ')[1];
            }
        })
        ;
    }
    
    getData() {
        let result = {};
        let obj = {};
        let startTime = this.startTime;
        let endTime = this.endTime;
        let nowDate = new Date().getTime();
        if (startTime && endTime) {
            if (nowDate >= startTime.valueOf()) {
                Message.error('开始时间必须晚于当前时间');
                return null;
            }
            if (nowDate >= endTime.valueOf()) {
                Message.error('结束时间必须晚于当前时间');
                return null;
            }
            obj['startTime'] = startTime;
            obj['endTime'] = endTime;
        } else {
            if (!startTime) {
                Message.error('请输入开始时间');
                return null;
            }
            if (!endTime) {
                Message.error('请输入结束时间');
                return null;
            }
        }
        result[this.config.domData.name] = $.extend({}, obj);
        return result;
    }
}
export default JDDateRange;