import './style.scss'
import moment from 'moment';
import {DatePicker, Message} from '@hot/pc-es-cn-shop-ui'
import BaseFormControl from "../../baseFormControl"
import {JDDateRangeTpl} from "./tpl";
class JDDateRangeLine extends BaseFormControl {
    constructor(config) {
        super();
        this.setConfig(config);
        this.startTime = this.config.startTime;
        this.endTime = this.config.endTime;
        this.render();
    }
    
    setConfig(config) {
        this.config = $.extend(true, {}, {
            domData: {},
            defaultChecked: 0
        }, config);
    }
    render() {
        let $DateRangeNode = template.render(JDDateRangeTpl, {
            startTime: this.config.startTime,
            endTime: this.config.endTime,
            skin: this.config.domData.extConfig ? this.config.domData.extConfig.skin : ''
        });
        this.appendFormControl($DateRangeNode);
        this.startTimeInstance = new DatePicker({
            elem: this.element.find('.J_dateStart'),
            eventElem: this.element.find('.cd-rang-end'),
            positionElem: this.element.find('.J_dateRange_content'),
            trigger: 'click',
            type: 'datetime',
            min: new Date().getTime(),
            range: true,
            done: (value) => {
                this.startTime = value.split(' ' + this.startTimeInstance._config.range + ' ')[0];
                this.endTime = value.split(' ' + this.startTimeInstance._config.range + ' ')[1];
            }
        });
    }
    resetData(reset) {
        this.startTime = reset.split(' ' + this.startTimeInstance._config.range + ' ')[0];
        this.endTime = reset.split(' ' + this.startTimeInstance._config.range + ' ')[1];
    }
    getData() {
        let obj = {};
        let startTime = this.startTime;
        let endTime = this.endTime;
        if (startTime && endTime) {
            let nowDate = new Date().getTime();
            if (nowDate >= startTime.valueOf()) {
                Message.error('开始时间必须晚于当前时间');
                return null;
            }
            if (nowDate >= endTime.valueOf()) {
                Message.error('结束时间必须晚于当前时间');
                return null;
            }
            if (startTime >= endTime) {
                Message.error('结束时间必须晚于开始时间');
                return null;
            }
            obj['startTime'] = startTime;
            obj['endTime'] = endTime;
        } else {
            if (!startTime) {
                Message.error('请输入开始时间');
                return null;
            }
            if (!endTime) {
                Message.error('请输入结束时间');
                return null;
            }
        }
        return obj;
    }
}
export default JDDateRangeLine;