<template>
    <div class="senior-item-block" :style="`paddingLeft:${initData.labelWidth}px`">
        <div class="item-title" :style="`width:${initData.labelWidth}px`">{{ initData.title }}</div>
        <DatePicker
            type="daterange"
            class="item-block-width"
            :options="options"
            :editable="false"
            @on-change="getDateTime"
            :format="initData.format && initData.format.config ? initData.format.config : 'yyyy-MM-dd'"
            placeholder="请选择日期"
            v-model="dateTimeVal"
        />
    </div>
</template>
<script>
import SeniorQueryUtil from './util/seniorQueryUtil'
export default {
    name: "rbcDateQuery",
    data() {
        return {
            options: {
              disabledDate (date) {
                console.log({date})
              }
            },
            initID: this.initData.id,
            dateTimeVal: "",
            dateTime: 0
        };
    },
    props: {
        initData: {
            type: Object,
            required: true
        },
        defaultVal: {
            type: String,
            default: ""
        },
        emitFlag: {
            type: String,
            default: ""
        }
    },
    watch: {
        defaultVal: {
            handler(newVal, oldVal) {
                if (newVal != "") {
                    this.dateTimeVal = new Date(
                        this.getTimeStamp(this.defaultVal)
                    );
                }
            },
            immediate: true
        },
        initData: {
            handler(newVal, oldVal) {
                let disabledDate = function (date) {
                    if (newVal.config.choseRange) {
                        if (newVal.config.choseRange == "currBefore") {
                            return date && date.valueOf() > Date.now();
                        } else if (newVal.config.choseRange == "currAfter") {
                            return date && date.valueOf() < Date.now() - 86400000;
                        }
                    }
                };
                this.$set(this.options, "disabledDate", disabledDate);
            },
            immediate: true
        },
    },
    methods: {
        getDateStr(time) {
            var timeDate = new Date(time);
            var year = timeDate.getFullYear(); //当前年份
            var month = timeDate.getMonth() + 1; //当前月份
            var date = timeDate.getDate(); //天
            var timeStr = year + "-" + month + "-" + date;
            return timeStr;
        },
        getDateTime(dateTime) {
            let beginDate = "";
            let endDate = "";
            let suffix = " 00:00:00";
            if (dateTime[0] != "" && dateTime[1] != "") {
                beginDate = new Date(dateTime[0] + suffix).getTime();
                endDate = new Date(dateTime[1] + suffix).getTime();
            }
            let initData = JSON.parse(JSON.stringify(this.initData));
            let param = {};
            param.id = initData.id;
            param.name = {}
            param.value = {}
            param.name.min = initData.config.begin;
            param.name.max = initData.config.end;
            param.title = initData.title;
            param.value.min = beginDate;
            param.value.max = endDate;
            param.text = dateTime[0] + '-' + dateTime[1]
            // param.name = initData.config.name;
            param.type = "dateRange";
            param.dataType = "DATE";
            param.isChangeDatasource = true
            param.suffix = new Date(dateTime[0]).getFullYear()
            this.$emit("custom-query-condition-change", SeniorQueryUtil.initCondition(param, 'RANGE'), false, 'RANGE');
        },
        getTimeStamp(str) {
            if (str == "now") {
                let now = new Date();
                let year = now.getFullYear(); //当前年份
                let month = now.getMonth() + 1; //当前月份
                let day = now.getDate(); //天
                let dateStr = year + "-" + month + "-" + day + " 00:00:00";
                return new Date(dateStr).getTime();
            } else if (str == "last") {
                let now = new Date();
                let year = now.getFullYear(); //当前年份
                let month = now.getMonth() + 1; //当前月份
                let day = now.getDate(); //天
                // let hour = now.getHours(); //小时
                // let min = now.getMinutes(); //分
                // let sec = now.getSeconds(); //秒
                let dateStr = year - 1 + "-" + month + "-" + day + " 00:00:00";
                let last = new Date(dateStr).getTime();
                return last;
            } else if (str == "next") {
                let now = new Date();
                let year = now.getFullYear(); //当前年份
                let month = now.getMonth() + 1; //当前月份
                let day = now.getDate(); //天
                // let hour = now.getHours(); //小时
                // let min = now.getMinutes(); //分
                // let sec = now.getSeconds(); //秒
                let dateStr = year + 1 + "-" + month + "-" + day + " 00:00:00";
                let next = new Date(dateStr).getTime();
                return next;
            } else if (str == "nowBegin") {
                let now = new Date();
                let year = now.getFullYear(); //当前年份
                // let month = now.getMonth() + 1; //当前月份
                // let day = now.getDate(); //天
                // let hour = now.getHours(); //小时
                // let min = now.getMinutes(); //分
                // let sec = now.getSeconds(); //秒
                let dateStr = year + "-01-01 00:00:00";
                let nowBegin = new Date(dateStr).getTime();
                return nowBegin;
            } else if (str == "nowEnd") {
                let now = new Date();
                let year = now.getFullYear(); //当前年份
                // let month = now.getMonth() + 1; //当前月份
                // let day = now.getDate(); //天
                // let hour = now.getHours(); //小时
                // let min = now.getMinutes(); //分
                // let sec = now.getSeconds(); //秒
                let dateStr = year + "-12-31 00:00:00";
                let nowEnd = new Date(dateStr).getTime();
                return nowEnd;
            } else if (str == "monthBegin") {
                let now = new Date();
                let year = now.getFullYear(); //当前年份
                let month = now.getMonth() + 1; //当前月份
                let dateStr = year + "-" + month + "-01 00:00:00";
                let monthBegin = new Date(dateStr).getTime();
                return monthBegin;
            }
        }
    },
    created() {
        console.log('initData', this.initData)
        var that = this;
        GMS.vbus.$on("dateCloseSingleTag", function (data, emitFlag) {
            if (GMS.util.compareEmitFlag(that.emitFlag, emitFlag)) {
                let parseData = JSON.parse(data);
                if (parseData.id == that.initID) {
                    that.dateTimeVal = "";
                }
            }
        });
        GMS.vbus.$on("closeAllTag", function (emitFlag) {
            if (GMS.util.compareEmitFlag(that.emitFlag, emitFlag)) {
                that.dateTimeVal = "";
            }
        });
        GMS.vbus.$on("returnQueryData", function (data) {
            that.dateTimeVal = "";
            for (let i = 0; i < data.length; i++) {
                if (that.initID == data[i].id) {
                    that.dateTimeVal = new Date(data[i].value);
                }
            }
        });
    },
};
</script>
<style lang="less" scoped>
.senior-item-block {
    display: inline-block;
    box-sizing: border-box;
    padding-left: 100px;
    position: relative;
    width: 100%;
    .item-title {
        font-family: Microsoft YaHei;
        font-size: 12px;
        color: #555555;
        text-align: justify;
        width: 100px;
        padding-right: 10px;
        text-align: right;
        position: absolute;
        top: 6px;
        left: 0;
    }
    .item-block-width {
        min-width: 140px;
        width: 100%;
    }
}
.no-checked {
    display: inline-block;
    font-family: Microsoft YaHei;
    font-size: 12px;
    color: #222222;
    text-align: justify;
    margin-right: 5px;
    cursor: pointer;
    padding: 0 6px;
    border: 1px solid transparent;
    position: relative;
    top: 4px;
}
.checked {
    opacity: 0.5;
    background: #ffffff;
    border-color: #3477cb;
    border-radius: 2px;
    color: #3477cb;
}
</style>